Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by hulahoolgames · Mar 27, 2016 at 09:09 PM · jsonlitjson

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hulahoolgames · Mar 27, 2016 at 09:30 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges