Search Unity

Objetcs hierarchy with JSON files

Discussion in 'Scripting' started by danielesuppo, Sep 4, 2021.

  1. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    231
    Ok, I have to admit, I've a problem with JSON files, and I'm working with JS from a long time so maybe I'm a bit confused
    I shoud create a simple JSON file like that:

    Code (CSharp):
    1. {
    2.   videoFolder: "empty",
    3.   email: {
    4.       from: "empty",
    5.       to: "empty",
    6.   }
    7. }
    So, I've created 2 classes:

    Code (CSharp):
    1. public class MainData {
    2.     public string videoFolder;
    3.     public Email email;
    4. }
    5.  
    6. public class Email
    7. {
    8.     public string from;
    9.     public string to;
    10. }
    Now I should create the JSON file, so:

    Code (CSharp):
    1. public class SaveData : MonoBehaviour
    2. {
    3.     public void SaveIntoJson()
    4.     {
    5.         MainData mainData = new MainData();
    6.      
    7.         mainData.videoFolder = "empty";
    8.         mainData.email = new Email();
    9.         mainData.email.from = "empty";
    10.         mainData.email.to = "empty";
    11.      
    12.         string configFile = JsonUtility.ToJson(mainData, true);
    13.         System.IO.File.WriteAllText(Application.dataPath + "/config.json", configFile);
    14.     }
    15. }
    In Unity I obviously can get all "data" from mainData.email.from, and mainData.email.to,
    but these values are not written in the JSON file, so what I get is that:

    Code (CSharp):
    1. {
    2.   videoFolder: "empty",
    3. }
    What I'm missing?
    Many thanks!
     
  2. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    231
    Solved
    I've forgot System.Serializable
     
    orionsyndrome likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    1,187
    It would help others if you add some more details what you actually did ^^. Specifically your two classes are not marked as Serializable.
     
  4. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    231
    Sure! Quite easy:

    Code (CSharp):
    1.  
    2.  
    3. public static class Globals
    4. {
    5.     [Serializable]
    6.     public class MainData
    7.     {
    8.         public string videoFolder;
    9.         public Email email = new Email();
    10.     }
    11.  
    12.  
    13.     [Serializable]
    14.     public class Email
    15.     {
    16.         public string from;
    17.         public string to;
    18.     }
    19. }
    20.  
    21.  
    22. public class SaveData : MonoBehaviour
    23. {
    24.     public void SaveIntoJson()
    25.     {
    26.         string configFileName = "config.json";
    27.         Globals.MainData mainData = new Globals.MainData();
    28.         string configFile = JsonUtility.ToJson(mainData, true);
    29.         System.IO.File.WriteAllText(Application.persistentDataPath + "/" + configFileName, configFile);
    30.     }
    31. }
    32.  
     
    Bunny83 likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    20,335
    The key point you're missing is that Unity JSON is SUPER limited. It's like ultra light Fisher Price kindergarten JSON.

    Problems with Unity "tiny lite" built-in JSON:

    In general I highly suggest staying away from Unity's JSON "tiny lite" package. It's really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

    Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window -> Package Manager).

    https://forum.unity.com/threads/jso...-not-working-as-expected.722783/#post-4824743

    https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

    Also, always be sure to leverage sites like:

    https://jsonlint.com
    https://json2csharp.com
    https://csharp2json.io

    PS: for folks howling about how NewtonSoft JSON .NET will "add too much size" to your game, JSON .NET is like 307k in size, and it has the important advantage that it actually works the way you expect a JSON serializer to work in the year 2021.
     
    danielesuppo likes this.
  6. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    231
    Many thanks Kurt-Dekker, for sure I'll give a look to the package and links that you've suggested.
    For this specific project I don't have too big requirements, so for my needs this simple solution is enough.
    With JS structured data are a lot more easy to manage, but this is another story (and that's why sometimes,
    moving back and forth from JS and Unity C# can become quite hard ;))
     
    Kurt-Dekker likes this.
  7. danielesuppo

    danielesuppo

    Joined:
    Oct 20, 2015
    Posts:
    231
    Kurt-Dekker likes this.
unityunity