0

I'm trying to configure JsonConvert to return an error if an attempt is made to deserialize json which includes additional properties which are not included in the C# class object. I have the following code in my Global.asax.cs > Application_Start():

        JsonConvert.DefaultSettings = () => new JsonSerializerSettings
        {
            Formatting = Formatting.Indented,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
            TypeNameHandling = TypeNameHandling.Auto,
            MissingMemberHandling = MissingMemberHandling.Error//,
            //Error = 
        };

The Error property should reference an error handler for deserialization errors. What is the proper way to define/configure this type of error handler? Should the error handler be defined and implemented in it's own class or do most people tend to simply implement the method in global.asax where JsonConvert.DefaultSettings is defined.

Also, I specifically need to configure JsonConvert.DefaultSettings so it throws an error if the json contains an additional property which is not included in the C# model. Will the configuration above achieve this or do I need to take a different approach?

user9393635
  • 1,369
  • 4
  • 13
  • 32
  • This question seems partly opinion-based. But you also asked, *I specifically need to configure JsonConvert.DefaultSettings so it throws an error if the json contains an additional property which is not included in the C# model. Will the configuration above achieve this?* - It should, see [Can you detect if an object you deserialized was missing a field with the JsonConvert class in Json.NET](https://stackoverflow.com/a/21032130/3744182). – dbc May 04 '18 at 16:25
  • Or maybe you are looking for [How to set custom JsonSerializerSettings for Json.NET in MVC 4 Web API?](https://stackoverflow.com/q/13274625/3744182) – dbc May 04 '18 at 16:28

0 Answers0