question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dictionary conversion for complex key-types is really buggy, defaults to ToString() output

See original GitHub issue

Source/destination types

using ActionRewards = System.Tuple<float, float>;
using QTable = System.Collections.Generic.Dictionary<Vector2, System.Tuple<float, float>>;

[JsonObject]
public struct Vector2
{
    public Vector2(float x, float y) { this.x = x; this.y = y; }

    [JsonProperty]
    public float x;

    [JsonProperty]
    public float y;

    public override string ToString()
    {
        return $"({ x }, { y })";
    }
}

Source/destination JSON

// expected output
{{"x":0.7,"y":0.3}:{"Item1":0.5,"Item2":0.6}}

// actual output
{"(0.7, 0.3)":{"Item1":0.5,"Item2":0.6}}

Expected behavior

I expected the dictionary’s key to be serialized using Newtonsoft.Json class annotations or just the standard behavior of the serializer for a struct object (which even works fine without annotations). Btw, converting the struct to a class didn’t help either and caused to same behavior.

Actual behavior

From the output of my little test program you can see that the dictionary’s key was serialized using the output of ToString().

Unhandled exception. Newtonsoft.Json.JsonSerializationException: Could not convert string ‘(0.7, 0.3)’ to dictionary key type ‘Vector2’. Create a TypeConverter to convert from the string to the key type object. Path ‘[’(0.7, 0.3)‘]’, line 1, position 14. —> Newtonsoft.Json.JsonSerializationException: Error converting value “(0.7, 0.3)” to type ‘Vector2’. Path ‘[’(0.7, 0.3)‘]’, line 1, position 14. —> System.ArgumentException: Could not cast or convert from System.String to Vector2. at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType) at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) — End of inner exception stack trace — at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary(IDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, JsonProperty containerProperty, String id) — End of inner exception stack trace — at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary(IDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, JsonProperty containerProperty, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) at dotnet_test.Program.Main(String[] args) in /home/rl-dev2/dotnet-test/Program.cs:line 46

Steps to reproduce

public class Program
{
    public static void Main(string[] args)
    {
        string vectorContent = "{ \"x\": 0.7, \"y\": 0.3 }";

        // test single Vector2 serialization
        var vector = JsonConvert.DeserializeObject<Vector2>(vectorContent);
        Console.WriteLine($"{ vector }");
        string serVectorContent = JsonConvert.SerializeObject(vector);
        Console.WriteLine($"serialized json: {serVectorContent }");
        var reserializedVector = JsonConvert.DeserializeObject<Vector2>(serVectorContent);
        Console.WriteLine($"{ reserializedVector }");

        // test Vector2 serialization as dictionary key
        var dict = new QTable() {
            { new Vector2(0.7f, 0.3f), new ActionRewards(0.5f, 0.6f) }
        };
        string dictJson = JsonConvert.SerializeObject(dict); // serializes the key using ToString() function output
        Console.WriteLine($"{ dictJson }");
        var serDict = JsonConvert.DeserializeObject<QTable>(dictJson); // throws a conversion error
        Console.WriteLine($"{ serDict }");
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Bonifatius94commented, Dec 18, 2020

I did resolve my issue with a workaround converting the 2D vector into a single number by multiplying it with the screen size. If I find the time I could also try your suggested solution, but it’s only a small student project, so I’m not sure if the effort is worth it. But anyways … Thanks a lot for your help, Tyler 😃

0reactions
JamesNKcommented, Dec 20, 2020

JsonConverter isn’t used for dictionary keys because they’re strings, not JSON. That is why ToString or a TypeConverter is used.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Incorrect python dictionary to JSON conversion
Looking at your target string, the spacing seems non-standard to me, so I would guess that it would not match the output of...
Read more >
Convert String Dictionary to Dictionary Python
Use the 'json.loads()' function to convert the dictionary string to a dictionary object and assign it to the variable 'res'.
Read more >
Exploring C++
Input and output conversion are controlled by the declared type of the I/O variables: you write the same thing to output a double...
Read more >
Object.prototype.toString() - JavaScript - MDN Web Docs
The toString() method of Object instances returns a string representing ... so that your custom object can be converted to a string value....
Read more >
3 Ways to Convert String to Dictionary in Python
1) Using json.loads() ... You can easily convert python string to the dictionary by using the inbuilt function of loads of json library...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found