跳到主要内容

4. 实践考虑 (Practical Considerations)

4.1. 在 Concise Data Definition Language (CDDL) 中指定 CBOR Sequences (Specifying CBOR Sequences in Concise Data Definition Language)

在 Concise Data Definition Language (CDDL) [RFC8610] 中, 通过使用数组作为控制器类型, ".cborseq" 控制操作符 (Section 3.8.4 of [RFC8610]) 已经支持将 CBOR Sequences 作为字节字符串的内容:

my-embedded-cbor-seq = bytes .cborseq my-array
my-array = [* my-element]
my-element = my-foo / my-bar

目前, CDDL 尚不支持将未加修饰的 CBOR Sequences 作为规范的顶层主题. 目前的建议是: 顶层规则使用一个数组, 与 ".cborseq" 控制操作符的用法一致, 并加入英文说明文字, 解释该规范实际描述的是由该数组元素构成的 CBOR Sequence:

; This defines an array, the elements of which are to be used
; in a CBOR Sequence:
my-sequence = [* my-element]
my-element = my-foo / my-bar

未来版本的 CDDL 可能会为顶层 CBOR Sequences 提供一种记法, 例如在 CDDL 规范中使用 group 作为顶层规则.

4.2. 诊断记法 (Diagnostic Notation)

CBOR diagnostic notation (见 [RFC7049] 的 Section 6) 或 extended diagnostic notation ([RFC8610] 的 Appendix G) 目前同样不支持未加修饰的 CBOR Sequences. 后者确实支持按 [RFC8610] 的 Appendix G.3 将 CBOR Sequences 嵌入字节字符串.

本规范按照与上述 CDDL 建议类似的思路, 建议将 CBOR data item 包在一个数组中. 在更非正式的场合, 如果使用该记法的边界是显而易见的, 也可以省略该数组的外层括号, 如下面两个示例所示:

[1, 2, 3]

1, 2, 3

请注意, 使用后一种形式讨论零长度 CBOR Sequences 会有些困难.

4.3. 为跳过元素而优化 CBOR Sequences (Optimizing CBOR Sequences for Skipping Elements)

在某些应用中, 能够高效跳过某个元素而无需解码其子结构, 或者能够将元素高效分发给多线程解码进程, 至关重要. 对于这些应用, 可以使用包含嵌入式 CBOR 的字节字符串作为 CBOR Sequence 的元素, 这些字节字符串以字节为单位携带长度信息:

; This defines an array of CBOR byte strings, the elements of which
; are to be used in a CBOR Sequence:
my-sequence = [* my-element]
my-element = bytes .cbor my-element-structure
my-element-structure = my-foo / my-bar

在一定范围内, 这也可能允许从内部并非良构的元素中恢复. 其限制在于, 字节字符串的 sequence 本身确实需要是良构的.