1

I am currently trying to deserialize a json string of type Vector3[][] for Unity (for a virtual reality project) and it is throwing the exception above. My json string is as follows:

[[{"x":-2.343096,"y":1.72245443,"z":0.0,"normalized":{"x":-0.805718362,"y":0.592298865,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":2.9080832,"sqrMagnitude":8.456948},{"x":-2.343096,"y":1.72245443,"z":0.0,"normalized":{"x":-0.805718362,"y":0.592298865,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":2.9080832,"sqrMagnitude":8.456948},{"x":-2.30125523,"y":1.61087871,"z":0.0,"normalized":{"x":-0.819231868,"y":0.573462367,"z":0.0,"normalized":{"x":-0.8192319,"y":0.5734624,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.00000012},"magnitude":0.99999994,"sqrMagnitude":0.99999994},"magnitude":2.80904,"sqrMagnitude":7.89070559},{"x":-2.24546719,"y":1.4853555,"z":0.0,"normalized":{"x":-0.834037364,"y":0.5517079,"z":0.0,"magnitude":1.0,"sqrMagnitude":0.99999994},"magnitude":2.692286,"sqrMagnitude":7.248404},{"x":-2.21757317,"y":1.38772666,"z":0.0,"normalized":{"x":-0.8476986,"y":0.5304781,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":2.61599231,"sqrMagnitude":6.843416},{"x":-2.17573214,"y":1.27615035,"z":0.0,"normalized":{"x":-0.8625732,"y":0.5059323,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":2.522374,"sqrMagnitude":6.36237},{"x":-2.147838,"y":1.17852151,"z":0.0,"normalized":{"x":-0.8766962,"y":0.481044352,"z":0.0,"magnitude":1.0,"sqrMagnitude":0.99999994},"magnitude":2.4499228,"sqrMagnitude":6.00212145},{"x":-2.09205,"y":0.997210741,"z":0.0,"normalized":{"x":-0.90269357,"y":0.430284023,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":2.317564,"sqrMagnitude":5.371103},{"x":-2.05020928,"y":0.829846859,"z":0.0,"normalized":{"x":-0.926946759,"y":0.375192851,"z":0.0,"magnitude":1.0,"sqrMagnitude":0.99999994},"magnitude":2.21178746,"sqrMagnitude":4.892004},{"x":-2.02231526,"y":0.6764293,"z":0.0,"normalized":{"x":-0.9483558,"y":0.3172085,"z":0.0,"magnitude":1.0,"sqrMagnitude":0.99999994},"magnitude":2.13244367,"sqrMagnitude":4.5473156},{"x":-1.99442124,"y":0.5230123,"z":0.0,"normalized":{"x":-0.967293262,"y":0.253660679,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":2.061858,"sqrMagnitude":4.251258},{"x":-1.99442124,"y":0.383542776,"z":0.0,"normalized":{"x":-0.982006431,"y":0.1888475,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":2.03096557,"sqrMagnitude":4.124821},{"x":-1.980474,"y":0.230125785,"z":0.0,"normalized":{"x":-0.99331665,"y":0.115420744,"z":0.0,"normalized":{"x":-0.9933167,"y":0.115420751,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":0.99999994,"sqrMagnitude":0.99999994},"magnitude":1.99379921,"sqrMagnitude":3.97523522}],[{"x":-0.9344492,"y":-4.748954,"z":0.0,"normalized":{"x":-0.193067357,"y":-0.9811855,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":4.84001637,"sqrMagnitude":23.4257584},{"x":-0.9344492,"y":-4.748954,"z":0.0,"normalized":{"x":-0.193067357,"y":-0.9811855,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":4.84001637,"sqrMagnitude":23.4257584}],[{"x":0.7112968,"y":-4.66527176,"z":0.0,"normalized":{"x":0.1507245,"y":-0.988575757,"z":0.0,"normalized":{"x":0.150724515,"y":-0.9885758,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":0.99999994,"sqrMagnitude":0.9999999},"magnitude":4.719185,"sqrMagnitude":22.2707043},{"x":0.7112968,"y":-4.66527176,"z":0.0,"normalized":{"x":0.1507245,"y":-0.988575757,"z":0.0,"normalized":{"x":0.150724515,"y":-0.9885758,"z":0.0,"magnitude":1.0,"sqrMagnitude":1.0},"magnitude":0.99999994,"sqrMagnitude":0.9999999},"magnitude":4.719185,"sqrMagnitude":22.2707043}]]

I am using the code:

Vector3[][] allLines = JsonConvert.DeserializeObject<Vector3[][]>(jsonString);

Why does this exception usually happen?

Exception Screenshot

System.NotSupportedException: System.Reflection.Emit.DynamicMethod::ctor
at System.Reflection.Emit.DynamicMethod..ctor (System.String name; System.Type returntype. System.Type[] paremeterTypes, System.Type [...]
at Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory.CreateDynamicMethod [...]
At "" CreatenewLoist
At "" CreateList
At "" CreateValueInternal
At "" Desiarilize
At "" DeserializeInternal
at "" DeserializeObject
at "" DeserializeObject[T]
at Draw.Start
dbc
  • 104,963
  • 20
  • 228
  • 340
a6i09per5f
  • 260
  • 1
  • 15
  • Im going to try to but I cant copy and paste it, because I can only show it through a text element on the Oculus Quest 2 (Visual Studio Debugger does not work yet for vr programming, because the Oculus quest 2 is a separate computer). I'll try get you some details though that might be relevant. Edit: Ill try get a screenshot, i think that would be possible. – a6i09per5f May 25 '21 at 19:56
  • Are you sure the json string is a valid one to be deserialized? I would create a valid vector and serialize to json and compare the two to see what are you missing – Mocas May 25 '21 at 20:21
  • I tried to write out the methods from the exception string. Hope that helps. You can zoom in on the picture though to read it, maybe that helps, it helped me to read it. @Mocas Yes I am sure, because I tried it to deserialize it on the desktop application where I serialize the Vector[][] and it worked (I serialized it and then desierialized it both in the same application and it worked, but as soon as I put it in a json file, transfer it to the oculus quest 2, open it up there, it doesnt work anymore. – a6i09per5f May 25 '21 at 20:26
  • the amount of characters in the jsonstring on the quest2 and in the jsonstring on the desktop computer is exactly the same, so i assume that nothing of the string changed during the transfer. – a6i09per5f May 25 '21 at 20:28
  • Can you try using JsonSerializer.Deserialize and see if you still get an error? https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializer.deserialize?view=net-5.0 – Mocas May 25 '21 at 20:30
  • 1
    Hmm OK. That traceback looks a lot like the one from [Unity Json.net System.Reflection.Emit error in iOS](https://stackoverflow.com/q/41582580/3744182) and [Json.NET under Unity3d for iOS](https://stackoverflow.com/q/16359628/3744182). You're running under Oculus not iOS but the problem might be the same -- an attempt to use runtime code generation to generate an object constructor when the platform does not allow it. You might try the solutions from those questions. – dbc May 25 '21 at 20:33

1 Answers1

2

Ok Guys. It worked. So I did exactly what the post said that @dbc linked:

  1. Get the \unityprojects\football\Packages\Newtonsoft.Json.13.0.1\lib\net20\Newtonsoft.json.dll file where "football" is the projectname.

  2. Put it into the folder \assemblies\AOT folder in the unityproject

  3. Create the "link.xml" file and put it into the assets folder with the following content:

         <linker>
         <assembly fullname="Your.Dto.Package.Name" preserve="all" />
         <assembly fullname="System.Core">
             <type fullname="System.Linq.Expressions.Interpreter.LightLambda" preserve="all" />
         </assembly></linker>
    

Hope that helps if someone has the same problem.

a6i09per5f
  • 260
  • 1
  • 15