Skip to content

Examples

Binary datum

from avro import encode, decode, WireWriter
from manual_types import Message

var m = Message()
m.f_bool = True
m.f_int32 = 150
m.f_string = String("hi")
var bytes = encode(m)
var back = decode[Message](bytes)

GenericRecord

from avro import GenericDatum, parse_avsc

var pool = parse_avsc(String('{"type":"record","name":"M","fields":[{"name":"x","type":"int"}]}'))
var g = GenericDatum(pool^)
# fill nodes, then:
var buf = g.encode()

Object Container File

write_ocf writes magic Obj1, the schema, a sync marker, and one block per object. Codec 0 is null. Codec 1 is raw DEFLATE.

Single-object encoding

encode_single_object writes C3 01, the CRC-64-AVRO fingerprint, and the binary datum. decode_single_object checks the header and decodes the payload. A second argument is the writer schema JSON when the reader type differs.

Resolution

named_match / can_resolve implement writer/reader rules: unqualified names, reader aliases, promotions. decode_resolving decodes writer bytes then convert_to[T].

Avro JSON

encode_json / decode_json are the official Avro JSON encoding. Union non-null branches wrap as {"shortName": value}. decode_default is the default-only subset (union = first branch, no wrapper).