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:
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}")
}1005
pub(api) interface DecoderС версии. 1.0.0-beta
Методы
isJson | () -> boolWhether the input is JSON. |
readI64 | () -> i64 | DecodeErrorReads a signed integer of up to 64 bits. |
readU64 | () -> u64 | DecodeErrorReads an unsigned integer of up to 64 bits. |
readF64 | () -> f64 | DecodeErrorReads a floating point number. |
readBool | () -> bool | DecodeErrorReads a boolean. |
readStr | () -> str | DecodeErrorReads a text. |
readBytes | () -> bytes | DecodeErrorReads a byte string. |
beginMessage | () -> () | DecodeErrorStarts reading a message; nextField then yields its fields until none. |
nextField | () -> FieldHeader? | DecodeErrorThe next field of the current message, none at its end. |
endMessage | ()Ends reading the message that beginMessage started. |
beginList | () -> usize | DecodeErrorD-8344. The reader of Encoder.beginList(len) / endList() |
endList | ()Ends reading the list that beginList started, after its elements were read. |
beginMap | () -> usize | DecodeErrorThe reader of beginMap(len) / endMap() |
endMap | ()Ends reading the map that beginMap started, after its entries were read. |
readNull | () -> bool | DecodeErrorThe reader of writeNull() |
Определено в: std/ser/ser.oxl:1462