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 pablotunon · Mar 13, 2018 at 12:52 AM · serializationjsondictionary

Serialize / Deserialize dictionary with unity serialization system, without using lists for keys and values

Hello,

The following class:

 [System.Serializable]
 public class Profile
 {
     public string nick;
     public Dictionary<string, long> highScores; // <mapid, score>
 }

should write, when serialized, jsons with a format like this:

 {
     "nick" : "player1",
     "highScores" : {
         "mapid1" : 1234,
         "mapid2" : 2345,
         "mapidn" : 3456
     }
 }

.

The second answer to this question addresses the issue, but for me it's only a pretty good workaround, as output format is not ideal.

(for reference, this is the output obtained following the solution given):

 {
     "nick" : "player1",
     "highScores" : {
         "keys" : ["mapid1", "mapid2", "mapidn"],
         "values" : [1234, 2345, 3456]
     }
 }

.

I've tried to use dynamic programming to create class members on the go, as explained here, but I'm not able to import the needed packages (perhaps they are not supported in unity).

It would be great if it was possible to override Serialize/ToString and Deserialize/FromString methods. Is this possible? Any other idea?

Thanks in advance!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Mar 13, 2018 at 01:47 AM

This is not possible with Unity's JsonUtility. It's a pure object mapper and only plays by the rules of Unity's serialization system. Therefore it can't serialize Dictionaries at all.


If you just want to ensure this Json format you could use my SimpleJSON framework.


You can create your own Serialize / Deserialize method like this:

 [System.Serializable]
 public class Profile
 {
     public string nick;
     public Dictionary<string, long> highScores; // <mapid, score>
     pubic JSONNode Serialize()
     {
         var n = new JSONObject();
         n["nick"] = nick;
         var h = n["highScores"].AsObject;
         foreach(var v in highScores)
             h[v.Key] = v.Value;
         return n;
     }
     public void Deserialize(JSONNode aNode)
     {
         nick = aNode["nick"];
         highScores = new Dictionary<string, long>();
         foreach(var v in aNode["highScores"])
             highScores[v.Key] = v.Value;
     }
 }

To get your Json text just do

     Profile profile;
     string text = profile.Serialize().ToString(3);

Note that the "3" is the space indention count. If you don't pass an indention count it will create compact json without newline characters.

To deserialize just do something like:

     string text;
     Profile profile = new Profile();
     profile.Deserialize(JSON.Parse(text));

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

79 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 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 avatar image

Related Questions

Deserializing dictionary with 'Json.Net' for Unity returns a null object. 0 Answers

How can I save a Dictionary in a class to JSON? 2 Answers

(SOLVED) Serializing different JSON objects, contained in an array, in another JSON object. (C#) 2 Answers

Saving dictionary from editor mode in play mode 1 Answer

Dictionary in inspector 4 Answers


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