Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Help with JsonUtility.FromJson

Discussion in 'Scripting' started by acandael, May 25, 2022.

  1. acandael

    acandael

    Joined:
    Apr 7, 2021
    Posts:
    74
    Hello,

    Is it possible to use JsonUtility.FromJson to parse a JSON file that has some of its attributes consisting of 2 words?

    For instance, I'd like to retrieve the "Element Category" from this JSON extract:
    upload_2022-5-25_10-45-17.png

    I can do it for single word attributes like "Level", but don't know how to do it for 2-words attributes.

    Thanks a lot,
    Arnaud
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,912
    In short: No. Most object mappers would have issues with such keys as they can not be represented as a field or property name directly. Some parsers support attributes like the JsonProperty attribute which can be used to indicate the key name of a certain field inside your class. So you would have to use a proper variable name inside your class but you can specifiy a different key name that is used for json serialization / deserialization. Unity's JsonUtility does not support this at all. So you would have to use something like the Newtonsoft Json.NET library.

    Another option is to don't use an object mapper at all and just let the data being parsed into a Dictionary structure. Though this also can not be done by Unity's JsonUtility. For example I made this json library which is not an object mapper and uses Dictionaries / Lists internally to represent the json data on the C# side.
     
    acandael likes this.
  3. acandael

    acandael

    Joined:
    Apr 7, 2021
    Posts:
    74
    Hello!
    Thank you for your answer!

    As a matter of fact, I have been using your JSON library, and it's great! :)
    The reason I wanted to switch to this one is because I thought it would be somehow faster to do what I want (but I have no technical expertise at all to back up this assumption...).

    Here's my case: I'm loading a gltf file, which is basically a geometry consisting of a JSON file + a binary file, and which can be pretty heavy. In my case it's a building. Once it's loaded I want to do a mapping between the geometries (i.e. lots of GameObjects) and their properties (listed in the gltf JSON). And to do that I'm just having a nested loop of both all the GameObjects and all the JSONObject... (if GameObject's name == JSONObject["name"] basically) which takes time.

    I somehow thought the iteration would be faster if I was doing this via Unity's JsonUtility instead of simpleJSON, but again, I have no clue.

    I know this is a bit out of topic of my original question, but would you have recommendations to perform such mapping fast? We can assume 1000 - 2000 GameObjects and as many JSON nodes..

    Thanks a lot again!
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,912
    Well, I doubt that the name lookup would be the bottleneck here. SimplyJSON uses an ordinary Dictionary internally which has a quite fast lookup. Sure, if you have the data in an actual class, the lookup would be faster, however you would probably loose this speed at the parsing of the json data since no matter how you look at it, you have to do the character by character matching everytime at least once for every object. 1000 - 10000 objects does not really sound much in terms of json data. However the slowest part would be to create that many GameObjects.

    Apart from that, once you've loaded in the json data you can always copy / extract relevant data into other structures if you need to. This may be necessary if data that belongs together is actually distributed in the json file.

    So you seem to have an explicit issue you try to solve but you haven't included any specifics about that issue. What have you actually done to debug / profile your issue? What does the profiler tell you what parts take how much time? How often is a certain lookup performed?

    Note that reading the name of a gameobject is slow as, just with the tag, it's actually stored on the native C++ side and when you read those properties in C#, Unity actually has to allocate managed memory in order to return the name / tag as a string. Maybe that's your issue?

    I once made a simple circuit simulation which allowed creating "integrated circuits" and reuse them. I stored everything with my json library. Saving and loading only took a fraction of a second and I actually had a lot cross referencing going on in post deserialization as there were actual circular references between objects which can not be represented in json.

    Yes, my json library has some memory overhead compared to actual C# class instances and I wouldn't necessarily use it to hold the actual runtime data if I have like a million objects. Though that shouldn't matter when it comes to serializing / deserializing data.