facebook-csharp-sdk/simple-json

Unable to deserialize structs

Opened this issue · 2 comments

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

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.

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.