0

I'm looking for the inverse behavior of MissingMemberHandling (and of CheckAdditionalContent?)

The behavior I want is that a call to JsonSerializer.Populate if its input stream is missing fields that, if present, would cause a value to be set in the destination object. Equivalently (in my case): I want the deserializer to check that the JSON contains all the fields and values that would be present if I were to (re)serialize the populated object.

How does one ask the deserializer to check this?

I don't necessarily have ownership over the types that I'm (de)serializing so I can't add my own tags.

dbc
  • 104,963
  • 20
  • 228
  • 340
bfops
  • 5,348
  • 5
  • 36
  • 48
  • Why not just validate the stream against Json schema? there are free libs that do that, e.g. https://github.com/RSuter/NJsonSchema – zaitsman Dec 17 '18 at 23:16
  • Doesn't `MissingMemberHandling` have an `Error` setting? Wouldn't that cause the deserialization to fail? – Jacob Dec 17 '18 at 23:16
  • [`[JsonProperty(Required = Required.Always)]`](https://www.newtonsoft.com/json/help/html/JsonPropertyRequired.htm) (or maybe `Required.AllowNull`) should do the job. See [Json.NET require all properties on deserialization](https://stackoverflow.com/a/29660550/3744182) for a contract resolver that sets it automatically on all properties. In fact this may be a duplicate, agree? – dbc Dec 17 '18 at 23:19
  • @Jacob no, that has the opposite behavior. From the link in my post: "JSON contains a property that isn't a member on the object". Imo it's really poorly named because I agree it sounds like it does the thing I'm asking for but there are tons of posts on SO that confirm that it does the opposite thing. – bfops Dec 17 '18 at 23:21
  • @dbc I don't necessarily have ownership over the types that I'm (de)serializing so I can't add my own tags. – bfops Dec 17 '18 at 23:22
  • 1
    Then the contract resolver from the second half of the answer should do the job. – dbc Dec 17 '18 at 23:23
  • 1
    @dbc yup, it looks like it needs some more tweaking to interact properly with the unrelated contract resolver I've got going, but it definitely solves this problem. I agree this question's a duplicate then! Thanks! – bfops Dec 17 '18 at 23:35

0 Answers0