I want to overwrite an existing object with some fields and dicitionary.
class MyInt {int value; int enchantment} // stat
enum MyEnum {FIRE, COLD, POISON}
class Creature {
MyInt hp;
MyInt mana;
Dictionary<MyEnum, MyInt> resists;
}
...
Creature c = ...; // assigned elswhere (from Unity-Inspector)
JsonConvert.PopulateObject(json_str, c);
After Populating object from JSon, fields hp and mana are correctly overwritten, but values of dictionary resists are recreated. Dictionary itself will be not recreated and has same hash_code after populating, what is correct. But the exemplars of MyInt
will be recrated (with new HashCodes etc.).
My JSON Looks like that:
{
"hp": "3",
"mana": "7",
"resists": {
"FIRE": "11",
"COLD": "0",
"POISON": "33"
}
}
I have found setting ObjectCreationHandling = ObjectCreationHandling.Reuse
but unfortunately it is same as Auto
and does not help in this case.
Is any way to force JConvert overwrite the values of dictionary, without recreating new MyInt
objects, like it works for fields hp/mana? Something like JConvert.PopulateTransitive(...)
?