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

Unable to deserialize structs #39

Open
hackedd opened this issue Sep 9, 2013 · 2 comments
Open

Unable to deserialize structs #39

hackedd opened this issue Sep 9, 2013 · 2 comments

Comments

@hackedd
Copy link

hackedd commented Sep 9, 2013

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
Copy link
Member

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
Copy link
Author

hackedd commented Sep 10, 2013

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.

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