Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Dictionary support (or workaround) for JsonUtility (test case included)

Discussion in '5.4 Beta' started by bdovaz, Jan 13, 2016.

  1. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,053
    For example, is there any way to parse this structure? (NOTE: Title field being dynamic and not a 3 fixed value structure)

    Code (csharp):
    1.  
    2. {
    3.     "id": 1,
    4.     "title": {
    5.         "es": "1",
    6.         "en": "2",
    7.         "fr": "3"
    8.     }
    9. }
    Code (csharp):
    1.  
    2. public class Test {
    3.     public int id;
    4.     public Dictionary<string, string> title;
    5. }
    6.  
    https://feedback.unity3d.com/suggestions/support-conversion-of-json-string-into-a-dictionary
     
  2. Alex-Lian

    Alex-Lian

    Guest

    First off, not particularly relevant to 5.4 beta, but I'll still answer.

    From our docs page: http://docs.unity3d.com/Manual/JSONSerialization.html

    and later on

    Trade-offs were made for speed, less allocations, and also clean integration with our serialization system. There's other work we have to do first to get dictionary support in.
     
    MGGDev likes this.
  3. Cripple

    Cripple

    Joined:
    Aug 16, 2012
    Posts:
    92
    You should approach FullInspector/FullSerializer developer. This guy has made so much work on Unity serialization and he is able to serialiaze pretty much everything in json (dictionary, interface, inheritance, generics ...).

    Unity must hire this guy and give him the chance to improve the core engine :)
     
  4. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    Like it was said above and was said elsewhere they want to first have as efficient and light weighted support they can at start and not to bloat it with every feature a man can think of. It's not really lack of skills or anything. There are already a lot of full feature set libs out there for free and paid.
     
  5. bdominguezvw

    bdominguezvw

    Joined:
    Dec 4, 2013
    Posts:
    96
    We know that but many many people will use it to consume third parties web services where you can't have control of the JSON output and it's really common to have "Dictionary" like random structures.

    So right now, Unity JSONUtility is useless.
     
    _Radagan likes this.
  6. Novack

    Novack

    Joined:
    Oct 28, 2009
    Posts:
    844
    Im afraid my line of thinking goes in the same direction. JsonUtility seems like a mere anecdotic class. Currently the feature set can only work with an object. From and to Json, only supports an object.

    If you have a Json string with an array of objects, wont work. Let's not even mention nested arrays. And json is basically keys value pairs, but deserializing to a dictionary seems to be asking for "slow bloatware".

    I was exited about replacing third party dependencies, but resulted tough to find that my very basic needs, deserializing an array of objects, was too much to handle for the Unity API.
     
    Last edited: Apr 9, 2016
  7. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    I highly recommend fastJSON: https://fastjson.codeplex.com/

    Open source, very lightweight, superfast, can handle everything you throw at it, comes with a .NET 2.0-specific version that works with Unity.
     
  8. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,053
    Yes and there is also Json .NET on Asset Store but I want to use the Unity implementation so I don't depend on third party libraries.
     
    Novack likes this.
  9. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    Good luck with that :D

    Everything in Unity is made to be extended and improved. It's part of what makes it shines. Trust me, you don't want to depend entirely on one company to create your game, especially Unity. We have fast turnarounds when it comes to fixing bugs with store packages, but with Unity, fixing one big bug can take several months.
     
    Erethan likes this.