Hi everyone,
I’m trying to use the brand new System.Text.Json library with Il2CPP builds of Unity (using 2019.4.16f).
The issue is that for some reason, I cannot prevent the builder from stripping some parts of the library, which results in errors at runtime.
When trying to serialize an simple float array float[3]
, I get the error :
ExecutionEngineException: Attempting to call method 'System.Text.Json.Serialization.Converters.ArrayConverter`2[[System.Single[ ], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Single, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]::.ctor' for which no ahead of time (AOT) code was generated.
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[ ] parameters) [0x00000] in <00000000000000000000000000000000>:0
at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic) [0x00000] in <00000000000000000000000000000000>:0
It seems that the generic System.Text.Json.Serialization.Converters.ArrayConverter class of System.Text.Json could not be included in the build.
I had this issue with string and base types before, but I solved it by adding a link.xml to my assets folder to prevent some stripping, which for some reason doesn’t work for arrays and other more complex types.
<linker>
<assembly fullname="System.Text.Json">
<namespace fullname="System.Text.Json" preserve="all" />
<namespace fullname="ystem.Text.Json.Serialization" preserve="all" />
<namespace fullname="System.Text.Json.Serialization.Converters" preserve="all" />
<type fullname="System.Text.Json.Serialization.Converters.*" preserve="all"/>
</assembly>
</linker>
To use System.Text.Json on Unity, I use the netstandard2.0 version of the System.Text.Json package, which does not use System.Reflection.Emit (unsupported on Il2CPP).
Did anyone manage to make System.Text.Json to work with Il2CPP ?