Language

JSONUtility creates a null object of subclass even when no data

Hi,

The JsonUtility's FromJson behaves strangely for subclass parsing..

Example:

[Serializable]
public class Player {
public int id;
public string name;
public PlayerSkills skills;
public static new Player createFromJSON(string jsonstring) {
return JsonUtility.FromJson<Player>(jsonstring);
}
}

If the json data received from server is {"id":123, "name":"Rajat"} and the data for skills is not given, the values of the field skills is still an object of class PlayerSkills instead of being null.. and the value for fields in skills are default values, eg 0 for a int field..

How to differentiate between server sending the data of skills as value zero and server not sending the data at all..

If the JSON data contains values that do not map to fields in MyClass then those values will simply be ignored, and if the JSON data is missing values for fields in MyClass, then thosefields will be left at theirconstructed values in the returnedobject.

We are trying to port our app form android, and the behaviour there for json parsing will the that the field will have null value if there is no data sent for it..