neuecc / Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
MIT License
2.35k stars 266 forks source link

Deserialize enum issues #185

Open mmajcica opened 4 years ago

mmajcica commented 4 years ago

I'm trying to deserialize an enum but with no luck. Following a simple example of what is going wrong:

const string incomingStringFromMicroservice = @"{""Name"":""Greg"",""fruit"":""Apple""}";

var newtonsoftFruit = Newtonsoft.Json.JsonConvert.DeserializeObject<FruitPicker>(incomingStringFromMicroservice);

var utf8Fruit = Utf8Json.JsonSerializer.Deserialize<FruitPicker>(incomingStringFromMicroservice);

where

public class FruitPicker
    {
        public string Name { get; set; }
        public Fruit Fruit { get; set; }
    }
    public enum Fruit
    {
        Orange = 0,
        Apple = 1
    }

In the case of utf8json the Fruit is set to the default value. What I'm I doing wrong? Why is my enum not set correctly?

Thanks