Preferred Serialization and Canonical encoding in CBOR
- Linguagem predominante
- Rust
- Estrelas
- 305
- Forks
- 79
- Merge médio
- 21h 59min
- PRs com merge (30d)
- 2
Descrição
The CBOR mailing list has been discussing the definition of Canonical in the standard and have been making changes. I wanted to document these and maybe discuss how they might be implemented in cbor2.
Updated draft standard: https://datatracker.ietf.org/doc/draft-ietf-cbor-7049bis
What constitutes a true canonical encoding can be redefined at will by protocol implementers, CBOR standard provides guidelines.
A general encoder/decoder like cbor2 will need to support a number of variations and validate them.
Constraints which have been discussed on the ietf mailing list and in the updated draft:
* Shortest possible float representations
* Fixed float representations
* Fixed integer representations
* Maps sorted by lexicographical ordering of encoded value (DRAFT)
* Maps sorted by ascending length then lexicographic (RFC7049)
There may be tagged arrays created for fixed length binary encodings of float values. (Tag values TBD)
See: https://datatracker.ietf.org/doc/draft-ietf-cbor-array-tags/
Decoders may need to validate these by raising errors if the following conditions are met:
* Indefinite length types
* Floating point values not in shortest form
* Floating point values not in fixed representation
* Integers not in shortest form
* Integers not in Fixed form
* Unsorted maps
* Maps sorted with the wrong algorithm
* Maps with duplicate keys
* Incorrect tag type
Instead of a single `canonical=True` argument there needs to be separate flags for each potential constraint.
For example, if a device expects only 16bit floating point data you could create the encoder like this:
```python
encoder = CBOREncoder(f, float_format="binary16")
encoder.encode(data)
```
Or for a minimal float encoding and sorted maps using the encoded length
```python
encoder = CBOREncoder(f, float_format="minimal", sort_maps=True, sort_by_length=True)
encoder.encode(data)
```
On the decoding side:
```python
decoder = CBORDecoder(f, validate_floats_as="binary16")
result = decoder.decode()
```
```python
decoder = CBORDecoder(f, validate_floats_as="minimal",
validate_map_order=True,
ordered_by_length=True,
ignore_duplicate_keys=False)
result = decoder.decode()
```
Of course these argument names and the way they are set up are just intended as an example.
Guia de contribuição
Nenhum guia de contribuição indexado para este repositório
Avaliação
Esta issue ainda não foi avaliada.