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 (
Timestamp,Duration, wrappers), that scalar.- type
The message type's full name, such as
"pkg.Message".- pool
A pool from
proto_pool()holdingtype.- 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
proto_decode(unknown = "keep")in aproto_unknownattribute.- check_required
Whether a missing proto2
requiredfield is an error.- extra
"error"to refuse names the type does not define,"ignore"to skip them.
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).
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)
#> [1] 08 2a
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)
#> [1] "2026-10-25 12:00:00 UTC"