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

Wrong managing Formatter Resolvers for DynamicCompositeResolver (Be careful) #197

Open AndrewGumenyuk opened 4 years ago

AndrewGumenyuk commented 4 years ago

I've set of Resolvers which I want to pass for configuring OutputFormatters and InputFormatters.

Ex. Utf8Json.Resolvers.CompositeResolver.Create(StandardResolver.CamelCase, EnumResolver.UnderlyingValue);

Note, that in this example all enums will be serialized and deserialized as with EnumResolver.Default but that data is in CamelCase. So, the second Formatter wasn't worked properly.

But, if you change the order of resolvers, everything would be applied (and CamelCase also). Ex. Utf8Json.Resolvers.CompositeResolver.Create(EnumResolver.UnderlyingValue, StandardResolver.CamelCase)

The full example of code:

.AddMvcOptions(options =>
                {
                    //remove default JsonFormatter from Mvc
                    options.OutputFormatters.RemoveType<SystemTextJsonOutputFormatter>();
                    options.InputFormatters.RemoveType<SystemTextJsonInputFormatter>();

                    var resolver = Utf8Json.Resolvers.CompositeResolver.Create(EnumResolver.UnderlyingValue, StandardResolver.CamelCase);

                    options.OutputFormatters.Add(new JsonOutputFormatter(resolver));
                    options.InputFormatters.Add(item: new JsonInputFormatter(resolver));
                })