This project has moved and is read-only. For the latest updates, please go here.

Json.NET on Unity iOS

May 4, 2012 at 10:06 AM
Edited May 4, 2012 at 10: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:

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.

Is it possible to modify or improve to overcome this problem?

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

Jul 3, 2014 at 9:25 PM 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.