neuecc / Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
MIT License
2.36k stars 267 forks source link

Unity/IL2CPP Error when initializing Utf8Json.Resolvers.StandardResolver.Default #107

Open travisbrownjohn opened 6 years ago

travisbrownjohn commented 6 years ago

output_log.txt I get this error:

NotSupportedException: mscorlib\System.Reflection.Emit\AssemblyBuilder.cpp(20) : Unsupported internal call for IL2CPP:AssemblyBuilder::basic_init - System.Reflection.Emit is not supported. (see the attached log for full details)

Running the simple code below. Built in Unity 2018.2.0f2, PC Standalone, IL2CPP. I have the same issue with Android IL2CPP.

`
using UnityEngine; using Utf8Json; using Utf8Json.Resolvers;

public class TestUtf8Json : MonoBehaviour {

// Use this for initialization
void Awake () {

    Debug.Log(">>> TestUtf8Json AWAKE formatters = new IJsonFormatter[]");
    // use global-singleton CompositeResolver.
    // This method initialize CompositeResolver and set to default JsonSerializer
    var formatters = new IJsonFormatter[]{ };

    Debug.Log(">>> TestUtf8Json AWAKE resolvers = new IJsonFormatterResolver[]");
    var resolvers = new IJsonFormatterResolver[]{
        StandardResolver.Default
    };

    CompositeResolver.RegisterAndSetAsDefault(formatters, resolvers);

    Debug.Log(">>> TestUtf8Json AWAKE Complete");
}

}`
pantone170145 commented 5 years ago

In my Unity/IL2CPP environment it worked with the following code.

// CompositeResolver is singleton helper for use custom resolver.
// Ofcourse you can also make custom resolver.
Utf8Json.Resolvers.CompositeResolver.RegisterAndSetAsDefault(
    // use generated resolver first, and combine many other generated/custom resolvers
    Utf8Json.Resolvers.GeneratedResolver.Instance,

    // for primitive
    Utf8Json.Resolvers.BuiltinResolver.Instance,
    // for enum
    Utf8Json.Resolvers.EnumResolver.Instance,
    // for unity
    Utf8Json.Unity.UnityResolver.Instance

    // It does not work under IL2CPP and caused crash.
    // Utf8Json.Resolvers.StandardResolver.Default,
);