0
Answered

JSON.Net Runtime issues

John Karczewski 4 years ago updated by Jesse Ford 4 years ago 7

We're trying to incorporate Bolt into one of our Unity projects and ran into a conflict with one of the internal tools we are using.  The tool requires newtonsoft.json runtime functionality, but enabling that appears to wreak havoc with Bolt/Ludiq runtime deserialization.  While everything works fine in the editor, during builds, deserialization fails with errors like:

Failed to deserialize behaviour.
System.Runtime.Serialization.SerializationException: Deserialization into 'Bolt.Variables' failed. ---> System.TypeInitializationException: An exception was thrown by the type initializer for Ludiq.MemberUtility ---> System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
  at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)
  at System.Reflection.Assembly.GetTypes () [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/Assembly.cs:371 
  at Ludiq.TypeUtility+<>c.<get_availableTypes>b__8_0 (System.Reflection.Assembly a) [0x00000] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Reflection\TypeUtility.cs:69 
  at System.Linq.Enumerable+<CreateSelectManyIterator>c__Iterator12`2[System.Reflection.Assembly,System.Type].MoveNext () [0x00059] in /Users/builduser/buildslave/mono/build/mcs/class/System.Core/System.Linq/Enumerable.cs:1871 
  at System.Collections.Generic.List`1[System.Type].AddEnumerable (IEnumerable`1 enumerable) [0x0001a] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:128 
  at System.Collections.Generic.List`1[System.Type]..ctor (IEnumerable`1 collection) [0x00025] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:65 
  at System.Linq.Enumerable.ToArray[Type] (IEnumerable`1 source) [0x00029] in /Users/builduser/buildslave/mono/build/mcs/class/System.Core/System.Linq/Enumerable.cs:2403 
  at Ludiq.TypeUtility.get_availableTypes () [0x00007] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Reflection\TypeUtility.cs:69 
  at Ludiq.MemberUtility..cctor () [0x00000] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Reflection\MemberUtility.cs:18 
  --- End of inner exception stack trace ---
  at Ludiq.OptimizedReflection.GetPropertyAccessor (System.Reflection.PropertyInfo propertyInfo) [0x0001b] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Reflection\Optimization\OptimizedReflection.cs:173 
  at Ludiq.OptimizedReflection.SetValueOptimized (System.Reflection.PropertyInfo propertyInfo, System.Object target, System.Object value) [0x00000] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Reflection\Optimization\OptimizedReflection.cs:159 
  at Ludiq.FullSerializer.fsMetaProperty.Write (System.Object context, System.Object value) [0x00046] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Dependencies\FullSerializer\Reflection\fsMetaProperty.cs:131 
  at Ludiq.FullSerializer.fsReflectedConverter.TryDeserialize (Ludiq.FullSerializer.fsData data, System.Object& instance, System.Type storageType) [0x000b3] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Dependencies\FullSerializer\Converters\fsReflectedConverter.cs:105 
  at Ludiq.FullSerializer.fsSerializer.InternalDeserialize_5_Converter (System.Type overrideConverterType, Ludiq.FullSerializer.fsData data, System.Type resultType, System.Object& result) [0x0001a] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Dependencies\FullSerializer\fsSerializer.cs:861 
  at Ludiq.FullSerializer.fsSerializer.InternalDeserialize_4_Cycles (System.Type overrideConverterType, Ludiq.FullSerializer.fsData data, System.Type resultType, System.Object& result) [0x00032] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Dependencies\FullSerializer\fsSerializer.cs:851 
  at Ludiq.FullSerializer.fsSerializer.InternalDeserialize_3_Inheritance (System.Type overrideConverterType, Ludiq.FullSerializer.fsData data, System.Type storageType, System.Object& result, System.Collections.Generic.List`1& processors) [0x00159] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Dependencies\FullSerializer\fsSerializer.cs:826 
  at Ludiq.FullSerializer.fsSerializer.InternalDeserialize_2_Version (System.Type overrideConverterType, Ludiq.FullSerializer.fsData data, System.Type storageType, System.Object& result, System.Collections.Generic.List`1& processors) [0x0011c] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Dependencies\FullSerializer\fsSerializer.cs:730 
  at Ludiq.FullSerializer.fsSerializer.InternalDeserialize_1_CycleReference (System.Type overrideConverterType, Ludiq.FullSerializer.fsData data, System.Type storageType, System.Object& result, System.Collections.Generic.List`1& processors) [0x00049] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Dependencies\FullSerializer\fsSerializer.cs:675 
  at Ludiq.FullSerializer.fsSerializer.TryDeserialize (Ludiq.FullSerializer.fsData data, System.Type storageType, System.Type overrideConverterType, System.Object& result) [0x0003d] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Dependencies\FullSerializer\fsSerializer.cs:628 
  at Ludiq.Serialization.DeserializeJson (Ludiq.FullSerializer.fsSerializer serializer, System.String json, System.Object& instance, Boolean forceReflected) [0x0000a] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Serialization\Serialization.cs:158 
  at Ludiq.Serialization.DeserializeInto (SerializationData data, System.Object& instance, Boolean forceReflected) [0x0002b] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Serialization\Serialization.cs:113 
  --- End of inner exception stack trace ---
  at Ludiq.Serialization.DeserializeInto (SerializationData data, System.Object& instance, Boolean forceReflected) [0x00059] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Serialization\Serialization.cs:119 
  at Ludiq.LudiqBehaviour.UnityEngine.ISerializationCallbackReceiver.OnAfterDeserialize () [0x00010] in C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Unity\LudiqBehaviour.cs:46 
UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object, Object)
UnityEngine.Debug:LogError(Object, Object)
Ludiq.LudiqBehaviour:UnityEngine.ISerializationCallbackReceiver.OnAfterDeserialize() (at C:\Users\Lazlo\Projects\Ludiq\Ludiq.Core\Runtime\Unity\LudiqBehaviour.cs:53)

We've tried a number of things to resolve this, but at this point, thought it would be better to reach out and see what you recommend before deciding what to do.  Thanks in advance or the help, we've been really impressed with Bolt thus far.

Best,

John

Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):
GOOD, I'M SATISFIED
Satisfaction mark by John Karczewski 4 years ago
Pending Review

Hi John,

Do you have multiple Newtonsoft.Json.dll in your project?

If so, try deleting one of them.

Nope, just the version that comes with bolt in the Assemblies folder.

On install, it comes flagged as Editor only in the import settings.  Toggling the other platforms on, such as Standalone, causes the serialization error I mentioned when built out.  Setting it back to Editor only and building, removes the error and the scripting works as expected.


Ah, that would explain it!

There's a reason why it's flagged as Editor Only. The version of Newtonsoft.Json that ships with Bolt is not compatible with builds. According to ILSpy, it has these dependencies:

If any of those is not available in the build, the Newtonsoft assembly will fail to load. 

Your other tool that requires Newtonsoft must have come with a matching DLL, right? If so, enable this one while in Standalone, not the Bolt one. 

Ah, interesting - good to know!

I'm pretty sure I've already tried using the newtonsoft.dll flagged for build platforms, and left the bolt one set to editor only. However, I will try again and confirm.

Thanks for the help!

+1

It turns out that the tool's version of Newtonsoft also had those same dependencies.  So it would fail to load as well.  I ended up grabbing JSON .NET For Unity assemblies off the Asset Store, used those at runtime instead, and the problem went away.

Thanks again for the help, been really enjoying using Bolt! 

Answered

Glad you got it fixed! :)

+2

Just a heads up on this newtonsoft.json.  This asset https://assetstore.unity.com/packages/tools/video/youtube-video-player-youtube-api-v3-29704 uses or has a dll in it as well.  However the one in that one was a version 8 while Bolt uses a version 9.  Not sure i did this right but all is working well by deleting the version 8 one.


Took me forever to figure out why I was getting a "the imported type is defined multiple times unity" error with no real point to as to where it was coming from. Had to do lots of GoogleFu to figure it out.  Should i run into any issues I will try to do Johns fix.  For now its not broken , so I do not need to fix it :)