Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Best file format to store and pass around data (JSON?)

Discussion in 'Scripting' started by Creslin7, Apr 28, 2020.

  1. Creslin7

    Creslin7

    Joined:
    Dec 3, 2019
    Posts:
    5
    Hi Everyone,

    I'm currently working on a procedural generation algorithm. One thing I have to do is store a possibly lengthy and fairly dynamic (ie randomized) set of parameters to pass around to my different generation classes. Basically, these parameters would specify what kind of area to create. IE, a grassland that is hilly and has two dungeons in, and then the dungeons would have their own sets of parameters, etc. etc. It could probably get pretty long if you consider that an area may have lots of sub areas which in turn have sub areas, and each one of these has their own sets of parameters.

    I was thinking of using JSON for this...but as I'm relatively inexperienced in this area, I just wanted to ask to see if there was a different or more standard way of doing this.

    Thanks so much!
     
    Kokowolo likes this.
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    JSON is absolutely wonderful for this and, as a developer, easily my favorite file format to work with. You can use Unity's built in JsonUtility - it's efficient and easy to get to - as long as your data doesn't contain any Lists or dictionaries. There are two other packages I'd recommend for more robust JSON support:
    1. LitJson is fast and efficient and supports Lists and Dictionaries.
    2. Newtonsoft JSON.NET (available here which can be added via the Unity Package Manager) is the most full-featured package I know of. It supports the above, plus it supports polymorphism by including the class name with the JSON files, and it's also more extensible. But it's a little slower than LitJson (generates more GC for collection), so it's probably only appropriate if all your JSON reading/writing functions are happening during breaks in the game (load screens) and you're not constantly streaming data.

    The good news is that these all work basically the same way for serializing a class, so you can write your code for one and later swap them out and only change a couple lines of code.
     
    Vryken likes this.
  3. Creslin7

    Creslin7

    Joined:
    Dec 3, 2019
    Posts:
    5
    Wow thanks so much, that's really helpful :). Since this stuff is all going to be happening during world generation, I'm not ultra-concerned about performance, so I might go for the middle ground and look at LitJson first.

    Thanks again!!!
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    JSON is fairly simple to understand, and learning it would be valuable.
    There are really only three things you work with in JSON:
    • Objects:
      {}
    • Arrays:
      []
    • Key-value properties:
      key:value
    A JSON document must always start with either a root object block...
    Code (CSharp):
    1. {
    2.  
    3. }
    ...or a root array block:
    Code (CSharp):
    1. [
    2.  
    3. ]
    Objects contain properties, which are separated by commas:
    Code (CSharp):
    1. {
    2.    "name": "John Smith",
    3.    "age": 30,
    4.    "isEmployed": true
    5. }
    Arrays can contain sets of simple values...
    Code (CSharp):
    1. [1, 2, 3, 4, 5]
    2. ["A", "B", "C", "D", "E"]
    3. [true, false, true, false, false]
    4. [1, "B", true, 4, "E"]
    ...but they can also contain a set of objects:
    Code (CSharp):
    1. [
    2.    {
    3.       "name": "John Smith",
    4.       "age": 30,
    5.       "isEmployed": true
    6.    },
    7.    {
    8.       "name": "Fred Smith",
    9.       "age": 72,
    10.       "isEmployed": false
    11.    },
    12.    {
    13.       "name": "Lisa Smith",
    14.       "age": 23,
    15.       "isEmployed": true
    16.    }
    17. ]
    Properties can also have objects or arrays as values:
    Code (CSharp):
    1. [
    2.    {
    3.       "name": "John Smith",
    4.       "age": 30,
    5.       "isEmployed": true,
    6.       "pets": {
    7.          "name": "Fuzzy",
    8.          "species": "Cat"
    9.       }
    10.    },
    11.    {
    12.       "name": "Fred Smith",
    13.       "age": 72,
    14.       "isEmployed": false,
    15.       "pets": [
    16.          {
    17.             "name": "Chirpy",
    18.             "species": "Bird"
    19.          },
    20.          {
    21.             "name": "Lucky",
    22.             "species": "Dog"
    23.          }
    24.       ]
    25.    },
    26.    {
    27.       "name": "Lisa Smith",
    28.       "age": 23,
    29.       "isEmployed": true,
    30.    }
    31. ]
     
    Kokowolo likes this.
  5. Creslin7

    Creslin7

    Joined:
    Dec 3, 2019
    Posts:
    5
    Thanks so much!!