- Home /
Why are my null JSON variables returning fully initialized?
I'm using a JsonUtility.ToJson and FromJson to save/load data. One part of the data for example is an InventorySlot class who has a public variable Item that by default is null. When creating a new character I specify their equipment as a list of 12 new InventorySlot() which means that there should be 12 null Item variables within the list.
However, when I FromJson the saved data back all of the Item variables within each InventorySlot are initialized as a new Item(), not as a null.
This breaks past all my null reference checks within the game.... I could rewrite the checks, not a HUGE deal, but is there an easy way to make Json save null as null? Some option I am missing?
Sample code: The InventorySlot class:
[Serializable]
public class InventorySlot {
public int slotNumber = -1;
public int itemAmount = 0;
public Item item = null;
public InventorySlotTypes slotType = InventorySlotTypes.Null;
}
the InventorySlot being initialized during character creation (new save file creation):
//initialize equipment
for (int i = 0; i <= 18; i++) {
this.equipmentSlots.Add(new InventorySlot());
this.equipmentSlots[i].slotNumber = i;
this.equipmentSlots[i].slotType = InventorySlotTypes.Equipment;
}
There is no touching of the equipmentSlots after that, I then toJson, binaryformatter serialize to a file, deserialize on load, and fromJson back, and all of the item variables are initialized as new Item()..?
I found a forum post stating serialization will force it to be initialized... a problem I've run into clicking on monobehaviors with public variables will force initialization for the inspector... But I've been serializing/deserializing for months before I started using JSON and I never ran into a problem here?...
I think basic binaryformatting was not forcing initialization on class variables, where JSON requires it for its structuring...?
Answer by malkere · Sep 09, 2017 at 08:11 AM
It seems that JSON will force serialization on anything and everything it touches. Simply writing out binary data did not force serialization on null objects so I never ran into this before.
I've found that even calling ToJson with the equipment list being reference will force serialization on the entire list and all child variables even in runtime. Even copying the list without explicit recreation of any nested non-generics (ie: child lists) would serialize all said variables removing any and all null values in runtime (big probs if you are expecting things to be null).
Using a check on load to look for any Item with the default values and setting it to null before importing solved that. And making sure to use " new List(ListToSave) " on anything being pulled into the class ToJson will be called on (including all nested classes) prevented runtime serialization of anything I didn't want changed.
Answer by Bunny83 · Sep 09, 2017 at 08:14 AM
Unity's JsonUtility class behaves roughly the same as it's normal serialization system but will less features. So have a look at the script serialization documentation. Especially the section "When might the serializer behave unexpectedly?"
Wow, just WOW! No support for null for custom classes
That's $$anonymous$$d blowing...
Your answer
![](https://koobas.hobune.stream/wayback/20220612134453im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
About Json Datapath. 1 Answer
LitJson null bug 0 Answers
Recieving Null when I should not be with JSONParse.js 0 Answers
how can I check if an object is null? 4 Answers
what is null 1 Answer