Json.NET on Unity iOS

May 4, 2012 at 9:06 AM
Edited May 4, 2012 at 9:10 AM

Hi~ We are developing a game with Unity on Web and iOS. We would like to use json between our server and web client, or server and iOS client. Json.NET is best solution we can find. But we encounter  a problem. When using Json.NET to serialize a struct or class. like this:

[Serializable]
public struct Test1234
{
	public string str1;	
	public string str2;	
}

Test1234 t;
t.str1 = "aaa";
t.str2 = "bbb";
string sStr = Newtonsoft.Json.JsonConvert.SerializeObject(t);

It performs good on Windows/Mac/Web/Android, but on iOS, it shows

(Filename: /Applications/buildAgent/work/b0bcff80449a48aa/Runtime/ExportGenerated/iPhonePlayer-armv7/UnityEngineDebug.cpp Line: 43)

ExecutionEngineException: Attempting to JIT compile method '(wrapper dynamic-method) Test1234:CreateTest1234 ()' while running with --aot-only.
  
at System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) [0x00000] in :0 
at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) [0x00000] in :0 
at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method) [0x00000] in :0 
at System.Reflection.Emit.DynamicMethod.CreateDelegate (System.Type delegateType) [0x00000] in :0 
at Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory.CreateDefaultConstructor[Object] (System.Type type) [0x00000] in :0 
at Newtonsoft.Json.Serialization.DefaultContractResolver.GetDefaultCreator (System.Type createdType) [0x00000] in :0 
at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract (Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in :0 
at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract (System.Type objectType) [0x00000] in :0  
at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract (System.Type objectType) [0x00000] in :0 
at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00000] in :0 
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.GetContractSafe (System.Object value) [0x00000] in :0 
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) [0x00000] in :0 
at Newtonsoft.Json.JsonSerializer.SerializeInternal (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) [0x00000] in :0 
at Newtonsoft.Json.JsonSerializer.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value) [0x00000] in :0 
at Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value, Formatting formatting, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in :0 
at Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value) [0x00000] in :0 
at TestDicSerialize.SerializeWithNewtonJson () [0x00000] in :0 
at TestDicSerialize.OnGUI () [0x00000] in :0 

We have found others has same problem. http://answers.unity3d.com/questions/169651/jsonnet-ios-and-aot.html

Is it possible to modify or improve Json.net to overcome this problem?

If anything we can help, please tell us, Thanks!

Jul 3 at 8:25 PM
Json.net uses Reflection.Emit to generate objects. This will crash Unity3d running on IOS. You need to use a variant which does not use Reflection.Emit.

That said, currently all variants are flawed in some way.