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

Dictionaries are not serialized properly #80

Open
ygoe opened this issue Feb 1, 2018 · 0 comments
Open

Dictionaries are not serialized properly #80

ygoe opened this issue Feb 1, 2018 · 0 comments

Comments

@ygoe
Copy link

ygoe commented Feb 1, 2018

Deserialize this JSON

{
  "profiles": {
    "production": {
      "host": "localhost",
      "username": "root"
    }
  }
}

into this class

class File
{
    public Dictionary<string, Profile> profiles { get; set; }
}
class Profile
{
    public string host { get; set; }
    public string username { get; set; }
}

and you get a dictionary with one key of "production" and its value containing "localhost" and "root".

Serialize it back to JSON and (after some formatting, see #79) you get

{
  "profiles": [
    {
      "Key": "production",
      "Value": {
        "host": "localhost",
        "username": "root"
      }
    }
  ]
}

Seems like SimpleJson didn't recognise the Dictionary and treated it as an IEnumerable<KeyValuePair<string, Profile>> instead, serializing its properties "Key" and "Value".

The problem lies in line 1015 in the method SimpleJson.SerializeValue:

IDictionary<string, object> dict = value as IDictionary<string, object>;

That won't find dictionaries of any other value type than object, which is a useless type because you can't do anything with it. In my case, that's Profile instead of object.

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

1 participant