It gets worse. Trying to circumvent this leak with a custom JSON loader that uses a local instance of JsonSerializer, it turns out that when already read data is not a primitive (e.g. a map), then its members are NULLed when the local serializer ceases to exist.
To illustrate the problem:
class CustomJsonLoader<Class T>
{
static bool LoadData(string string_data, out T data, out string errorMessage)
{
string error;
JsonSerializer serializer = new JsonSerializer();
if (!serializer.ReadFromString(data, string_data, error))
{
errorMessage = string.Format("Cannot load provided JSON data (deserialization error) - check syntax: %1", error);
return false;
}
return true;
}
}
class TestData1
{
int m_CustomVar1;
}
class TestData2
{
int m_CustomVar2;
}
class CustomData1
{
ref map<string, ref TestData1> m_TestData1;
void Load_CustomJsonLoader()
{
string test1 = "{\"m_TestData1\": {\"key1\": {\"m_CustomVar1\": 1}}}";
string errMsg;
CustomJsonLoader<CustomData1>.LoadData(test1, this, errMsg);
Print(errMsg);
}
void Load_VanillaJsonLoader()
{
string test1 = "{\"m_TestData1\": {\"key1\": {\"m_CustomVar1\": 1}}}";
string errMsg;
JsonFileLoader<CustomData1>.LoadData(test1, this, errMsg);
Print(errMsg);
}
}
class CustomData2
{
ref map<string, ref TestData2> m_TestData2;
void Load_CustomJsonLoader()
{
string test2 = "{\"m_TestData2\": {\"key2\": {\"m_CustomVar2\": 2}}}";
string errMsg;
CustomJsonLoader<CustomData2>.LoadData(test2, this, errMsg);
Print(errMsg);
}
void Load_VanillaJsonLoader()
{
string test2 = "{\"m_TestData2\": {\"key2\": {\"m_CustomVar2\": 2}}}";
string errMsg;
JsonFileLoader<CustomData2>.LoadData(test2, this, errMsg);
Print(errMsg);
}
}
CustomData1 customData1a = new CustomData1();
customData1a.Load_CustomJsonLoader();
CustomData2 customData2a = new CustomData2();
customData2a.Load_CustomJsonLoader();
Print(customData1a.m_TestData1["key1"]);
Print(customData1a.m_TestData1.Count());
Print(customData2a.m_TestData2["key2"]);
Print(customData2a.m_TestData2.Count());
CustomData1 customData1b = new CustomData1();
customData1b.Load_VanillaJsonLoader();
CustomData2 customData2b = new CustomData2();
customData2b.Load_VanillaJsonLoader();
Print(customData1b.m_TestData1["key1"]);
Print(customData2b.m_TestData2["key2"]);