facebook-csharp-sdk / simple-json

JSON library for .NET 2.0+/SL4+/WP7/WindowsStore with optional support for dynamic and DataContract
MIT License
382 stars 143 forks source link

Minor bug with ambiguous JSON keys #58

Closed ghost closed 9 years ago

ghost commented 10 years ago

There is a minor bug with simple-json poco deserialization when a JSON key can either hold an object or an array. For an instance of this, see these JSON-RPC specifications regarding the "params" key of their JSON Request object. The specifications state that:

[when passing] by-position: params MUST be an Array... [when passing] by-name: params MUST be an Object...

For this type of JSON schema, I define my poco data class like this:

class JsonMessage {
    public string method { get; set; }
    public object @params { get; set; }
}

I expected the @params property of my JsonMessage instance to contain either a JsonObject or a JsonArray, depending on whichever one was given in the JSON. Simple-json performed as expected in the case where a key/value object was present in the JSON for the "params" key.

However, when the JSON "params" key had an array, my @params property is null. The problem happens during deserialization when Line 1457 of SimpleJson.cs is executed because the list variable is null even though value still contains something useful to me (a JsonArray instance).

So, my simple way of solving this was to change this line to say obj = list ?? value;.

prabirshrestha commented 10 years ago

can you start by sending a breaking unit test as a PR.

ghost commented 9 years ago

Closing issues on projects that don't seem to have a maintainer.