rpgmaker / NetJSON

Faster than Any Binary? Benchmark: http://theburningmonk.com/2014/08/json-serializers-benchmarks-updated-2/
MIT License
231 stars 29 forks source link

Deserialization to a class that contains enum cause an error #237

Closed isabr85 closed 3 years ago

isabr85 commented 3 years ago

I have made a simple class, Customer, it contains member with type of enum: Gender: public enum Gender { Male = 0, Female = 1 } ` public class Customer

{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public string Address { get; set; }
    public Gender gender { get; set; }
    public Customer()
    {
        FirstName = "First Name Test";
        LastName = "Last Name Test";
        Age = 30;
        Address = "Neverland";
        gender = Gender.Female;
    }
}`

I am trying to deserialize this json: { "FirstName": "First Name Test", "LastName": "Last Name Test", "Age": 30, "Address": "Neverland", "gender": 1 } But it cause an exception: "Must specify valid information for parsing in the string." I guess that it happen because enum is set with int.

What can I do in order to be able to deserialize my json with not exception?

rpgmaker commented 3 years ago

Unfortunately I only support using enum as strings. The current UseEnumString flag in the settings class only let you either use the int or name representation of enum. But both will still be within a quote string.

I do have an option that let register a custom deserializer using NetJSON.RegisterCustomTypeDeserializer but I have not really done a lot of work on it. Since all it does is overwrite/intercept my implementation of deserialization for the type and calls the static method you register.

rpgmaker commented 3 years ago

Please reopen if my answer doesn't work for you.