Skip to content
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

Problem deserializing JSON object as a Dictionary #15

Closed
glenwong opened this issue Feb 10, 2015 · 1 comment
Closed

Problem deserializing JSON object as a Dictionary #15

glenwong opened this issue Feb 10, 2015 · 1 comment

Comments

@glenwong
Copy link

It seems fullserializer does not serialize JSON dictionaries? Running the code below I get the following RawMessage: fsIEnumerableConverter expected Array but got Object in {"someKey":"someValue"}

public class SomeObject {
  public Dictionary<string, string> someField;
}

fsData data = fsJsonParser.Parse("{\"someField\": {\"someKey\": \"someValue\"}}");
object deserialized = null;
var serializer = new fsSerializer ();
serializer.TryDeserialize(data, typeof(someObject), ref deserialized).AssertSuccessWithoutWarnings();
@jacobdufault
Copy link
Owner

The JSON dictionary in your example is available in the fsData data instance - the actual dictionary content is available as data.AsDictionary.

I'd recommend taking a look at the serializer output for some examples to see how the serialization data is encoded into JSON. Generally though, serializing dictionaries as JSON objects is a bad idea w.r.t. robustness because it requires that keys be serializable as strings, which is frequently not the case (for example, with Dictionary<ComplexKey, ComplexValue>).

For your example, the expected JSON data is the following:

{
    "someField": [
        {
            "Key": {
                "$content": "someKey",
                "$type": "System.String"
            },
            "Value": {
                "$content": "someValue",
                "$type": "System.String"
            }
        }
    ]
}

The $type fields are there due to cross-platform compatibility issues w.r.t. generics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants