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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Serialization of: List<string[]> as JSON

Discussion in 'Scripting' started by Di_o, Mar 18, 2020.

  1. Di_o

    Di_o

    Joined:
    May 10, 2016
    Posts:
    55
    I'm working on a little tool that lets me connect several GameObjects with a LineRenderer. It's possible to connect multiple objects within one line. To store the Data of every connection, I'm saving the GameObject.name of every point in the connection in an array (string[]). Serializing this array as JSON worked without problems.

    Next step is to store multipe Lines in a list of arrays (List<string[]>) and store it as JSON, so I can deserialize all my Lines at once.

    Here is what I'm doing:

    Code (CSharp):
    1.  
    2. public ConnectionCollectionObject activeCollection; // <- This one is filled by the user by clicking things in the scene
    3.  
    4. public void SaveConnectionCollection()
    5.         {          
    6.  
    7.             if (activeCollection == null) return;        
    8.  
    9.             Debug.Log(JsonUtility.ToJson(activeCollection, true));  // result: {}
    10.  
    11.             string path = Path.Combine(Application.persistentDataPath, id + "_ConnectionCollection.json");
    12.             File.WriteAllText(path, JsonUtility.ToJson(activeCollection, true));
    13.  
    14.             Debug.Log(path + " saved.");
    15.         }
    16.  
    17. /////////
    18.  
    19. [Serializable]
    20.    public class ConnectionCollectionObject
    21.    {
    22.        public List<string[]> connectionCollection;
    23.    }
    24.  
    ...using the same princple like I used when serializing the string[] gives ony an empty .json file.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    JsonUtility has some limitations on things, so it probably doesn't handle it correctly. Try Json.net instead.
     
    Di_o likes this.
  3. Di_o

    Di_o

    Joined:
    May 10, 2016
    Posts:
    55
    Worked thanks!