silent failure saving Dictionary with bad value type

Discussion and help for Easy Save 3
Post Reply
nghugh
Posts: 2
Joined: Mon Nov 29, 2021 6:22 pm

silent failure saving Dictionary with bad value type

Post by nghugh »

hello, please see the following code:

Code: Select all

public class Person {
  public string Name;
  public int Age;

  // providing two parameter constructor deletes implicit zero parameter constructor
  public Person(string name, int age) {
    Name = name;
    Age = age;
  }
}

public class MyTestBehaviour: MonoBehaviour {
  public Dictionary<string, Person> MyPersonDictionary;
}
depending on if you serialize an instance of MyTestBehaviour, the field MyPersonDictionary, or an instance of Person, you get three different behaviors from Easy Save 3:

ES3.Save("MyTestBehaviour", testBehaviourInstance);
Silently discards the MyPersonDictionary field.

ES3.Save("MyPersonDictionary", MyPersonDictionary);
Throws exception with vague message: NotSupportedException: Types of System.Collections.Generic.Dictionary`2[System.String,Person] are not supported.

ES3.Save("MyPerson", personInstance);
Throws exception with helpful message: NotSupportedException: Type of Person is not supported as it does not have a parameterless constructor. Only value types, Components or ScriptableObjects are supportable without a parameterless constructor. However, you may be able to create an ES3Type script to add support for it.

i spent a long time debugging what was happening in my project with Dictionaries disappearing and sometimes throwing exceptions. i believe Easy Save 3 can be improved by always surfacing the helpful message you get if you try to serialize a Person directly.

thanks for your consideration
Last edited by nghugh on Tue Nov 30, 2021 9:50 pm, edited 1 time in total.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: silent failure saving Dictionary with bad value type

Post by Joel »

Hi there, and thanks for your feedback.

As you've worked out, your Person class is not a supported type because it doesn't have a parameterless constructor (see the Supported Types guide for more information: https://docs.moodkie.com/easy-save-3/es ... ted-types/)
ES3.Save("MyTestBehaviour", testBehaviourInstance);
Silently discards the MyPersonDictionary field.
Because the Person type is not supported, any fields which use the Person type are not supported. It's normal for their to be unsupported fields in classes and as such an error isn't thrown (this is the same with all serializers). If we threw an error in this case, almost all Unity classes would become unserializable, as they rely on the fact that certain types of data aren't serialized to operate.
ES3.Save("MyPersonDictionary", MyPersonDictionary);
Throws exception with vague message: NotSupportedException: Types of System.Collections.Generic.Dictionary`2[System.String,Person] are not supported.
The error is thrown in this circumstance because the Type you're storing is itself not supported (because it uses the Person class). This is an invalid operation and as such a critical error is thrown. A more specific error isn't thrown in this case because it's a failure of a generic type rather than the type itself, meaning that there could be multiple issues leading to this. However, I'll change the error message so that when this happens with a generic type it suggests checking that the generic type is supported.
ES3.Save("MyPerson", personInstance);
Throws exception with helpful message: NotSupportedException: Type of Person is not supported as it does not have a parameterless constructor. Only value types, Components or ScriptableObjects are supportable without a parameterless constructor. However, you may be able to create an ES3Type script to add support for it.
In this case a specific error can be thrown because the type itself is causing the error, rather than a generic parameter provided to that class.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
nghugh
Posts: 2
Joined: Mon Nov 29, 2021 6:22 pm

Re: silent failure saving Dictionary with bad value type

Post by nghugh »

makes sense, thanks for the detailed reply, Joel (:
Post Reply