This repository was archived by the owner on Aug 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Implement semantic tagging of values #157
Copy link
Copy link
Closed
Labels
Description
Support for CBOR tagged values is the most requested feature in serde_cbor (see #3, #56, #129, #151). Tags allow users to "give [a data item] additional semantics while retaining its structure". They enable the definition of common data types and they are needed to correctly implement some specifications that use CBOR (see for example COSE).
There is a hack (with a few variations) commonly suggested to implement tags: Create a struct with a special name and two fields, one for the tag and another for the value. Now use this type to represent tagged values and give it special treatment in the Serializer and Deserializer` to convert tags.
This has two downsides:
- If the name of the special struct occurs (or is introduced by an attacker) in a CBOR document the deserialization breaks.
- Serde decouples data formats and data types. The hack forces a coupling between the two.
- If a CBOR deserializer emits a tag but the data type does not expect one the deserialization fails (or produces wrong results).
- If a data type emits its tag to a serializer other than the CBOR serializer an artifact from the tag is found in the output.
Design criteria
- The changes made in serde should be small and must be backwards-compatible (that is: not require changes to existing data formats).
- Do not rely on features not yet available in stable rust (such as specialization).
- Tagged values must be ignored by data formats and data types alike if they are not supported.
- The solution should not be limited to CBOR but work for other data formats that use tags as well.
- BSON uses some tags, specialized formats with a rich type model such as PDF would benefit: Deserializing richer types than in Serde's data model is extremely painful serde-rs/serde#1556
- It should be possible to tag a data type for multiple formats and select the correct tag during serialization while ignoring the others.
- Users should be able to:
- Serialize/Deserialize loosely typed
serde_cbor::Values with tags. - Serialize custom types with the correct CBOR tag.
- Deserialize CBOR documents according to the contained tags.
- Serialize/Deserialize loosely typed
Tasks
- Extend the
serde::ser::Serializertrait to allow the serialization of tagged values. (branch)- Get the serialization changes reviewed and merged into serde. (Implement the serialization of tagged values serde-rs/serde#1643)
- Extend
serde::de::Visitorto visit a tagged value and make other necessary changes for deserialization.- Get the deserialization changes reviewed and merged into serde.
- Implement tagged serialization in
serde_cbor. (exprimental branch) - Implement tagged deserialization in
serde_cbor. - Add
Taggedvariant toserde_cbor::Value- Implement serialization.
- Implement deserializaton.
- (optional) Create procedural macros to simplify the serialization of tagged values.
- (optional) Always serialize common types from third-party crates with the correct tag.
-
url::Urlwith tag 32 - standard date/time string with tag 0 (chrono crate?)
- epoch-based date/time with tag 1 (chrono crate?)
- bignums?
- to be extended
-
Reactions are currently unavailable