I’ve been trying to use JSON.NET library for de/serializing JSON in Unity. I use it because it says it works with Mono:
But, as soon as I make the simplest call to library (see image 1), Unity IDE crashes with no visible exception in alert box (image 2), but log says:
GLib: Cannot convert message: Conversion from character set 'UTF-8' to 'CP1250' is not supported
** ERROR **: file ..\..\mono\metadata\object.c: line 1940: assertion failed: (res)
aborting...
I’m interested, did someone use this library in Unity and what may be the problem?
Regarding this post, I’ve tried JSONsharp, LitJSON and Jayrock, but I don’t like the fact that they cannot deserialize generic lists.
Yap, this library works! Many thanks for the tip!!
I’ve serialized an object on the server and successfully deserialized it in Unity using JsonFx (see the code below).
The object contains a generic collection of other objects, so I it’s a pretty complex structure.
I tested the web player version on both PC and Mac and it works.
Server:
ProductList list = new ProductList();
list.Products.Add(new Product("001", "Boulder Dash"));
list.Products.Add(new Product("002", "Knight Lore"));
list.Products.Add(new Product("003", "Head Over Heels"));
list.Products.Add(new Product("004", "Barbarian"));
string json = JsonFx.Json.JsonWriter.Serialize(list);
Do you happen to know if JsonFX works in Unity 3.0 web player build? I was using Json.NET in Unity 2.6.1 without problems, but I’ve run into issues with it in the Webplayer builds for Unity 3.0. (The strange thing is that it works fine in the Editor Web Emulation mode which I thought had the same framework restrictions as the web player build.)
Json.NET is known to not work cause they access framework a json library should not even touch
either jsonfx as well as litjson both should work, did at least in beta. Also both come with sources so you can clean them from offending framework missuses
Mind being a bit more explicit? Json.NET is considered to be the one to use for pretty much every other C# project. I just went through it and started from the .NET 2 version, yet there are still features that make Unity throw a hissy fit without telling me whats wrong with it. (PS i’m talking about the webplayer here).
Hey duke, I am currently using jayrock but want to do a web player build. Any speculation on how hard it would be to convert from jayrock to your package above? The reason is I have already switched once and I was hoping I was done with that part of the project but apparently not if I want to deploy to a Unity webplayer.
iByte
Update:
Okay - wow that was easy.
Dropped the DDL into Unity project
Replaced the using Jayrock.Json.Conversion; statement with using Newtonsoft.Json;
Replaced JsonConvert.Import(jsondata); with JsonConvert.DeserializeObject(jsondata);
I had a couple of property name overrides that I replaced with JsonPropertyAttribute’s.
All done and working - sweet!! Now if I can get past this crossdomain policy error I am getting … I actually be might be there…
Hmm spoke too soon - getting an error when trying to run from a real iDevice. Works fine from Web, MacIntel and Win64.
ExecutionEngineException: [B]Attempting to JIT compile method[/B] 'System.Collections.Generic.GenericEqualityComparer`1<Newtonsoft.Json.Serialization.ResolverContractKey>:.ctor ()' while running with --aot-only.
at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0
at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in <filename unknown>:0
at System.Activator.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0
at System.Collections.Generic.EqualityComparer`1[Newtonsoft.Json.Serialization.ResolverContractKey]..cctor () [0x00000] in <filename unknown>:0
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for System.Collections.Generic.EqualityComparer`1
at System.Collections.Generic.Dictionary`2[Newtonsoft.Json.Serialization.ResolverContractKey,Newtonsoft.Json.Serialization.JsonContract].Init (Int32 capacity, IEqualityComparer`1 hcp) [0x00000] in <filename unknown>:0
at System.Collections.Generic.Dictionary`2[Newtonsoft.Json.Serialization.ResolverContractKey,Newtonsoft.Json.Serialization.JsonContract]..ctor () [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract (System.Type type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContractSafe (System.Type type) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType (Newtonsoft.Json.JsonReader reader, System.Type t, Newtonsoft.Json.JsonConverter propertyConverter) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonConvert.DeserializeObject[MediaFeed] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <filename unknown>:0
at Newtonsoft.Json.JsonConvert.DeserializeObject[MediaFeed] (System.String value) [0x00000] in <filename unknown>:0
at AdvancedListManager+<getFeeds>c__Iterator1.MoveNext () [0x00000] in <filename unknown>:0
Hello duke,
Could you tell me how to modify Newtonsoft.Json to make it available in the webplayer?
I tried newest 4.5.7 and 4.0.2(same version as you post, but size is different). I can’t find any Newtonsoft.Json available in the Unity webplayer.
I think your post version is a modified version, but I can’t find where it came from…
Gets a +1 for me. Works significantly better and that on all platforms, cause it does not rely on half a dozen other .NET frameworks and bloat the builds