Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rmpv::ext::to_value_named to keep struct keys #337

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

julienvincent
Copy link

The current rmpv::ext::to_value implementation turns Structs into Value::Array<Value::String> which is not deserializable by other languages.

Technically I would call this a bug and opt to change the existing to_value implementation, but to stay cautious of not breaking existing users' code I think it makes sense to introduce a new to_value_named fn which instead turns Structs into Value::Map and maintains their keys.

Fixes #190

The current `rmpv::ext::to_value` implementation turns Structs into
`Value::Array<Value::String>` which is not deserializable by other
languages.

Technically I would call this a bug and opt to change the existing
`to_value` implementation, but to stay cautious of not breaking existing
users' code I think it makes sense to introduce a new `to_value_named`
fn which instead turns Structs into `Value::Map` and maintains their
keys.
where T: Serialize
{
ser::SerializeSeq::serialize_element(self, value)
self.map.push((to_value(key)?, to_value(value)?));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to call to_value_named(value) for value here if named is set to true to ensure any struct values get serialised with key names.

Similar kind of refactoring needs to be done for SerializeVec, DefaultSerializeMap etc. to ensure any struct values are encoded with key names if Serializer has named_structs set to true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Serializing custom types into Value, allow named Map instead of Array representation
2 participants