soramitsu / 1code-rust

Rust version of https://github.com/soramitsu/1code-java
Apache License 2.0
3 stars 2 forks source link

Structs' fields should be serialized in lexicographical order #4

Open gsomix opened 4 years ago

gsomix commented 4 years ago

Structs are encoded as dictionaries, so fields' names (keys) should be serialized in the in lexicographical order. In accordance with specification:

For each possible (complex) value, there is only a single valid 1coding; i.e. there is a bijection between values and their encodings. ... A dictionary is encoded as d<contents>e. The elements of the dictionary are 1coded each key immediately followed by its value. All keys must be byte strings and must appear in lexicographical order.

humb1t commented 4 years ago

@gsomix thanks, will fix the order. What about only a single valid coding, I don't think we can reach it for all languages. This guarantee works well only for theoretical types described in spec.

humb1t commented 4 years ago

https://github.com/contain-rs/linked-hash-map/pull/48 https://github.com/serde-rs/serde/issues/269 https://github.com/serde-rs/json/pull/80 https://github.com/serde-rs/serde/issues/65#issuecomment-96399485

humb1t commented 4 years ago

impl<'a> ser::SerializeMap for &'a mut Serializer and impl<'a> ser::SerializeStruct for &'a mut Serializer { should preserve order. To do so - we need to add more fields to Serializer struct and "flush" into result string in the end of the struct (e char)

humb1t commented 4 years ago

https://github.com/serde-rs/json - Compound type should be used