diff --git a/content/programming-guides/cbor.md b/content/programming-guides/cbor.md new file mode 100644 index 00000000..4171f1ac --- /dev/null +++ b/content/programming-guides/cbor.md @@ -0,0 +1,206 @@ ++++ +title = "ProtoCBOR Format" +weight = 62 +description = "Covers how to use the Protobuf to CBOR conversion utilities." +type = "docs" ++++ + +Protobuf supports a canonical encoding in CBOR, making it easier to share data +with systems that do not support the standard protobuf binary wire format and +have processing constraints that prefer more concise encoding. + +ProtoCBOR Format is not as efficient as protobuf wire format. +Like ProtoJSON, the converter uses more CPU to encode and decode messages and encoded messages usually consume more space. +CBOR is a schemaless type-length-value (TLV) encoding, so it incurs more interpretive overhead than the standard wire format. + +The encoding is described on a type-by-type basis in the table later in this topic. + +When parsing CBOR-encoded data into a protocol buffer, if a value is missing or if its value is `null`, it will be interpreted as the corresponding [default value](/programming-guides/editions#default). + +When generating CBOR-encoded output from a protocol buffer, if a protobuf field has the default value and if the field doesn't support field presence, it will be omitted from the output by default. +An implementation may provide options to include fields with default values in the output. + +ProtoCBOR does support field presence. +A message type field in any edition of protobuf supports field presence and if set will appear in the output. +Proto3 implicit-presence scalar fields will only appear in the CBOR output if they are not set to the default value for that type. + +
Protobuf | +CBOR | +CBOR example (diagnostic notation) | +Notes | +
---|---|---|---|
message | +map | +{/fooBar/ 0: v, /g/ 1: null, ...} |
+ Generates CBOR maps. Message field numbers are represented as CBOR `int` and become CBOR map keys.
+ null is an accepted value for all field types and treated as the default value of the corresponding field type.
+ |
+
enum | +int | +5 |
+ The enum value as specified in proto is used and sent as a CBOR-encoded int. + | +
map<K,V> | +map | +{ * K => V} |
+ All keys are converted to their CBOR encodings. | +
repeated V | +definite-length array | +[v, ...] |
+ null is accepted as the empty list [] . |
+
bool | +true, false | +true, false |
+ + |
string | +string | +"Hello World!" |
+ + |
bytes | +bstr | +h'deadc0de' |
+ CBOR value will be the data encoded as a byte string. + | +
int32, int64, uint32, uint64 | +int | +1, -10, 0 |
+ CBOR value will use its variable length encodings for integers, which account for the first 60 entries of the initial byte jump table. + Unsigned integers do not use the latter 32 entries of the initial 60 entries of the initial byte jump table. | +
fixed32 | +tagged little endian | +1, 10, 0 |
+ CBOR value is #6.70(bytes .size 4) encoded as little-endian. |
+
+
fixed64 | +tagged little endian | +1, 10, 0 |
+ CBOR value is #6.71(bytes .size 8) encoded as little-endian. |
+
+
sfixed32 | +tagged little endian | +1, -10, 0 |
+ CBOR value is #6.78(bytes .size 4) encoded as little-endian. |
+
+
sfixed64 | +tagged little endian | +1, -10, 0 |
+ CBOR value is #6.79(bytes .size 8) encoded as little-endian. |
+
+
float, double | +float, double | +1.1, -10.0, 0, NaN, Infinity |
+ CBOR value will be the smallest lossless encoding of the floating point number, choosing between simple value, half-precision, single-precision, or double-precision. + | +
Any | +Generic value CBOR |
+ 27([TypeURL, value]) |
+ The URL of a binary `google.protobuf.Type` paired with the deserialized CBOR representation. + Check google.protobuf.Any for details. | +
Timestamp | +#6.0(string) | +0("1972-01-01T10:00:20.021Z") |
+ Uses RFC 3339, where generated output will always be Z-normalized and uses 0, 3, 6 or 9 fractional digits. + Offsets other than "Z" are also accepted. + | +
Duration | +#6.1002(map) | +1002({1: 17020, -9: 900000}) |
+ A duration in RFC9581 format, limited to durations representable in `google.protobuf.Duration`. | +
Struct | +map |
+ { int => any } |
+ Any CBOR map with integer keys and respectively constrained values. See struct.proto . |
+
Wrapper types | +various types | +2, "2", "foo", true, "true", null, 0, ... |
+ Wrappers use the same representation in CBOR as the wrapped primitive
+ type, except that null is allowed and preserved during data
+ conversion and transfer.
+ |
+
FieldMask | +string | +"f.fooBar,h" |
+ See field_mask.proto . TODO |
+
ListValue | +array | +[foo, bar, ...] |
+ Definite-length array. | +
Value | +value | ++ | A CBOR value that is JSON-like, but structs have integer keys. Check + google.protobuf.Value + for details. + | +
NullValue | +null | ++ | CBOR null | +
Empty | +map | +{} |
+ An empty CBOR map, but represented differently as an Any value. | +
name
1
string
methods
Method
options
Option
version
string
@@ -186,6 +260,7 @@ Api is a light-weight descriptor for a protocol buffer service.
source_context
mixins
+ 6
Mixin
syntax
+ 7
Syntax
@@ -225,6 +302,7 @@ Api is a light-weight descriptor for a protocol buffer service.
Wrapper message for `bool`.
The JSON representation for `BoolValue` is JSON `true` and `false`.
+The CBOR representation for `BoolValue` is CBOR `true` and `false`.
@@ -248,6 +326,7 @@ The JSON representation for `BoolValue` is JSON `true` and `false`.
Wrapper message for `bytes`.
The JSON representation for `BytesValue` is JSON string.
+The CBOR representation for `BytesValue` is a CBOR byte string.
@@ -271,6 +350,7 @@ The JSON representation for `BytesValue` is JSON string.
Wrapper message for `double`.
The JSON representation for `DoubleValue` is JSON number.
+The CBOR representation for `DoubleValue` is a CBOR major type 7 value.
@@ -373,6 +453,33 @@ expressed as fractional seconds.
+The CBOR representation for `Duration` is in the RFC9581 format of a #6.1002-tagged map limited to the representation capacity of a `google.protobuf.Duration`.
+Note that the nanoseconds are `uint` and not `int`, so negative durations are represented with different second and nanosecond values.
+A duration below -263s + 0.999999999s is illegal.
+A duration above 263s - 0.999999999s is illegal.
+
+
+
+
+ Key
+ Value Type
+ Description
+
+
+
+
+ &(seconds: 1)
+ int
+ Time in seconds.
+
+
+ &(nanoseconds: -9)
+ uint
+ Fractional time in 1e-9s added to the given seconds. Must be between 0 and 999,999,999.
+
+
+
+
## Empty {#empty}
A generic empty message that you can re-use to avoid defining duplicated empty
@@ -387,6 +494,8 @@ service Foo {
The JSON representation for `Empty` is empty JSON object `{}`.
+The CBOR representation for `Empty` is an empty CBOR map `{}`.
+
## Enum {#enum}
Enum type definition
@@ -395,6 +504,7 @@ Enum type definition
Field name
+ CBOR map key
Type
Description
@@ -402,11 +512,13 @@ Enum type definition
name
+ 1
string
Enum type name.
enumvalue
+ 2
EnumValue
@@ -414,6 +526,7 @@ Enum type definition
options
+ 3
Option
@@ -421,6 +534,7 @@ Enum type definition
source_context
+ 4
SourceContext
@@ -428,11 +542,18 @@ Enum type definition
syntax
+ 5
Syntax
The source syntax.
+
+ edition
+ 6
+ >string
+ The syntax edition.
+
@@ -444,6 +565,7 @@ Enum value definition.
Field name
+ CBOR map key
Type
Description
@@ -451,16 +573,19 @@ Enum value definition.
name
+ 1
string
Enum value name.
number
+ 2
int32
Enum value number.
options
+ 3
Option
@@ -822,6 +947,8 @@ Wrapper message for `float`.
The JSON representation for `FloatValue` is JSON number.
+The CBOR representation for `FloatValue` is a CBOR major type 7 value.
+
@@ -845,6 +972,8 @@ Wrapper message for `int32`.
The JSON representation for `Int32Value` is JSON number.
+The CBOR representation for `Int32Value` is a CBOR major type 0 or 1 value.
+
@@ -868,6 +997,8 @@ Wrapper message for `int64`.
The JSON representation for `Int64Value` is JSON string.
+The CBOR representation for `Int32Value` is a CBOR major type 0 or 1 value.
+
@@ -891,6 +1022,8 @@ The JSON representation for `Int64Value` is JSON string.
The JSON representation for `ListValue` is JSON array.
+The CBOR representation for `ListValue` is a CBOR definite-length array.
+
@@ -918,6 +1051,7 @@ Method represents a method of an api.
Field name
+ CBOR map key
Type
Description
@@ -925,31 +1059,37 @@ Method represents a method of an api.
name
+ 1
string
The simple name of this method.
request_type_url
+ 2
string
A URL of the input message type.
request_streaming
+ 3
bool
If true, the request is streamed.
response_type_url
+ 4
string
The URL of the output message type.
response_streaming
+ 5
bool
If true, the response is streamed.
options
+ 6
Option
@@ -957,6 +1097,7 @@ Method represents a method of an api.
syntax
+ 7
Syntax
@@ -1056,6 +1197,7 @@ service Storage {
Field name
+ CBOR map key
Type
Description
@@ -1063,11 +1205,13 @@ service Storage {
name
+ 1
string
The fully qualified name of the API which is included.
root
+ 2
string
If non-empty specifies a path under which inherited HTTP paths are
@@ -1084,6 +1228,8 @@ service Storage {
The JSON representation for `NullValue` is JSON `null`.
+The CBOR representation for `NullValue` is CBOR `null`.
+
@@ -1108,6 +1254,7 @@ enumeration, etc.
Field name
+ CBOR map key
Type
Description
@@ -1115,6 +1262,7 @@ enumeration, etc.
name
+ 1
string
The option's name. For example, "java_package"
.
@@ -1122,6 +1270,7 @@ enumeration, etc.
value
+ 2
Any
@@ -1159,12 +1308,16 @@ like the file in which it is defined.
+The CBOR representation for `SourceContext` with `file_name` _f_ uses the Any representation, `27([0xb, `_f_`])`.
+
## StringValue {#string-value}
Wrapper message for `string`.
The JSON representation for `StringValue` is JSON string.
+The CBOR representation for `StringValue` is a CBOR UTF-8 string (major type 3).
+
@@ -1192,6 +1345,8 @@ together with the proto support for the language.
The JSON representation for `Struct` is JSON object.
+The CBOR representation for `Struct` is an int-keyed map.
+
@@ -1389,6 +1544,8 @@ Wrapper message for `uint32`.
The JSON representation for `UInt32Value` is JSON number.
+The CBOR representation for `UInt32Value` is a major type 0 value.
+
@@ -1412,6 +1569,8 @@ Wrapper message for `uint64`.
The JSON representation for `UInt64Value` is JSON string.
+The CBOR representation for `UInt64Value` is a major type 0 value.
+
@@ -1438,6 +1597,14 @@ indicates an error.
The JSON representation for `Value` is JSON value.
+The CBOR representation for `Value` validates against the following CDDL.
+
+```cddl
+value /= null / int / float / tstr / bool
+value /= { * int => value }
+value /= [ * value ]
+```
+