Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

[FREE] Json.NET Converters - Simple compatible solution

Discussion in 'Assets and Asset Store' started by Wanzyee, Mar 5, 2017.

  1. Wanzyee

    Wanzyee

    Joined:
    Jul 24, 2015
    Posts:
    135



    I just wanna use Json.NET in Unity!

    • Json.NET is a powerful, open source, and free serializing solution.
    • But it's too smart to serialize something like Vectoe3 or Color, etc.
    • It even tries to serialize the "normalized" property and throw a loop exception.
    • And it always treats a dictionary key as a string, even if it's an array.

    Overview:
    • This has some common type converters, and sets them as default to make it up..
    • If you're using Json.NET, just import and forget this, then coding like before.
    • Include Json.Net.Unity3D in case you don't have any version of Json.NET.

    The further technical documentation is available here.
    And the tutorial is right below.


    Remarks:
    • Remember to set the "Api Compatibility Lavel" to .NET 2.0 before importing.
    • Thank James Newton-King!
    • Thank SaladLab and Esun Kim!
    • This's released as feedback for free.


    Release:

    http://u3d.as/pXW


    This thread is also for customer support, feel free to leave me a comment if you need.
    Please let me know if there's any question or suggestion.

    I'd also forward important messages between the forum and my site to have them visible.
    Thanks, enjoy!
     
    Last edited: Oct 14, 2018
  2. Wanzyee

    Wanzyee

    Joined:
    Jul 24, 2015
    Posts:
    135
    After a few customer mails, I decide to open this thread for support.
    BTW, the original Json.NET might not be suitable for iOS and WebGL because it uses System.Reflection.Emit.
    Or you may need to download its source code to modify...
     
  3. arnebp

    arnebp

    Joined:
    Oct 15, 2013
    Posts:
    22
    Big thumbs for sharing this! Simple yet useful alternative to the overpriced Json.Net packages in the Asset Store. When the core project is free, so should be the extensions/wrappers.
     
  4. AmirLevi

    AmirLevi

    Joined:
    Dec 5, 2012
    Posts:
    12
    so does it or does it not support ios ?
     
  5. Wanzyee

    Wanzyee

    Joined:
    Jul 24, 2015
    Posts:
    135
    AFAIK, the official Newtonsoft.Json.dll provided by JSON.NET doesn't support iOS for now.
    But you could download it's source code and remove the relatived code, and it'll work.
    Or there's others who compiled the dll for Unity.
     
  6. Wanzyee

    Wanzyee

    Joined:
    Jul 24, 2015
    Posts:
    135
    New version 1.0.3 is currently "Published".

    - Refactoring.
    - Update the included Json.NET to 10.0.3.

    The package is uploaded with Unity 5.3, but it should works in 5.1.
     
  7. Bodkin

    Bodkin

    Joined:
    Jul 12, 2012
    Posts:
    11
    Works on iOS and WebGL. Tested it yesterday :)
     
    Wanzyee likes this.
  8. Bodkin

    Bodkin

    Joined:
    Jul 12, 2012
    Posts:
    11
    Here is an editor update to keep the plugin up to date:
     

    Attached Files:

    Wanzyee likes this.
  9. Wanzyee

    Wanzyee

    Joined:
    Jul 24, 2015
    Posts:
    135
    New version is currently "Published".

    Note, it may be better to remove the old version before you update.
    Since the Json.NET core dll is replaced.
    Or you may use your preferred one and ignore the included.

    1.1
    - Change the Json.NET dll from official to Json.Net.Unity3D for cross platform.
    - Rearrange the Json.Net.Unity3D's asset files to indicate the provider.
    - New class PartialConverter to simplify the code of other converters.
    - Fix potential deserialization fail caused by bytecode stripping for IL2CPP.
    - Fix initializing default converters fail caused by conditional compilation.
    - Make the utility initialize the converters safer.

    I have to say, I'm so excited when I found the "Json.NET for Unity" becomes free!
    I even thought that's the time to deprecate this package.
    Then I found it doesn't handle Dictionary as I need.

    Since I still need these converters, I try to deal with the cross platform problem.
    According to @Bodkin's help, I tried the Json.Net.Unity3D, on Windows, WebGL, Android.
    (I have no iDevice... Oops!)

    Then I decide to directly use it instead of including his helper... LOL
    Wish we all enjoy it!
     
    Last edited: Jan 17, 2018
  10. Wanzyee

    Wanzyee

    Joined:
    Jul 24, 2015
    Posts:
    135
    Hmm... Oooooops... HOTFIX!!
    New version is currently "Published".

    1.1.1
    - Change PartialConverter CanRead as "true" by default, avoid DefaultValueHandling.Populate causing deserializing fail.

    Thank Stefan Landvogt for mailing me, here's the code failed when deserializing Quaternion.

    Code (CSharp):
    1. JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
    2. {
    3.     Converters = WanzyeeStudio.JsonNetUtility.defaultSettings.Converters,
    4.     DefaultValueHandling = DefaultValueHandling.Populate
    5. };
    6.  
    7. //this's fine.
    8. var vec1 = JsonConvert.DeserializeObject<Vector4>("{'x':1.0,'y':0.5, 'z': 0.25, 'w':0.1}");
    9.  
    10. //this returns a default value.
    11. var vec2 = JsonConvert.DeserializeObject<Quaternion>("{'x':1.0,'y':0.5, 'z': 0.25, 'w':0.1}");
    I've found that DefaultValueHandling.Populate will write Quaternion.eulerAngles to zero.
    So I change the default CanRead, let the converters handle both of serializing and deserializing, to avoid Json.NET's default wrong behaviour.

    The attachment package for hotfix is the same one as I submitted today, enjoy.
     

    Attached Files:

    Last edited: Jan 19, 2018
  11. XCVG

    XCVG

    Joined:
    Jun 28, 2017
    Posts:
    7
    What is the reasoning behind the custom Dictionary converter? I actually ran into problems with it because its serialization and deserialization is incompatible with the functionality in Json.NET, and indeed with how I would expect JSON to be formatted. Is it designed to handle certain edge cases?
     
  12. Wanzyee

    Wanzyee

    Joined:
    Jul 24, 2015
    Posts:
    135
    Hi, @XCVG,

    Json.NET serializes a Dictionary key as string, or propaply int okay?
    But I sometimes need a Vector2 or other types as the key.

    For this reason, I can't use the default Dictionary serialization.
    And sure you can remove a specific converter if you don't use it.

    Thanks for your support.
    Wanzyee

    [EDIT] Oops I saw your store review and stars after I reply this, LOL...
     
    Last edited: Apr 2, 2018
  13. Wanzyee

    Wanzyee

    Joined:
    Jul 24, 2015
    Posts:
    135
    New version 1.1.2 is currently "Pblished".

    - Provide assembly definition files, please be aware the script files were moved therefore.
    - Change the namespace of JsonNetUtility to "WanzyeeStudio.Json".

    It's recommended to update only if you downloaded my other packages which include asmdef.
     
    Last edited: Oct 15, 2018
  14. neil_sublime

    neil_sublime

    Joined:
    Jul 9, 2018
    Posts:
    17
    Hi. I've been using the converter in a project since Unity 2018.2, and it's been working perfectly. Thanks for releasing it. :)

    But my team has recently upgraded from 2018.3.5 to 2018.3.7 and suddenly everything's broken. :(

    In edit mode, I frequently get this exception appearing in the console:

    System.TypeInitializationException: The type initializer for 'WanzyeeStudio.Json.JsonNetUtility' threw an exception. ---> System.Reflection.ReflectionTypeLoadException: Exception of type 'System.Reflection.ReflectionTypeLoadException' was thrown.
    at (wrapper managed-to-native) System.Reflection.Assembly.GetTypes(System.Reflection.Assembly,bool)
    at System.Reflection.Assembly.GetTypes () [0x00000] in <d7ac571ca2d04b2f981d0d886fa067cf>:0
    at WanzyeeStudio.Json.JsonNetUtility+<>c.<FindConverterTypes>b__4_0 (System.Reflection.Assembly dll) [0x00000] in WanzyeeStudio\Scripts\Runtime\Concrete\Json\JsonNetUtility.cs:121
    at System.Linq.Enumerable+SelectManySingleSelectorIterator`2[TSource,TResult].MoveNext () [0x00051] in <1b13ba6391c74847bbc3eddc86df7eee>:0
    at System.Linq.Enumerable+WhereEnumerableIterator`1[TSource].ToArray () [0x00033] in <1b13ba6391c74847bbc3eddc86df7eee>:0
    at System.Linq.Buffer`1[TElement]..ctor (System.Collections.Generic.IEnumerable`1[T] source) [0x0000a] in <1b13ba6391c74847bbc3eddc86df7eee>:0
    at System.Linq.OrderedEnumerable`1[TElement].ToArray () [0x00000] in <1b13ba6391c74847bbc3eddc86df7eee>:0
    at System.Linq.Enumerable.ToArray[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x0001f] in <1b13ba6391c74847bbc3eddc86df7eee>:0
    at WanzyeeStudio.Json.JsonNetUtility.FindConverterTypes () [0x00001] in WanzyeeStudio\Scripts\Runtime\Concrete\Json\JsonNetUtility.cs:119
    at WanzyeeStudio.Json.JsonNetUtility.CreateConverters () [0x00001] in WanzyeeStudio\Scripts\Runtime\Concrete\Json\JsonNetUtility.cs:90
    at WanzyeeStudio.Json.JsonNetUtility..cctor () [0x00000] in WanzyeeStudio\Scripts\Runtime\Concrete\Json\JsonNetUtility.cs:67
    --- End of inner exception stack trace ---
    at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
    at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <d7ac571ca2d04b2f981d0d886fa067cf>:0
    UnityEditor.EditorAssemblies:ProcessInitializeOnLoadAttributes()


    And when running the project, I get a whole load of JsonSerializationExceptions when calling into Json.NET.

    I'm hoping there's a simple fix and would be grateful for any assistance.
     
  15. Wanzyee

    Wanzyee

    Joined:
    Jul 24, 2015
    Posts:
    135
    Hi @neil_sublime,
    Very sorry for noticing your post so late.

    I've tried this package in an empty Unity 2018.3.7 project, nothing happens.
    The exception occurs at JsonNetUtility.cs, Line 121, "Assembly.GetTypes()".

    I guess there may be something in your project cause it, e.g., I've ever seen dynamic assemblies cause exceptions when I call some assembly methods.

    The easiest way to fix it as below, and I'll also patch it in this way in the next update.

    1. Open "JsonNetUtility.cs" and find line 121 with the code snippet:
    Code (CSharp):
    1. (dll) => dll.GetTypes()
    2. Replace it to:
    Code (CSharp):
    1. (dll) => {
    2.     try{ return dll.GetTypes(); }
    3.     catch{ return new Type[0]; }
    4. }
    3. Done.

    Thank you.

    [EDIT]
    I'll use the method below to fix it.
    https://haacked.com/archive/2012/07/23/get-all-types-in-an-assembly.aspx/
     
    Last edited: May 19, 2019
    neil_sublime likes this.
  16. neil_sublime

    neil_sublime

    Joined:
    Jul 9, 2018
    Posts:
    17
    Hi @Wanzyee,

    Thanks for the response. There was a problem with loading "accessibility" DLLs via reflection after Unity released 2018.3.7 to hotfix a remote execution flaw. That problem seems to have been fixed for the upcoming 2019.2 release.

    https://issuetracker.unity3d.com/is...get-the-types-from-the-accessibility-assembly

    After I realised that was the issue, I patched the code myself and it's been working flawlessly again ever since. I would certainly appreciate getting an official fix however, and look forward to your update. Thanks again for your hard work in providing this library and maintaining it. :)
     
  17. exoainteractive

    exoainteractive

    Joined:
    Jan 21, 2017
    Posts:
    20
    Hi @Wanzyee,
    Is there any sample code to use it properly ? You talk about a tutorial in the main thread but I can't find it.

    For now this is what I'm trying out :

    Code (CSharp):
    1.         Dictionary<string, object> dic = new Dictionary<string, object>();
    2.         dic.Add("vec", Vector3.one);
    3.         dic.Add("rot", Quaternion.AngleAxis(98f, Vector3.one));
    4.         dic.Add("text", "test");
    5.         dic.Add("enum", TestEnum.No);
    6.  
    7.         string json = JsonConvert.SerializeObject(dic);
    8.         print(json);
    9.         Dictionary<string, object> dic2 = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
    10.         foreach (KeyValuePair<string, object> item in dic2)
    11.         {
    12.             print(item.Key + " " + item.Value.GetType() + " " + item.Value);
    13.        }
    The Vector and Quaternion are now "JObject" type and the Enum is a string.
    How can I have them deserialized properly to Vector3, Quaternion and Enum value ?

    Thanks for your help !
    Anthony

    EDIT: I ended up converting JObject manually like this :
    ((JObject)field.Value).ToObject<Vector3>()

    is the right way to go ?
     
    Last edited: Mar 12, 2020
  18. amynox

    amynox

    Joined:
    Oct 23, 2016
    Posts:
    178
    Hi @Wanzyee ,

    Thank you for this great asset.

    After upgrading "Jobs" package to v0.2.10 (it include Newtonsoft Json v12.0.1.22727) i'm starting gettings this erros :
    Code (CSharp):
    1. JsonReaderException: Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path '', line 1, position 1.
    2. Newtonsoft.Json.Linq.JArray.Load (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Linq.JsonLoadSettings settings) (at <72bc94bec6c44acf84d97f15326490ac>:0)
    3. Newtonsoft.Json.Linq.JArray.Load (Newtonsoft.Json.JsonReader reader) (at <72bc94bec6c44acf84d97f15326490ac>:0)
    4. WanzyeeStudio.Json.DictionaryConverter.ReadJson (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Object existingValue, Newtonsoft.Json.JsonSerializer serializer) (at Assets/WanzyeeStudio/Scripts/Runtime/Concrete/Json/DictionaryConverter.cs:59)
    5. Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable (Newtonsoft.Json.JsonConverter converter, Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Object existingValue) (at <72bc94bec6c44acf84d97f15326490ac>:0)
    6. Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) (at <72bc94bec6c44acf84d97f15326490ac>:0)
    7. Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at <72bc94bec6c44acf84d97f15326490ac>:0)
    8. Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at <72bc94bec6c44acf84d97f15326490ac>:0)
    9. Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) (at <72bc94bec6c44acf84d97f15326490ac>:0)
    10. Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) (at <72bc94bec6c44acf84d97f15326490ac>:0)
    11. Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) (at <72bc94bec6c44acf84d97f15326490ac>:0)
    Any help/fix will be much appreciated.
    Thanks

    i'm using Unity 1019.4.3f1
     
  19. TofyKajo

    TofyKajo

    Joined:
    Mar 22, 2018
    Posts:
    1
    do you fix this ?