Preferred Serialization and Canonical encoding in CBOR
- 主要语言
- Rust
- 星标
- 305
- 派生
- 79
- 平均合并
- 21 小时 59 分钟
- 30 天内合并 PR
- 2
描述
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.
贡献指南
这个仓库没有索引到贡献指南
评估
这个 Issue 还没有评估数据。