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

Unable to deserialize structs #39

Open hackedd opened 11 years ago

hackedd commented 11 years ago

SimpleJson is unable to deserialize a struct because it can not contain an explicit parameterless constructor. For example:

using SimpleJson;

class Program
{
    public struct Point
    {
        public int x;
        public int y;
    }

    static void Main(string[] args)
    {
        string json = "{ \"x\": 1, \"y\": 1 }";
        Point obj;

        obj = SimpleJson.SimpleJson.DeserializeObject<Point>(json);
        Console.WriteLine("{0}, {1}", obj.x, obj.y);
    }
}

Will throw a NullReferenceException when trying to invoke the constructor in PocoJsonSerializerStragegy.DeserializeObject

prabirshrestha commented 11 years ago

Any reason why you want to use structs?

The core reason we never supported structs was coz SimpleJson was focused primarily on passing json around http and most c# devs would just go with classes. If you are using SimpleJson for other features let us know and we can look into it. In the mean time feel free to send a PR.

hackedd commented 11 years ago

In this case I'd want to use a struct because a point represents a single value (similar to a primitive type), and it makes sense to use a value type. However, changing the code to use a class instead is of course trivial.

I'll see if I can work out how to instantiate structs.