-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deserialization in Unity using Converters #43
Comments
Hi @zory! Thanks for reporting this Though I'm having troubles understanding the full picture. Will try to reproduce it myself and hopefully find some issue. Could you post the full stacktrace of the error? |
This is full stack trace. One of my structures GenericDictionary have its own ISerializationCallbackReceiver implementation. I will investigate, maybe it is breaking things.
|
Well something I can tell you right now is that the It can be a future feature, but it's not available right now |
I tried removing everything, leaving just simple types, so it doesn't rely on |
Huh I haven't actually tested the converters to be used as Dictionary keys before. Have not researched how Newtonsoft.Json handles those situations. All I remember though is that it has special built-in cases for As a workaround you maybe could use the type |
Yes, your workaround works. Serializing for example List<KeyValuePair<Vector3Int, int>> yields expected results. Thank you for letting me use your brain : ). If you would come up with another solution, let me know : ). |
Hey, I just ran into a similar issue, though your solution does not seem to work for me. This: public IEnumerable<KeyValuePair<Vector3Int, string>> EntitiesSerialized { get; set; } = new Dictionary<Vector3Int, string>(); is serialized into this: "EntitiesSerialized": {
"(4, 14, 0)": "PassiveEnemyShip",
"(3, 11, 0)": "PassiveEnemyShip",
"(1, 9, 0)": "PassiveEnemyShip"
} Which JsonConvert refuses to deserialize. Any thoughts? I even tried adding the Vector3IntConverter manually to the serialize and deserialize calls. |
Hi @MrFJ ! You're using a complex type as a dictionary key. That doesn't work in JSON, and Newtonsoft.JSON's solution for when doing this seems to just be to You do save the type as Maybe you could try use an explicit list instead? public List<KeyValuePair<Vector3Int, string>> EntitiesSerialized { get; set; } = new Dictionary<Vector3Int, string>().ToList(); |
Thanks for the quick response! I changed my implementation to: public Dictionary<Vector3Int, string> Entities { get; set; } = new ();
public IEnumerable<KeyValuePair<Vector3Int, string>> EntitiesSerialized {
get => Entities;
set => Entities = (Dictionary<Vector3Int, string>)value;
} to no avail, but it turns out ToString() was indeed the missing piece! This works perfectly: [JsonIgnore]
public Dictionary<Vector3Int, string> Entities { get; set; } = new ();
public IEnumerable<KeyValuePair<Vector3Int, string>> EntitiesSerialized {
get => Entities.ToList();
set => Entities = (Dictionary<Vector3Int, string>)value;
} |
Great! Happy you found a solution :) |
It seems I spoke too soon. This is my JSON: "EntitiesSerialized": [
{
"Key": {
"x": 6,
"y": 12,
"z": 0
},
"Value": "PassiveEnemyShip"
},
{
"Key": {
"x": 6,
"y": 11,
"z": 0
},
"Value": "PassiveEnemyShip"
},
{
"Key": {
"x": 6,
"y": 8,
"z": 0
},
"Value": "PassiveEnemyShip"
}
] The dictionary is empty after deserialization though :/ I tried changing IEnumerable to List, I even tried the following to no results: private IEnumerable<KeyValuePair<Vector3Int, string>> entities;
public IEnumerable<KeyValuePair<Vector3Int, string>> EntitiesSerialized {
get => Entities.ToList();
set => entities = value;
} I'm getting nada :( |
It's getting complicated with the types here. To simplify, you could have it more explicit. Such as by using one class for serializing/deserializing, and one that you're working with otherwise. Such pattern is usually called a DTO (Data-Transfer-Object). So that before you serialize it, you always convert the dictionary into a Example: |
Hello,
Probably I'm stuck in obvious case, because I can't find any documentation on this.. Maybe you could provide some example?
I'm trying to serialize/deserialize objects which contains List
I'm getting
ArgumentException: Could not cast or convert from System.String to UnityEngine.Vector3Int.
My code:
Thanks in advance.
The text was updated successfully, but these errors were encountered: