rpgmaker / NetJSON

Faster than Any Binary? Benchmark: http://theburningmonk.com/2014/08/json-serializers-benchmarks-updated-2/
MIT License
230 stars 29 forks source link

The type initializer for 'NetJSONCachedSerializer`1' threw an exception. #201

Closed bryanjhogan closed 6 years ago

bryanjhogan commented 6 years ago

I am using NetJSON 1.2.6 in a .NET Core 2.1 console application.

I get the below error when executing this - var json = NetJSON.NetJSON.Serialize(_items);

+$exception {System.TypeInitializationException: The type initializer for 'NetJSONCachedSerializer`1' threw an exception. ---> System.TypeLoadException: Access is denied: 'NetJSON.NetJSONSerializer`1[System.Collections.Generic.List`1[MyApplication.Person]]'.
   at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
   at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
   at System.Reflection.Emit.TypeBuilder.CreateTypeInfo()
   at NetJSON.NetJSON.Generate(Type objType)
   at NetJSON.NetJSON.NetJSONCachedSerializer`1.GetSerializer()
   at NetJSON.NetJSON.NetJSONCachedSerializer`1..cctor()
   --- End of inner exception stack trace ---
   at NetJSON.NetJSON.GetSerializer[T]()
   at NetJSON.NetJSON.Serialize[T](T value)
rpgmaker commented 6 years ago

Hi, could you provide the class structure you are trying to serialize?

Thanks,

bryanjhogan commented 6 years ago

I am testing multiple serializers, my model has had no problem with Jil, MessagePack, MsgPack.Cli, Newtonsoft.Json, protobuf-net, Utf8Json.

This is a simplification, but close enough.

public partial class Person
{
    public int PersonId { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public DateTime DateOfBirth { get; set; }
    public byte[] HashValue { get; set; }    
}
rpgmaker commented 6 years ago

This is a simple model indeed. Using it with .net framework works fine. I would look into the reason why it is showing access denied exception. It should not do that.

Thanks,

rpgmaker commented 6 years ago

I fixed it and pushed the changes to 1.2.7 nuget package. Please test and let me know if you find any other issues.

Thanks,

bryanjhogan commented 6 years ago

Thanks. Will test and let you know.