// Serialization (std.ser, ยง9): the binary format (protobuf 3 wire encoding), canonical JSON, schema evolution. use std.ser { encode, decode, hex, toJson, fromJson, EncodeError, DecodeError } enum Status { New = 1, Paid = 2, Shipped = 3 } data Item { sku: str qty: i32 } // version 1 of an order data OrderV1 { id: i64 status: Status items: List } // version 2 adds an optional field and a field with a default: old readers skip them, old writers need not send them data OrderV2 { id: i64 status: Status items: List note: str? priority: i32 = 3 } fn main() io { let order = OrderV1{ id: 7, status: Status.Paid, items: [Item{ sku: "a-1", qty: 2 }] } match encode(&order) { bytes wire => { print("wire: {hex(&wire)}") match decode(&wire) { OrderV2 v2 => print("as v2: id={v2.id} note={v2.note} priority={v2.priority}") DecodeError e => print("decode error {e}") } } EncodeError e => print("encode error {e}") } match toJson(&order) { str json => { print("json: {json}") match fromJson(json) { OrderV1 back => print("back: {back.items.len()} item(s), status {back.status}") DecodeError e => print("decode error {e}") } } EncodeError e => print("encode error {e}") } }