add serialize_map_struct
and deserialize_map_struct
#2431
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This might fixes #2350, #1346 and #1584.
This PR should not break any exist crates/code.
Problem
When a struct
S
has a field with attrflatten
, the struct would be treated as mapThis approach works well widely in format that doesn't care with type name (like
serde_json
), while sometimes it's annoying that we would like to treat map and struct differently (like ron, envy).This PR
add
serialize_map_struct
anddeserialize_map_struct
serialize_map
anddeserialize_map
, so nothing would be brokenname
(as well asfields
in deserialize), so your serializer could tell it is a struct other than mapserialize_field
toSerializeMap
in order to get field names if you interest.serialize_tuple
,serialize_tuple_struct
andserialize_tuple_variant
all handle with tuple, but with different parameters to indicate different semantics,serialize_unit
,serialize_newtype_struct
,serialize_struct
,serialize_map
.serialize_map_struct
is also a map, just come with additional information for struct.Alternatives
ron-rs/ron#115 (comment) shows a workaround that used in serde-starlark
It wrapped with
FunctionCall
which givesRustLibrary
struct name, as well as force child struct to be parsed as struct,This approach could handle things well for custom struct, but it is common that the
flatten
structs are import from some crates (and maybe deeply nested), so users had no control on this.