Hi, I want to test if some JSON has extraneous fields or not.
I only care if there's a uid and maybe a status field.
But I want to perform a specialized aciton if the are only those two fields.
I know that I can use #[serde(flatten)] to capture those fields into a container
and then test the container using is_empty().
Serde has no such attribute. You could use the #[serde(from = "FromType")] container attribute to convert a different type (with the additional fields) into your desired type with only the flag:
Not sure based on the documentation, but since proc-macros are a purely syntactic abstraction, container types shouldn't be special in any way. Your custom type will probably be treated as a Deserialize-able, and it will be passed a Deserializer on which you can call deserialize_struct() and/or deserialize_map().
It's a wondering about performance implications of making a hashmap and filling it with heap allocated Strings when I don't have to.
So it's a bit pedantic, but I would like to avoid allocating a relatively complex collection such as a HashMap just for the sake of saying 'Yes, more fields are present'.
Ah yes true, a collection in this case is something implementing Deserialize.
Thanks for the example, it looks like that's a good working solution.
I'll take a look at how serde::de::IgnoredAny is implemented, but I'm presuming no allocation occurs.
Of course not. The whole design of Serde avoids allocating by itself in most cases with the visitor pattern – unless the type to be deserialized allocates memory for itself, there won't be extra allocations resulting just form parsing the serialized representation.