Page 1 of 1

FormatException

Posted: Wed Aug 14, 2019 10:10 pm
by Gnarwi
Im trying to save a simple List<int> but it keeps throwing this exception when im trying to load it : [*]FormatException: Expected '}', found '0'.[/*]
Am i missing something obvious here?

Here are my Save / Load methods. Load() is being called on Start() and Save() is on OnApplicationQuit().

Code: Select all

    public void Save()
    {
        List<int> ids = new List<int>();

        for (int i = 0; i < items.Count; i++)
        {
            ids.Add(items[i].id);
        }

        ES3.Save<List<int>>("Inventory", ids);
      
    }

Code: Select all

    public void Load()
    {

        List<int> ids = new List<int>();

        if (ES3.FileExists())
        {
            if (ES3.KeyExists("Inventory"))
            {

                ES3.LoadInto<List<int>>("Inventory", ids);

            }
            
        }

    }
And here is what it outputs. Seems to save just fine?

Code: Select all

{"Inventory":{"__type":"System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]],mscorlib","value":[0,2,1]}}

Re: FormatException

Posted: Thu Aug 15, 2019 8:59 am
by Joel
Hi there,

You need to use ES3.Load rather than ES3.LoadInto. I.e.

ids = ES3.Load<List<int>>("Inventory");

ES3.LoadInto is for loading data into existing objects. As your list contains value types, this is not possible. In your case, as you've provided an empty List as a parameter, Easy Save expects an empty List in the data. Instead it finds an integer, triggering a FormatException.

All the best,
Joel

Re: FormatException

Posted: Thu Aug 15, 2019 9:41 am
by Gnarwi
That solved it.

Thanks for your help.