OxL Reference

interface Decoder

The reader of a format (§9.5), handed to Codec.decode and returned by Format.decoder. Written as a type it means dyn Decoder. The defaults report DecodeError.Malformed. decode<T>(bytes, MyFormat) (D-8344) walks the type T and pulls what it needs from the decoder in the order encode would have pushed it to the encoder: scalars with readI64 / readU64 (integers up to 64 bits, char, Duration, Date, LocalDateTime) / readF64 / readBool / readStr (str, enums without fields, Time, ActorRef) / readBytes (bytes, 128-bit integers: 16 bytes); a message (data, tuple, enum with fields, union with several members): beginMessage(), nextField() until it returns none (a field is found by its tag, or by its name when the tag is 0; an unknown field is an error), endMessage() - a nested message is read in the middle of its field; an enum with fields or a union is a message with one field (the variant / member); T?: readNull() first; lists, sets, arrays: beginList(), that many values, endList(); maps: beginMap(), key and value for each entry, endMap().

Example:

oxl✓ checked in CI
use std.ser { Codec, Encoder, Decoder, Schema, encode, decode, hex, EncodeError, DecodeError }

data Plus {}

impl Codec<i64> for Plus {
  static fn encode(x: &i64, w: &var Encoder) -> () | EncodeError { w.writeI64(x) }
  static fn decode(r: &var Decoder) -> i64 | DecodeError { r.readI64()? + 1000 }
  static fn schema() -> Schema => Schema.of("int64")
}

data Wrap {
  @codec(Plus) n: i64
}

fn main() io {
  let b = encode(&Wrap{ n: 5 })!
  print("{decode<Wrap>(&b)!.n}")
}
Output
1005
pub(api) interface Decoder

Since. 1.0.0-beta

Methods

isJson() -> bool
Whether the input is JSON.
readI64() -> i64 | DecodeError
Reads a signed integer of up to 64 bits.
readU64() -> u64 | DecodeError
Reads an unsigned integer of up to 64 bits.
readF64() -> f64 | DecodeError
Reads a floating point number.
readBool() -> bool | DecodeError
Reads a boolean.
readStr() -> str | DecodeError
Reads a text.
readBytes() -> bytes | DecodeError
Reads a byte string.
beginMessage() -> () | DecodeError
Starts reading a message; nextField then yields its fields until none.
nextField() -> FieldHeader? | DecodeError
The next field of the current message, none at its end.
endMessage()
Ends reading the message that beginMessage started.
beginList() -> usize | DecodeError
D-8344. The reader of Encoder.beginList(len) / endList()
endList()
Ends reading the list that beginList started, after its elements were read.
beginMap() -> usize | DecodeError
The reader of beginMap(len) / endMap()
endMap()
Ends reading the map that beginMap started, after its entries were read.
readNull() -> bool | DecodeError
The reader of writeNull()

Defined in: std/ser/ser.oxl:1462

See also