Serialize Rust data structures to / from pods#396
Conversation
|
|
As @robknight has pointed out, it makes more sense to represent a struct containing an Since the serialization feature is intended to be "something that does what you want most of the time" rather than "the canonical way of converting Rust data structures to POD", maybe it would make more sense to have newtype structs for the deserializers, rather than having |
|
The latest commit makes it so that |
|
I looked into an alternate approach that uses procedural macros instead of serde. Code here: Advantages of the serde approach:
Advantages of the procedural macro approach:
|
|
After trying the procedural macro approach for a while, I concluded that it's probably better in principle but also more time-consuming to implement. So the serde approach seems more realistic for now. |
This PR allows Rust data structures to be serialized to / from
podsValueandDictionary. There are some example tests inmiddleware/serialization.rs.The implementation seems good enough to cover most use cases, but there are few ways that it could be improved:
Valuewould return the sameValue, and serializing any ofRawValue,Point,SecretKey,Dictionary,Set,Arraywould just wrap it in aTypedValuein the obvious way. This behavior is implemented forRawValue,Point, andSecretKey, but not forDictionary,Set,ArrayandValue. Implementing the remaining four would be feasible, but the code would be ugly.RawValue,Point, orSecretKey, then the serializer will think that it is one of the POD types. We could fix this by having our types pass custom names toSerializer::serialize_newtype_struct.Dictionary, and maybe having deserializers for owned types in addition to reference types.Deserializeimplementation decide if it can do anything with that. It seems customary to return an error instead.Where possible, I tried to mimic
serde_jsonbehavior, but a few things had to be done differently since POD does not have null or floating point numbers.()and unit structs are serialized as an emptyArray(the JSON serializer usesnull)Noneis serialized as the string"None"(the JSON serializer usesnull)Some(x)is serialized as a dictionary mapping"Some"tox(the JSON serializer just usesx)Array/SetforNoneand a one-elementArray/SetforSome(x)instead?f32andf64are serialized as strings using scientific notation