std::serializer::serialize
Serializer Trait
Shared format-agnostic record serialization contract.
Overview
Serialize is implemented by records that know how to write their own fields
into a caller-provided String using a caller-provided Serializer::Writer.
The trait does not choose a format; JSON, TOML, or another front-end supplies
the writer configuration.
Contract
Implementations should:
- Append output to the provided
Stringwithout clearing it. - Call
Serializer::beginRecordbefore emitting fields. - Call
Serializer::writeFieldbefore each field value. - Pass
first = trueonly for the first field. - Use
Serializer::writeor nestedSerialize::serializefor field values. - Propagate
Serializer::Errorwith!when a helper fails. - Call
Serializer::endRecordbefore returning success.
import Serialize, Serializer from "std::serializer";
record Package {
name: String;
public serialize(&self, output: &mut String, writer: &Serializer::Writer): Result<boolean, Serializer::Error> {
Serializer::beginRecord(output, writer)!;
Serializer::writeField(output, "name", writer, true)!;
Serializer::write(output, &self.name, writer)!;
Serializer::endRecord(output, writer)!;
return Result::OK(true);
}
}
Traits
trait SerializeFormat-agnostic serialization contract for record-like values.
Members
serialize(&self, writer: &Writer): Result<boolean, Error>Appends a serialized representation of self to output.
Arguments
output: destination string to append to.writer: format settings supplied by the concrete front-end.
Returns
Result::OK(true) on success, or Serializer::Error when the selected
writer rejects a field key or root shape.