Hi, we (as almost everyone) are using the JSON .NET For Unity package to deserialize our JSON objects that come from the server.
However, it works in the editor and on Android, but on iOS the deserialization of nullable types crashes. Here is the minimum code to reproduce:
public class SimpleResponse
{
public List<int?> NullableList { get; set; }
}
public class DeserializeClass : MonoBehaviour
{
private string json = "{'NullableList': [1, null, 2]}".Replace("'", "\"");
void Start()
{
Debug.Log("Deserialize...");
var deserializedClass = JsonConvert.DeserializeObject<SimpleResponse>(json);
Debug.Log($"NullableList: {deserializedClass.NullableList.Count}\n" +
$"[0]:{deserializedClass.NullableList[0].HasValue}\n" +
$"[1]:{deserializedClass.NullableList[1].HasValue}\n" +
$"[2]:{deserializedClass.NullableList[2].HasValue}");
}
}
As you can see, its pretty straight forward, throw a nullable type into the json and the whole thing explodes. (Remember, works in the Editor)
When running on iOS, the deserialisation crashes and the following error is thrown:
Exception ArgumentNullException: Value cannot be null. Parameter name: method at Newtonsoft.Json.Utilities.ValidationUtils.ArgumentNotNull (System.Object value, System.String parameterName) [0x00000] in <00000000000000000000000000000000>:0 at Newtonsoft.Json.Utilities.LateBoundReflectionDelegateFactory.CreateParameterizedConstructor (System.Reflection.MethodBase method) [0x00000] in <00000000000000000000000000000000>:0 at Newtonsoft.Json.Serialization.JsonArrayContract.CreateWrapper (System.Object list) [0x00000] in <00000000000000000000000000000000>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewList (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonArrayContract contract, System.Boolean& createdFromNonDefaultCreator) [0x00000] in <00000000000000000000000000000000>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.Object existingValue, System.String id) [0x00000] in <00000000000000000000000000000000>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue (Newtonsoft.Json.Serialization.JsonProperty property, Newtonsoft.Json.JsonConverter propertyConverter, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, Newtonsoft.Json.JsonReader reader, System.Object target) [0x00000] in <00000000000000000000000000000000>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject (System.Object newObject, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.String id) [0x00000] in <00000000000000000000000000000000>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in <00000000000000000000000000000000>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x00000] in <00000000000000000000000000000000>:0 at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <00000000000000000000000000000000>:0 at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <00000000000000000000000000000000>:0 at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <00000000000000000000000000000000>:0 at DeserializeClass.Start () [0x00000] in <00000000000000000000000000000000>:0
I wasted my whole day on this, but whatever I tried, it won’t run on iOS.
This is a very common issue that probably every iOS app developer must have run into. So I hope someone in the department of the more advanced users can give me some hints on that?