- Home /
Serializing a dictionary to json using LitJson.
In my game players can customize their characters by changing the characters color and accessory. So I am trying to save this customization data as a Json to a text file. Here is my code: using UnityEngine; using System.Collections.Generic; using System.IO; using System; using LitJson;
public class SnakeCustomizationStorage : IFileStorage{
private static string CUSTOMIZATION_FILENAME = "/snakeCustomization.dat";
private static string FILEPATH = Application.persistentDataPath + CUSTOMIZATION_FILENAME;
private static SnakeCustomizationStorage m_instance;
private static Dictionary<string, CustomizationData> m_customData = new Dictionary<string, CustomizationData>();
private static JsonData m_jsonData = new JsonData();
public static SnakeCustomizationStorage Instance {
get {
if(m_instance == null) {
m_instance = new SnakeCustomizationStorage();
Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
}
return m_instance;
}
}
#region IFileStorage implementation
public void load()
{
if(File.Exists(FILEPATH)) {
string fileText = File.ReadAllText(FILEPATH);
m_customData = JsonMapper.ToObject<Dictionary<string, CustomizationData>>(fileText);
} else {
List<SnakeMysteryReward> unlockedSnakes = RewardManager.Instance.getGrantedSnakeRewards();
for(int i = 0; i < unlockedSnakes.Count; i++) {
string name = unlockedSnakes[i].productName;
CustomizationData data = new CustomizationData(GreedyConstants.DEFAULT_SNAKE_GREEN, "");
m_customData.Add(name, data);
}
save();
}
}
public void save()
{
JsonData json = JsonMapper.ToJson(m_customData);
File.WriteAllText(FILEPATH, json.ToString());
}
public string getFilePath()
{
return FILEPATH;
}
#endregion
}
[System.Serializable]
class CustomizationData {
public float [] color;
public string accessory;
public CustomizationData(Color snakeColor, string accessoryName) {
color = new float[] {
snakeColor.r,
snakeColor.g,
snakeColor.b,
snakeColor.a
};
accessory = accessoryName;
}
}
No matter what I try, I am not able to figure out how to get the dictionary serialized into the Json string. The code to convert it does not seem to work. What am I doing wrong? The format of Json I am looking to generate is: { "Name1" : {"accessory":"hat1", "color":[108, 0, 234, 1]}, "Name2" : {"accessory":"hat2", "color":[108, 0, 234, 1]} }
Basically key value pairs of the character name and the customization data. Any help is appreciated thanks.
when I create a file with the contents I am looking to output, it is converted back to a dictionary correctly. But when I try to actually create the json text file with the dictionary contents it gives me error saying JsonException: $$anonymous$$ax allowed object depth reached while trying to export from type System.Single.
Answer by hulahoolgames · Mar 28, 2016 at 04:22 PM
Couldn't find a way to fix this problem, so opted SimpleJSON thanks to @Bunny83. Much simpler to use and easy to understand.
Your answer
![](https://koobas.hobune.stream/wayback/20220612073513im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Json file not found after the Build... Help 1 Answer
Accesing StreamingsAssets in android (LitJson) 1 Answer
How to implement LitJSON? 2 Answers
jSON data error Unity3d 0 Answers
LitJSON android problem 1 Answer