json icon indicating copy to clipboard operation
json copied to clipboard

Isolate fields that exist in the JSON value but were not present in the struct deserialized into

Open evbo opened this issue 1 year ago • 1 comments

What's the cleanest way to handle a really scary problem I'm sure everyone is aware of:

  1. You create a struct to deserialize all fields that you think exist in a JSON value
  2. Unbeknownst to you, the producer of that JSON value adds new fields to the JSON value that your struct does not account for
  3. Your code silently fails by ignoring those fields since they aren't present in the struct you're deserializing into.

So how could we use serde_json to obtain a Value containing only the fields that were not present in the struct? It's sort of like a diff showing everything your struct is not accounting for.

My end goal would be to add a field to my struct that is just a String value containing a stringified representation of all fields not accounted for by my struct so that at least as a stop gap I'm not discarding them.

evbo avatar Aug 14 '24 16:08 evbo