5

I use an external API that changes unexpectedly. In order to detect these changes, I would like the JSON deserialization (using newtonsoft json.net on ASP.NET Core) to throw an exception whenever it detects a property in the JSON that is not mapped in my POCO.

Is it possible? I tried to use the following deserialization settings but it didn't change anything:

            var serializerSettings = new JsonSerializerSettings
            {
                CheckAdditionalContent = true,
            };
mabead
  • 2,171
  • 2
  • 27
  • 42
  • 1
    @brian-rogers Thsi is not a duplicate (at least not of the question you ponted to). I am asking the opposite question. I want to fail when there are **extra** properties, not **missing** properties. – mabead May 31 '17 at 18:22
  • 1
    It is a duplicate. Extra properties in the JSON == missing properties in your POCO. Try the suggested answer. It does what you want. Fiddle here if you don't believe me: https://dotnetfiddle.net/ggtYOm – Brian Rogers May 31 '17 at 20:35
  • 1
    Oh sorry, you are totally right. Sorry about that. – mabead Jun 01 '17 at 09:44
  • @BrianRogers I would say that question is not duplicate. This one is asking to fails on "over-posting", and the other is asking to fail on "under-posting". It just happens that solution is the same because it handles both. At first I skipped the answer you linked as duplicate because I wanted what OP described. Anyway, thanks for great example that shows that `MissingMemberHandling.Error` handle this use case also. – Mariusz Pawelski Aug 23 '19 at 09:23
  • @MariuszPawelski Re-read both questions more closely. Both are asking to force an error when the JSON has a property for which the target class does not have a corresponding member. That is what `MissingMemberHandling.Error` does. It does NOT handle the opposite situation, when the JSON lacks a property that the target class has. (Fiddle here if you don't believe me: https://dotnetfiddle.net/MsOJVt). In that case you would want to use `[JsonProperty(Required = Required.Always)]` attributes. Fiddle: https://dotnetfiddle.net/QgjZqE – Brian Rogers Aug 23 '19 at 15:21

0 Answers0