Codec#

This page covers how a client encodes and decodes values, as described under Protocol Buffers Encoding. The conformance tests check the client’s encoder and decoder against fixed encodings, with no server involved.

Scalars#

codec-integers [core] [MUST]

An integer of each type encodes to, and decodes from, its zigzag or plain varint. The vectors include zero, the minimum and maximum, the edges of the one byte encodings, and values past the range of a smaller type or of the integers a double holds exactly. A decoded value is an integer.

codec-floating-point [core] [MUST]

A double or float encodes to, and decodes from, its IEEE 754 bytes. The vectors include negative zero, the infinities, the largest value, the smallest normal value and the smallest subnormal value. A NaN of either sign, quiet or signaling, decodes as a NaN, and a NaN encodes to a NaN. A decoded value is a floating point number.

codec-bool [core] [MUST]

A bool encodes as the varint 1 or 0.

codec-enum [core] [MUST]

An enumeration value encodes as its value, as a sint32.

codec-out-of-range [dynamic] [MUST]

In a dynamically typed language, encoding an integer outside the range of its type raises the argument error. This includes a negative value for an unsigned type.

Strings and Bytes#

codec-string [core] [MUST]

A string encodes as the length of its UTF-8 bytes, as a varint, then the bytes. The vectors include characters of each UTF-8 length, a NUL character, and lengths that take one, two and three bytes.

codec-bytes [core] [MUST]

Bytes encode as their length, as a varint, then the bytes. The vectors include every byte value, bytes that are not valid UTF-8, and lengths that take one, two and three bytes.

Objects#

codec-object [core] [MUST]

An object encodes as its id, as a uint64. Every id is an object, including 0, as a null is carried by the is_null flag or a presence bool. The vectors include an id past the range of a uint32.

Collections and Structures#

codec-collections [core] [MUST]

Lists, sets, dictionaries and tuples encode to, and decode from, their given encodings. The vectors include empty collections, empty items, and items longer than 127 bytes. The entries of a dictionary and the items of a set may be encoded in any order.

codec-dictionary-value-left-out [core] [MUST]

A dictionary entry with no value field decodes with the value that encodes to no bytes, such as an empty list. A protobuf encoder leaves out a field holding no bytes.

codec-structures [core] [MUST]

A structure encodes as a tuple of its fields, in the order the structure declares them.

codec-extra-items [core] [MUST]

An encoded tuple or structure with more items than its type decodes, and the extra items are ignored. In a dynamically typed language, encoding a tuple or structure value with more items than its type raises the argument error.

codec-too-few-items [core] [MUST]

Decoding a tuple or structure with fewer items than its type raises the encoding error. In a dynamically typed language, encoding a tuple or structure value with too few items raises the argument error.

Null#

codec-nullable [core] [MUST]

A value at a position that can hold null carries a presence bool, which alone encodes a null. The bool comes ahead of the value’s own encoding, including that of a collection or a structure. The vectors set apart a null from a zero, false, an empty string, empty bytes and an empty collection, in list items, tuple items, dictionary values and structure fields.

Malformed Data#

codec-malformed-data [core] [MUST]

Decoding raises the encoding error for each of these:

  • data cut short, including a varint cut short

  • a length past the end of the data

  • a nullable item with no presence bool, with a presence bool and no value, with a presence bool other than 0 or 1, or with data after a null

  • a dictionary entry cut short

codec-empty-data [core] [MUST]

Decoding data with no bytes as a number, a bool, a string or bytes raises the encoding error. Each of these types encodes to at least one byte.

codec-invalid-utf8 [core] [SHOULD]

Decoding a string that is not valid UTF-8 raises the encoding error.

Messages#

codec-message [core] [MUST]

A protobuf message encodes as its serialized bytes, and with its length prefix when framed.