FormatException

Discussion and help for Easy Save 3
Post Reply
Gnarwi
Posts: 2
Joined: Wed Aug 14, 2019 9:53 pm

FormatException

Post 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]}}
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: FormatException

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Gnarwi
Posts: 2
Joined: Wed Aug 14, 2019 9:53 pm

Re: FormatException

Post by Gnarwi »

That solved it.

Thanks for your help.
Post Reply