| Title: | 'Protocol Buffers' Decoding and Encoding with 'upb' |
| Version: | 0.1.0 |
| Description: | Decodes and encodes 'Protocol Buffers' messages from descriptor sets rather than generated code. One message becomes a named list; many messages become a data frame built column by column. Uses a bundled copy of the 'upb' runtime (https://github.com/protocolbuffers/protobuf), so no system 'protobuf' library is required. Schemas are compiled into descriptor sets beforehand, with 'protoc' or 'buf'. |
| License: | MIT + file LICENSE |
| Copyright: | See inst/COPYRIGHTS and src/vendor/PROVENANCE. |
| URL: | https://github.com/pedrobtz/zuproto, https://pedrobtz.github.io/zuproto/ |
| BugReports: | https://github.com/pedrobtz/zuproto/issues |
| Depends: | R (≥ 4.1) |
| Suggests: | bit64, testthat (≥ 3.0.0), withr, zujson |
| Config/testthat/edition: | 3 |
| Encoding: | UTF-8 |
| Language: | en-GB |
| Config/roxygen2/version: | 8.1.0 |
| NeedsCompilation: | yes |
| Packaged: | 2026-11-06 12:18:08 UTC; pbtz |
| Author: | Pedro Baltazar [aut, cre, cph], Google LLC [cph] (upb and utf8_range, bundled in src/vendor), Yibo Cai [cph] (utf8_range, bundled in src/vendor/utf8_range) |
| Maintainer: | Pedro Baltazar <pedrobtz@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-11-28 15:20:02 UTC |
zuproto: 'Protocol Buffers' Decoding and Encoding with 'upb'
Description
Decodes and encodes 'Protocol Buffers' messages from descriptor sets rather than generated code. One message becomes a named list; many messages become a data frame built column by column. Uses a bundled copy of the 'upb' runtime (https://github.com/protocolbuffers/protobuf), so no system 'protobuf' library is required. Schemas are compiled into descriptor sets beforehand, with 'protoc' or 'buf'.
Author(s)
Maintainer: Pedro Baltazar pedrobtz@gmail.com [copyright holder]
Authors:
Pedro Baltazar pedrobtz@gmail.com [copyright holder]
Other contributors:
Google LLC (upb and utf8_range, bundled in src/vendor) [copyright holder]
Yibo Cai (utf8_range, bundled in src/vendor/utf8_range) [copyright holder]
See Also
Useful links:
Report bugs at https://github.com/pedrobtz/zuproto/issues
Decode one Protocol Buffers message
Description
Decodes the bytes of one message of type type into a named list, one
element per field, by the field's name. upb decodes the whole message
first; R values are built only from a message that decoded completely.
Usage
proto_decode(
x,
type,
pool,
int64 = c("double", "character", "integer64"),
enums = c("factor", "integer", "character"),
defaults = TRUE,
unknown = c("drop", "keep"),
bytes = c("raw", "base64"),
max_size = 64 * 1024^2,
max_depth = 100L
)
Arguments
x |
The serialised message: a raw vector, a path to a file, or a
connection. A file or connection is read up to |
type |
The message type's full name, such as |
pool |
A pool from |
int64 |
How 64-bit integer fields become R values: |
enums |
How enum fields become R values: |
defaults |
Whether unset fields without presence take their
defaults ( |
unknown |
|
bytes |
How |
max_size |
The largest input accepted, in bytes. upb's memory for the decode is also capped. The cap is a multiple of the input's size, set by the message type: the most a message of that type could need. |
max_depth |
The deepest nesting accepted: how many levels of messages within messages, the top-level message being level 0. A message holding a message holding a message is depth 2. A map entry counts as a level too, as upb counts it, so a message reached through a map is two levels down. At most 1000, so that hostile input cannot exhaust the C stack. |
Value
A named list. An int32-family value of -2^31 is NA, with
a zuproto_precision_loss warning, since R reserves it for NA.
Protobuf to R
| Protobuf | R |
double, float | double |
int32, sint32, sfixed32 | integer |
uint32, fixed32 | double, exact |
int64, uint64, sint64, fixed64, sfixed64 | per int64 |
bool | logical |
string | character, UTF-8 |
bytes | raw, or per bytes |
| enum | factor with the enum's value names as levels, or per enums |
| message | named list |
repeated scalar | vector |
repeated message | list of lists |
map<K, V> | named vector or named list, sorted by key |
oneof | the member that is set |
| extension | element named by its full name in brackets, [pkg.ext]
|
Well-known types: Timestamp becomes POSIXct in UTC, rounded to the
microsecond; Duration a difftime in seconds; a wrapper
(Int32Value and the rest) its value; Struct a named list, Value
its value (NULL for null) and ListValue a list, as in protobuf
JSON; Any a list of type_url and value (the packed bytes);
FieldMask a character vector of paths; Empty an empty list.
Repeated Timestamp, Duration and wrapper fields become vectors.
A field that tracks presence (a message, a oneof member, a proto2 or
proto3 optional field) is left out when it is not set. A field that
does not track presence cannot be told apart from its default on the
wire. With defaults = TRUE it takes its default (0, "", FALSE,
the enum's first value, an empty vector), as other protobuf runtimes
do. With defaults = FALSE it is left out when it holds its default.
Required fields are not checked: a proto2 required field missing
from the input decodes like any unset field.
Concatenated messages merge, as protobuf specifies: decoding
c(raw1, raw2) gives the message raw1 holds with raw2 merged into
it. A singular field takes the last value, a repeated field appends, a
map adds entries (a later key replaces an earlier one) and a message
field merges. This is protobuf's MergeFromString(). A message's
encoded size is length(proto_encode(x, type, pool)).
See Also
zuproto-conditions for the errors it raises.
Examples
pool <- proto_pool()
# A Timestamp of 1.5 seconds after the epoch: field 1 = 1, field 2 = 5e8.
bytes <- as.raw(c(0x08, 0x01, 0x10, 0x80, 0xca, 0xb5, 0xee, 0x01))
proto_decode(bytes, "google.protobuf.Timestamp", pool)
proto_decode(as.raw(c(0x08, 0x2a)), "google.protobuf.Int32Value", pool)
Decode many messages into a data frame
Description
Decodes many messages of one type into a data frame, one row per
message, built column by column: every message is decoded first, then
each column is filled across all of them. A message that fails stops the
whole call, and the condition's index says which one.
Usage
proto_decode_df(
x,
type,
pool,
field = NULL,
int64 = c("double", "character", "integer64"),
enums = c("factor", "integer", "character"),
defaults = TRUE,
bytes = c("raw", "base64"),
max_size = 64 * 1024^2,
max_depth = 100L,
max_rows = 1e+07
)
Arguments
x |
For |
type |
The message type's full name, such as |
pool |
A pool from |
field |
The name of a repeated message field of |
int64 |
How 64-bit integer fields become R values: |
enums |
How enum fields become R values: |
defaults |
Whether unset fields without presence take their
defaults ( |
bytes |
How |
max_size |
The largest input accepted, in bytes. upb's memory for the decode is also capped. The cap is a multiple of the input's size, set by the message type: the most a message of that type could need. |
max_depth |
The deepest nesting accepted: how many levels of messages within messages, the top-level message being level 0. A message holding a message holding a message is depth 2. A map entry counts as a level too, as upb counts it, so a message reached through a map is two levels down. At most 1000, so that hostile input cannot exhaust the C stack. |
max_rows |
The largest number of rows accepted. |
Details
With field, the input is one message instead, and the rows are the
elements of its repeated message field field. That is how a feed that
wraps its records in one message becomes a table. A GTFS Realtime
FeedMessage is one, with its records in the entity field.
Value
A data frame.
Columns
| Field | Column |
| scalar, enum, scalar-like well-known type | atomic vector, as in proto_decode() |
bytes | list of raw vectors |
| message | data frame (a column of the frame) |
repeated scalar | list of vectors |
repeated message | list of data frames |
map | list of named vectors or lists |
Struct, Value, ListValue, Any | list, as in proto_decode() |
oneof | a column per member, plus <oneof>_case with the member set |
| extension | a column named [pkg.ext] per extension the pool knows
|
A field a row does not set is NA (NULL in a list column) when the
field tracks presence. Otherwise it takes its default, or is NA with
defaults = FALSE. The <oneof>_case column is named <oneof>_case_
instead when a field has that name. A message field that no row sets is
an all-NA logical column rather than a data frame, which is also where
the columns of a recursive type end. A message type with no fields, such
as Empty, is a list column: list() where set, NULL where not. A
nested message that is set but has every field unset reads as unset,
since its row is all NA.
See Also
proto_read_delimited() for length-delimited streams.
Examples
pool <- proto_pool()
# Three FieldDescriptorProto messages, one per row.
type <- "google.protobuf.FieldDescriptorProto"
msgs <- lapply(c("id", "name", "when"), function(name) {
proto_encode(list(name = name, number = 1L), type, pool)
})
df <- proto_decode_df(msgs, type, pool)
df[c("name", "number")]
Encode one Protocol Buffers message
Description
Encodes a named list as one message of type type, the inverse of
proto_decode(): every value proto_decode() returns encodes back to
the same message.
Usage
proto_encode(
x,
type,
pool,
deterministic = TRUE,
unknown = TRUE,
check_required = TRUE,
extra = c("error", "ignore")
)
Arguments
x |
A named list, one element per field. For a well-known type with
an R scalar form ( |
type |
The message type's full name, such as |
pool |
A pool from |
deterministic |
Whether to encode deterministically: map entries sorted by key, so equal values always give equal bytes. |
unknown |
Whether to write back unknown fields kept by
|
check_required |
Whether a missing proto2 |
extra |
|
Details
Each element is a field, by its name, or an extension, by its full name
in brackets ("[pkg.ext]"). An element that is NULL or NA leaves its
field unset, as does a field the list leaves out. Values take the R
forms proto_decode() gives them, and also:
integer kinds: integers, or doubles that are whole and in range; 64-bit kinds also decimal strings, which are exact;
enums: factors, value names, or numbers (a closed enum accepts only the numbers it defines);
-
Timestamp:POSIXctor seconds since the epoch (not aDate, whose number is days: convert it withas.POSIXct());Duration:difftimeor seconds; a wrapper: its value; strings: valid UTF-8, or in an encoding R translates to it;
-
Value:NULL, a logical, number or string, a named list (aStruct), or an unnamed list or vector (aListValue).
bytes fields take raw vectors (a list of them when repeated).
Value
A raw vector.
See Also
zuproto-conditions for the errors it raises; each one about a
value carries path, the field at fault, such as "points[2].x".
Examples
pool <- proto_pool()
proto_encode(42L, "google.protobuf.Int32Value", pool)
when <- as.POSIXct("2026-10-25 12:00:00", tz = "UTC")
raw <- proto_encode(when, "google.protobuf.Timestamp", pool)
proto_decode(raw, "google.protobuf.Timestamp", pool)
Enum values
Description
Lists the values of one enum type in a pool: the levels a decoded enum field takes as a factor, with their numbers.
Usage
proto_enums(pool, type)
Arguments
pool |
A pool from |
type |
An enum type's full name, such as
|
Value
A data frame with one row per value, in definition order, and
columns name and number. An alias (allow_alias) is a row of its
own with the number it shares. R has no integer for -2^31, so that
number is NA, with a zuproto_precision_loss warning.
Examples
pool <- proto_pool()
proto_enums(pool, "google.protobuf.Field.Kind")
Descriptor pools
Description
A pool holds the message types zuproto can decode and encode. It is
built from serialised FileDescriptorSets, which protoc and buf
write:
Usage
proto_pool(x = NULL)
proto_pool_add(pool, x)
Arguments
x |
A serialised |
pool |
A pool from |
Details
protoc --include_imports --descriptor_set_out=schema.binpb schema.proto buf build -o schema.binpb
Every pool starts with the well-known types (google/protobuf/*.proto,
including descriptor.proto), so a set need not include them.
Descriptor sets are trusted input: upb checks them, but less thoroughly
than protoc does. Load sets you or your tools produced, not ones from
an untrusted source. Message bytes, by contrast, are never trusted.
Files are added in dependency order, whatever order the set lists them
in. A file already in the pool is skipped if its contents are identical.
It is a zuproto_pool_conflict if they are not, and the same holds for
a file listed twice. The exception is a bundled well-known type's file,
which another protoc release writes differently: the pool keeps its
own copy and skips the one in the set. A file importing one that is
neither in the set nor in the pool is a zuproto_pool_error naming
both, and nothing from the set is added. Extensions are loaded with the
files that declare them.
Value
proto_pool() returns a pool. proto_pool_add() returns pool,
invisibly, after adding x to it.
See Also
proto_types() and proto_fields() to see what a pool holds.
Examples
pool <- proto_pool()
pool
"google.protobuf.Timestamp" %in% proto_types(pool)$name
Length-delimited streams
Description
In a length-delimited stream, each message is preceded by its length as
a varint. Java's writeDelimitedTo() writes such streams, as do
Python's _VarintBytes helpers and many .pb log files.
Usage
proto_read_delimited(
x,
type,
pool,
each = NULL,
chunk_rows = 10000,
int64 = c("double", "character", "integer64"),
enums = c("factor", "integer", "character"),
defaults = TRUE,
bytes = c("raw", "base64"),
max_size = 64 * 1024^2,
max_depth = 100L,
max_rows = 1e+07
)
proto_write_delimited(x, type, pool, path = NULL, ...)
Arguments
x |
For |
type |
The message type's full name, such as |
pool |
A pool from |
each |
|
chunk_rows |
The number of messages in each chunk given to |
int64 |
How 64-bit integer fields become R values: |
enums |
How enum fields become R values: |
defaults |
Whether unset fields without presence take their
defaults ( |
bytes |
How |
max_size |
The largest input accepted, in bytes. upb's memory for the decode is also capped. The cap is a multiple of the input's size, set by the message type: the most a message of that type could need. |
max_depth |
The deepest nesting accepted: how many levels of messages within messages, the top-level message being level 0. A message holding a message holding a message is depth 2. A map entry counts as a level too, as upb counts it, so a message reached through a map is two levels down. At most 1000, so that hostile input cannot exhaust the C stack. |
max_rows |
The largest number of rows accepted. With |
path |
|
... |
Passed to |
Details
proto_read_delimited() reads a stream into a data frame like
proto_decode_df() builds. With each, it reads the stream in chunks
of chunk_rows messages and calls each(df) on each chunk instead, so
a stream larger than memory can be processed. proto_write_delimited()
writes a stream.
A stream that ends inside a length or a message is a
zuproto_parse_error.
proto_write_delimited() takes a data frame whose columns
follow the column table of proto_decode_df(), one message per row,
or a list of messages as proto_encode() takes them. NA values leave
fields unset, and <oneof>_case columns are ignored. A NULL cell
leaves its field unset too, so a Value that held JSON null is not
written back: a data frame cannot tell it from an unset one.
Value
proto_read_delimited(): a data frame, or with each, NULL,
invisibly.
proto_write_delimited(): the stream as a raw vector, or with
path, the path, invisibly.
Examples
pool <- proto_pool()
rows <- data.frame(seconds = c(1, 2, 3), nanos = 0L)
stream <- proto_write_delimited(rows, "google.protobuf.Duration", pool)
proto_read_delimited(stream, "google.protobuf.Duration", pool)
Protobuf JSON
Description
Converts a binary message to protobuf's canonical JSON mapping and back,
through upb. JSON field names are the json_names (lowerCamelCase) by
default, 64-bit integers are strings, bytes are base64, and well-known
types take their special JSON forms (Timestamp as an RFC 3339 string,
Struct as an object, and so on).
Usage
proto_to_json(
x,
type,
pool,
emit_defaults = FALSE,
proto_names = FALSE,
enums = c("name", "integer"),
parse = FALSE,
max_size = 64 * 1024^2,
max_depth = 100L
)
proto_from_json(
json,
type,
pool,
ignore_unknown = FALSE,
check_required = TRUE,
max_size = 64 * 1024^2
)
Arguments
x |
For |
type |
The message type's full name, such as |
pool |
A pool from |
emit_defaults |
Whether to write fields that hold their defaults. |
proto_names |
Whether to use the |
enums |
|
parse |
Whether to return the JSON parsed into an R value by
|
max_size |
The largest input accepted, in bytes. upb's memory for the decode is also capped. The cap is a multiple of the input's size, set by the message type: the most a message of that type could need. |
max_depth |
The deepest nesting accepted: how many levels of messages within messages, the top-level message being level 0. A message holding a message holding a message is depth 2. A map entry counts as a level too, as upb counts it, so a message reached through a map is two levels down. At most 1000, so that hostile input cannot exhaust the C stack. |
json |
Protobuf JSON for one message: a single string of JSON text,
or an R value to write as JSON with |
ignore_unknown |
Whether to skip JSON fields the type does not define, rather than fail. |
check_required |
Whether a missing proto2 |
Details
Any messages convert only when the type they pack is in the pool.
The entries of a map field, and so the keys of a Struct, are written
in no fixed order: it can differ between R sessions. With
parse = TRUE they are sorted by key, as proto_decode() sorts maps, so
the parsed value is the same in every session. Compare JSON as parsed
values, not as text.
JSON meets R values through
zujson, which must be
installed for it. proto_to_json(parse = TRUE) returns
zujson::json_parse()'s R value. proto_from_json() takes an R value as
well as JSON text, and writes it with zujson::json_write(). There, a
length-1 vector is a JSON scalar, so give a repeated field of one element
as I(x) or list(x).
Value
proto_to_json(): a single string, or with parse = TRUE an R
value. proto_from_json(): the message as a raw vector, encoded
deterministically.
Examples
pool <- proto_pool()
raw <- proto_encode(
as.POSIXct("2026-10-25 12:00:00", tz = "UTC"),
"google.protobuf.Timestamp", pool
)
json <- proto_to_json(raw, "google.protobuf.Timestamp", pool)
json
identical(proto_from_json(json, "google.protobuf.Timestamp", pool), raw)
Message types and their fields
Description
proto_types() lists every message type in a pool, nested types
included, as a data frame. proto_fields() lists the fields of one
type, followed by the extensions the pool knows for it.
Usage
proto_types(pool)
proto_fields(pool, type)
Arguments
pool |
A pool from |
type |
A message type's full name, such as
|
Details
An extension is named by its full name in brackets, as in protobuf's
JSON mapping, for example "[transit_realtime.trip_descriptor_ext]".
Decoded messages use the same names.
Value
proto_types(): a data frame with columns name (full name),
file (the .proto file declaring it) and fields (the number of
fields), sorted by name. Map entry types are left out.
proto_fields(): a data frame with one row per field and columns
-
name: the field's name, or[full.name]for an extension; -
number: the field number; -
type: the protobuf type, such as"int32","string","message"or"enum"; -
label:"optional","required"or"repeated"; -
type_name: the message or enum type's full name,NAfor scalars; -
oneof: the oneof the field belongs to, orNA; -
json_name: the name in protobuf JSON; -
presence: whether the field tracks whether it is set; -
packed: whether repeated values are packed on the wire; -
map: whether the field is a map; -
extension: whether the field is an extension.
Examples
pool <- proto_pool()
head(proto_types(pool))
proto_fields(pool, "google.protobuf.Timestamp")
Pack and unpack an Any
Description
proto_unpack_any() decodes the message a google.protobuf.Any packs,
as proto_decode() decodes any message. The type is the part of
type_url after its last /, and must be in the pool.
proto_pack_any() is the inverse: it encodes a message with
proto_encode() and names its type, giving a value to set an Any
field to.
Usage
proto_unpack_any(x, pool, ...)
proto_pack_any(x, type, pool, prefix = "type.googleapis.com/", ...)
Arguments
x |
For |
pool |
A pool from |
... |
Passed to |
type |
The message type's full name. |
prefix |
The |
Value
proto_unpack_any(): the packed message, as proto_decode()
returns it. proto_pack_any(): an Any, a list of type_url and
value.
Examples
pool <- proto_pool()
any <- proto_pack_any(42L, "google.protobuf.Int32Value", pool)
any$type_url
proto_unpack_any(any, pool)
Conditions raised by zuproto
Description
Every error zuproto raises carries a condition class, so it can be caught
by kind rather than by matching the message, which may change. Every
class below inherits from zuproto_error.
Details
zuproto_invalid_argumentAn argument was unusable, such as a pool that is not one, or a type name that is not a single string. Carries
arg, the argument at fault.zuproto_io_errorA file could not be opened or read, or a connection could not be read from.
zuproto_pool_errorA descriptor set could not be added to a pool: the bytes are not a
FileDescriptorSet, a file imports one that is neither in the set nor in the pool, or upb rejected a file. Carriesfile, the file at fault, when there is one.zuproto_pool_conflictA subclass of
zuproto_pool_error: a file of the same name is already in the pool, or earlier in the set, with different contents. Carriesfile.zuproto_unknown_typeA message type is not in the pool. Carries
type.zuproto_parse_errorThe input is not a valid message of the type: it ends early, has a bad tag, a length beyond the input, or the wrong wire type. Carries
status.zuproto_invalid_utf8A subclass of
zuproto_parse_error: astringfield is not valid UTF-8, in proto2 as in proto3.zuproto_unrepresentableThe input is valid but holds a value R cannot: a string containing a NUL byte, or longer than an R string may be. Carries
field, the field's full name.zuproto_type_mismatchproto_encode()was given a value of the wrong form for its field: the wrong R type, a number out of range or not whole, a name that is not a value of the enum,NAinside a repeated field, a string that is not valid UTF-8 text, a date or time where a number is expected (or aDatefor aTimestamp), a second member of a oneof, or a field or map key given twice. Carriespath, the field at fault, such as"points[2].x".zuproto_unknown_fieldproto_encode()was given a name the message type does not define. Carriespath.zuproto_missing_requiredproto_encode()withcheck_required = TRUE: a proto2requiredfield is not set.zuproto_json_errorproto_from_json()was given JSON that is not valid, or not the type's, orproto_to_json()could not write a message (anAnywhose packed type is not in the pool). Carriesdetail, upb's description.zuproto_limit_errorA limit was reached, and a subclass names which.
zuproto_size_limit: the input, or the memory decoding it needs, is too large formax_size.zuproto_depth_limit: messages nest deeper thanmax_depth.zuproto_row_limit: there are more rows thanmax_rows. It carrieslimit, the argument's name, andlimit_value, its value. A decode can need more memory than any message of its type and size could. Thenlimitis"memory"andlimit_valueisNA, since no argument raises that ceiling.proto_encode()raiseszuproto_depth_limitfor messages nested deeper than 1000 levels, the mostmax_depthletsproto_decode()accept.zuproto_internal_errorMemory ran out, or upb reported a status zuproto does not expect. Carries
code.
An error about one message of proto_decode_df(),
proto_read_delimited() or proto_write_delimited() also carries
index, the message's position in the batch or stream. It is a double,
since a stream can hold more than 2^31 - 1 messages.
Warnings inherit zuproto_warning:
zuproto_precision_lossValues could not be represented exactly: 64-bit integers beyond 2^53 decoded as doubles, or a 32-bit
-2^31, which R reserves forNA. Carriesn, how many.zuproto_unknown_enumEnum numbers the enum does not define became
NA. Carriesn.
Examples
pool <- proto_pool()
tryCatch(
proto_fields(pool, "no.such.Message"),
zuproto_unknown_type = function(e) e$type
)
Build information
Description
Reports the zuproto version and the Protocol Buffers release whose upb runtime is bundled.
Usage
zuproto_info()
Value
A list of class zuproto_info with elements:
-
version: zuproto's version. -
protobuf: the protobuf release tag upb was vendored from, such as"v36.2". -
protobuf_commit: that tag's commit. -
limits: the defaultmax_size(bytes) andmax_depthofproto_decode().
Examples
zuproto_info()