• 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
Question by Krzysztof-P · Feb 17, 2019 at 02:28 PM · serializationjson

Newtonsoft.Json Dictionary serialization problem

Hello!
I'm trying to force Newtonsoft.Json librabry to serialize/deserialize my custom class w$$anonymous$$ch contains a Dictionary < Vector2, Object >. However it serializes it in format that cannot be deserialized back and returns an exception: ArgumentException: Could not cast or convert from System.String to UnityEngine.Vector2. I believe it should be possible because librabry handles UnityEngine.Vector2 fine as well as more standard Dictionaries like < string , string > for example.

Small example to be more precise:
When I serialize UnityEngine.Vector2 object it results in string:
"Vector2VariableName": { "x": 7.0, "y": 7.0 }

However when I serialize a Dictionary it results with different, incorrect format: "DictionaryName": { "(-9.0, -9.0)": { "StringVariableName": "stringVariableValue" },


Could anyone give me a $$anonymous$$nt how to properly serialize and deserialize Dictionary < Vector2, Object > ?

Comment
starpxy

People who like this

1 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 zereda-games · Feb 18, 2019 at 09:54 AM 0
Share

https://dzone.com/articles/how-to-serializedeserialize-a-dictionary-object-in-1

When the Serialize method is called on a formatter, object serialization proceeds according to the following sequence of rules:

  1. A check is made to determine whether the formatter has a surrogate selector. If the formatter does, check whether the surrogate selector handles objects of the given type. If the selector handles the object type, ISerializable.GetObjectData is called on the surrogate selector.

  2. If there is no surrogate selector or if it does not handle the object type, a check is made to determine whether the object is marked with the Serializable attribute. If the object is not, a SerializationException is thrown.

  3. If the object is marked appropriately, check whether the object implements the ISerializable interface. If the object does, GetObjectData is called on the object.

  4. If the object does not implement ISerializable, the default serialization policy is used, serializing all fields not marked as NonSerialized.

https://docs.microsoft.com/en-us/dotnet/standard/serialization/steps-in-the-serialization-process

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Krzysztof-P · Feb 17, 2019 at 09:15 PM

Well, I'm afraid it's not the wrong type issue. Let me demonstrate it on a simple example:

alt text


As you can see I'm only creating Dictionary, adding two example values and then serializing it an deserializing it back. testDictionary variable is filled properly. I do have some serialized string in temp variable. However execution of line 220 throws an exceprion:

alt text


Any other ideas ? Did anyone have similar problem ? Thanks in advance!


debugging.png (28.5 kB)
exception.png (8.3 kB)
Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

105 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 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

Why is JsonUtility.ToJson turning all my floats into doubles? 2 Answers

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

JsonUtility and Arrays [Error - "JSON must represent an object type"] 2 Answers

How do I go about deserializing a json array? 3 Answers

What are the pros and cons of ScriptableObjects vs. JSON for data files? 2 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