Load type of 'object' from the location of 'cache' will not work properly if it was originally a collection(or a list).

Discussion and help for Easy Save 3
Post Reply
wodnr97
Posts: 3
Joined: Wed Feb 03, 2021 5:41 am

Load type of 'object' from the location of 'cache' will not work properly if it was originally a collection(or a list).

Post by wodnr97 »

Hi.


Background:
- Unity 2020.3.24f1 LTS
- ES3 3.4.0

1. Saves 'array' or 'list' as an 'object' type via ES3.Save(key, value) in cache.
more exactly it is originally a type of 'System.Collection.Generic.List'
(e.g. System.Collections.Generic.List`1[System.Boolean])

2. Error will occur when I load them from cache (cache that loaded file's data).

Problem:
- ES3JSONReader throws (FormatException: Expected '{' or "null", found '['. )
I think It cannot read object as Collection if I load object type from cache.

Full Error Stack:
ErrorStacks.png
ErrorStacks.png (113.35 KiB) Viewed 591 times
Data that occured error:
error data.png
error data.png (10.96 KiB) Viewed 591 times

It would be nice if you could solve this quickly. It is urgent because we need to ship this until tomorrow.

With regards.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Load type of 'object' from the location of 'cache' will not work properly if it was originally a collection(or a lis

Post by Joel »

Hi there, and thanks for getting in contact.

This is expected behaviour if you're telling it to save and load it as a System.Object. Although the data inherits from System.Object, it's type is not System.Object and therefore can't be deserialised as such (System.Object has no fields).

In this case you would either need to save it without 'object' as the generic parameter, or load it using ES3.Load<List<bool>>, or both. Otherwise it's impossible for any serialiser to know that you want to load it as anything other than a System.Object.

I.e. this will not work:

Code: Select all

var list = new List<bool>(new bool[]{ true, false, true, false });
var settings = new ES3Settings(ES3.Location.Cache);
ES3.Save<object>("list", list, settings);
object newList = ES3.Load<object>("list", settings);
Debug.Log(((List<bool>)newList).Count);
But this will work:

Code: Select all

var list = new List<bool>(new bool[]{ true, false, true, false });
var settings = new ES3Settings(ES3.Location.Cache);
ES3.Save("list", list, settings);
object newList = ES3.Load<object>("list", settings);
Debug.Log(((List<bool>)newList).Count);
And this will work:

Code: Select all

var list = new List<bool>(new bool[]{ true, false, true, false });
var settings = new ES3Settings(ES3.Location.Cache);
ES3.Save("list", list, settings);
object newList = ES3.Load<List<object>>("list", settings);
Debug.Log(((List<bool>)newList).Count);
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply