pub trait ShapeSerializer {
Show 24 methods
// Required methods
fn write_struct(
&mut self,
schema: &Schema<'_>,
value: &dyn SerializableStruct,
) -> Result<(), SerdeError>;
fn write_list(
&mut self,
schema: &Schema<'_>,
write_elements: &dyn Fn(&mut dyn ShapeSerializer) -> Result<(), SerdeError>,
) -> Result<(), SerdeError>;
fn write_map(
&mut self,
schema: &Schema<'_>,
write_entries: &dyn Fn(&mut dyn ShapeSerializer) -> Result<(), SerdeError>,
) -> Result<(), SerdeError>;
fn write_boolean(
&mut self,
schema: &Schema<'_>,
value: bool,
) -> Result<(), SerdeError>;
fn write_byte(
&mut self,
schema: &Schema<'_>,
value: i8,
) -> Result<(), SerdeError>;
fn write_short(
&mut self,
schema: &Schema<'_>,
value: i16,
) -> Result<(), SerdeError>;
fn write_integer(
&mut self,
schema: &Schema<'_>,
value: i32,
) -> Result<(), SerdeError>;
fn write_long(
&mut self,
schema: &Schema<'_>,
value: i64,
) -> Result<(), SerdeError>;
fn write_float(
&mut self,
schema: &Schema<'_>,
value: f32,
) -> Result<(), SerdeError>;
fn write_double(
&mut self,
schema: &Schema<'_>,
value: f64,
) -> Result<(), SerdeError>;
fn write_big_integer(
&mut self,
schema: &Schema<'_>,
value: &BigInteger,
) -> Result<(), SerdeError>;
fn write_big_decimal(
&mut self,
schema: &Schema<'_>,
value: &BigDecimal,
) -> Result<(), SerdeError>;
fn write_string(
&mut self,
schema: &Schema<'_>,
value: &str,
) -> Result<(), SerdeError>;
fn write_blob(
&mut self,
schema: &Schema<'_>,
value: Blob,
) -> Result<(), SerdeError>;
fn write_timestamp(
&mut self,
schema: &Schema<'_>,
value: &DateTime,
) -> Result<(), SerdeError>;
fn write_document(
&mut self,
schema: &Schema<'_>,
value: &Document,
) -> Result<(), SerdeError>;
fn write_null(&mut self, schema: &Schema<'_>) -> Result<(), SerdeError>;
// Provided methods
fn write_list_with_length(
&mut self,
schema: &Schema<'_>,
length: usize,
write_elements: &dyn Fn(&mut dyn ShapeSerializer) -> Result<(), SerdeError>,
) -> Result<(), SerdeError> { ... }
fn write_map_with_length(
&mut self,
schema: &Schema<'_>,
length: usize,
write_entries: &dyn Fn(&mut dyn ShapeSerializer) -> Result<(), SerdeError>,
) -> Result<(), SerdeError> { ... }
fn write_string_list(
&mut self,
schema: &Schema<'_>,
values: &[String],
) -> Result<(), SerdeError> { ... }
fn write_blob_list(
&mut self,
schema: &Schema<'_>,
values: &[Blob],
) -> Result<(), SerdeError> { ... }
fn write_integer_list(
&mut self,
schema: &Schema<'_>,
values: &[i32],
) -> Result<(), SerdeError> { ... }
fn write_long_list(
&mut self,
schema: &Schema<'_>,
values: &[i64],
) -> Result<(), SerdeError> { ... }
fn write_string_string_map(
&mut self,
schema: &Schema<'_>,
values: &HashMap<String, String>,
) -> Result<(), SerdeError> { ... }
}Expand description
Serializes Smithy shapes to a target format.
This trait provides a format-agnostic API for serializing the Smithy data model. Implementations serialize each data type to the corresponding encoding in their serial format (e.g., Smithy integers and floats to JSON numbers).
The serializer accepts a schema along with the value to provide additional information about how to serialize the value (e.g., timestamp format, JSON name).
This trait is object-safe so that generated SerializableStruct implementations
can use &mut dyn ShapeSerializer, producing one compiled serialize_members()
per shape regardless of how many codecs exist (shapes + codecs rather than
shapes * codecs in binary size).
§Example
let mut serializer = JsonSerializer::new();
serializer.write_string(&STRING_SCHEMA, "hello")?;Required Methods§
Sourcefn write_struct(
&mut self,
schema: &Schema<'_>,
value: &dyn SerializableStruct,
) -> Result<(), SerdeError>
fn write_struct( &mut self, schema: &Schema<'_>, value: &dyn SerializableStruct, ) -> Result<(), SerdeError>
Writes a structure to the serializer.
§Arguments
schema- The schema of the structure being serializedvalue- The structure to serialize
Sourcefn write_list(
&mut self,
schema: &Schema<'_>,
write_elements: &dyn Fn(&mut dyn ShapeSerializer) -> Result<(), SerdeError>,
) -> Result<(), SerdeError>
fn write_list( &mut self, schema: &Schema<'_>, write_elements: &dyn Fn(&mut dyn ShapeSerializer) -> Result<(), SerdeError>, ) -> Result<(), SerdeError>
Writes a list to the serializer.
§Arguments
schema- The schema of the list being serializedwrite_elements- Callback that writes the list elements
Sourcefn write_map(
&mut self,
schema: &Schema<'_>,
write_entries: &dyn Fn(&mut dyn ShapeSerializer) -> Result<(), SerdeError>,
) -> Result<(), SerdeError>
fn write_map( &mut self, schema: &Schema<'_>, write_entries: &dyn Fn(&mut dyn ShapeSerializer) -> Result<(), SerdeError>, ) -> Result<(), SerdeError>
Writes a map to the serializer.
§Arguments
schema- The schema of the map being serializedwrite_entries- Callback that writes the map entries
Sourcefn write_boolean(
&mut self,
schema: &Schema<'_>,
value: bool,
) -> Result<(), SerdeError>
fn write_boolean( &mut self, schema: &Schema<'_>, value: bool, ) -> Result<(), SerdeError>
Writes a boolean value.
Sourcefn write_byte(
&mut self,
schema: &Schema<'_>,
value: i8,
) -> Result<(), SerdeError>
fn write_byte( &mut self, schema: &Schema<'_>, value: i8, ) -> Result<(), SerdeError>
Writes a byte (i8) value.
Sourcefn write_short(
&mut self,
schema: &Schema<'_>,
value: i16,
) -> Result<(), SerdeError>
fn write_short( &mut self, schema: &Schema<'_>, value: i16, ) -> Result<(), SerdeError>
Writes a short (i16) value.
Sourcefn write_integer(
&mut self,
schema: &Schema<'_>,
value: i32,
) -> Result<(), SerdeError>
fn write_integer( &mut self, schema: &Schema<'_>, value: i32, ) -> Result<(), SerdeError>
Writes an integer (i32) value.
Sourcefn write_long(
&mut self,
schema: &Schema<'_>,
value: i64,
) -> Result<(), SerdeError>
fn write_long( &mut self, schema: &Schema<'_>, value: i64, ) -> Result<(), SerdeError>
Writes a long (i64) value.
Sourcefn write_float(
&mut self,
schema: &Schema<'_>,
value: f32,
) -> Result<(), SerdeError>
fn write_float( &mut self, schema: &Schema<'_>, value: f32, ) -> Result<(), SerdeError>
Writes a float (f32) value.
Sourcefn write_double(
&mut self,
schema: &Schema<'_>,
value: f64,
) -> Result<(), SerdeError>
fn write_double( &mut self, schema: &Schema<'_>, value: f64, ) -> Result<(), SerdeError>
Writes a double (f64) value.
Sourcefn write_big_integer(
&mut self,
schema: &Schema<'_>,
value: &BigInteger,
) -> Result<(), SerdeError>
fn write_big_integer( &mut self, schema: &Schema<'_>, value: &BigInteger, ) -> Result<(), SerdeError>
Writes a big integer value.
Sourcefn write_big_decimal(
&mut self,
schema: &Schema<'_>,
value: &BigDecimal,
) -> Result<(), SerdeError>
fn write_big_decimal( &mut self, schema: &Schema<'_>, value: &BigDecimal, ) -> Result<(), SerdeError>
Writes a big decimal value.
Sourcefn write_string(
&mut self,
schema: &Schema<'_>,
value: &str,
) -> Result<(), SerdeError>
fn write_string( &mut self, schema: &Schema<'_>, value: &str, ) -> Result<(), SerdeError>
Writes a string value.
Sourcefn write_blob(
&mut self,
schema: &Schema<'_>,
value: Blob,
) -> Result<(), SerdeError>
fn write_blob( &mut self, schema: &Schema<'_>, value: Blob, ) -> Result<(), SerdeError>
Writes a blob (byte array) value.
Takes an owned [Blob] rather than &[u8] so that an implementor which
needs to retain the bytes past this call can do so without copying
them. [Blob] wraps bytes::Bytes, so moving the parameter — or cloning
it — is a refcount operation rather than a payload copy.
This is load-bearing for the HTTP @httpPayload binding, which stores
the payload on the serializer and hands it to the request body after
serialization finishes. With a borrowed parameter there is no lifetime
relationship between value and self, so retaining it was only
possible by either copying the payload or unsoundly asserting a lifetime.
Callers that hold something other than a Blob convert cheaply:
Blob::from_maybe_shared(bytes) reuses an existing Bytes allocation,
and Blob::new(vec) takes ownership of a Vec<u8>. Generated client code
clones the Blob field of its data carrier, which is a refcount bump.
Note the deliberate asymmetry with Self::write_string and
Self::write_document, which stay borrowed. Blob is Bytes-backed,
so an owned parameter costs two atomics; String and Document have no
shared representation, so an owned parameter there would force a real
copy on every caller including the majority that never retain the value.
Do not “fix” the inconsistency.
Sourcefn write_timestamp(
&mut self,
schema: &Schema<'_>,
value: &DateTime,
) -> Result<(), SerdeError>
fn write_timestamp( &mut self, schema: &Schema<'_>, value: &DateTime, ) -> Result<(), SerdeError>
Writes a timestamp value.
Sourcefn write_document(
&mut self,
schema: &Schema<'_>,
value: &Document,
) -> Result<(), SerdeError>
fn write_document( &mut self, schema: &Schema<'_>, value: &Document, ) -> Result<(), SerdeError>
Writes a document value.
value is the [aws_smithy_types::Document] (fully
owned, no lifetime). Implementors clone the value into their
output representation.
Sourcefn write_null(&mut self, schema: &Schema<'_>) -> Result<(), SerdeError>
fn write_null(&mut self, schema: &Schema<'_>) -> Result<(), SerdeError>
Writes a null value (for sparse collections).
Provided Methods§
Sourcefn write_list_with_length(
&mut self,
schema: &Schema<'_>,
length: usize,
write_elements: &dyn Fn(&mut dyn ShapeSerializer) -> Result<(), SerdeError>,
) -> Result<(), SerdeError>
fn write_list_with_length( &mut self, schema: &Schema<'_>, length: usize, write_elements: &dyn Fn(&mut dyn ShapeSerializer) -> Result<(), SerdeError>, ) -> Result<(), SerdeError>
Writes a list with a known number of elements, including sparse nulls.
The callback must write exactly length elements. Formats that do not
need a length use the ordinary list implementation by default.
Sourcefn write_map_with_length(
&mut self,
schema: &Schema<'_>,
length: usize,
write_entries: &dyn Fn(&mut dyn ShapeSerializer) -> Result<(), SerdeError>,
) -> Result<(), SerdeError>
fn write_map_with_length( &mut self, schema: &Schema<'_>, length: usize, write_entries: &dyn Fn(&mut dyn ShapeSerializer) -> Result<(), SerdeError>, ) -> Result<(), SerdeError>
Writes a map with a known number of entries, including sparse null values.
The callback must write exactly length key/value pairs. Formats that
do not need a length use the ordinary map implementation by default.
Sourcefn write_string_list(
&mut self,
schema: &Schema<'_>,
values: &[String],
) -> Result<(), SerdeError>
fn write_string_list( &mut self, schema: &Schema<'_>, values: &[String], ) -> Result<(), SerdeError>
Writes a list of strings.
Sourcefn write_blob_list(
&mut self,
schema: &Schema<'_>,
values: &[Blob],
) -> Result<(), SerdeError>
fn write_blob_list( &mut self, schema: &Schema<'_>, values: &[Blob], ) -> Result<(), SerdeError>
Writes a list of blobs.
Sourcefn write_integer_list(
&mut self,
schema: &Schema<'_>,
values: &[i32],
) -> Result<(), SerdeError>
fn write_integer_list( &mut self, schema: &Schema<'_>, values: &[i32], ) -> Result<(), SerdeError>
Writes a list of integers.
Sourcefn write_long_list(
&mut self,
schema: &Schema<'_>,
values: &[i64],
) -> Result<(), SerdeError>
fn write_long_list( &mut self, schema: &Schema<'_>, values: &[i64], ) -> Result<(), SerdeError>
Writes a list of longs.
Sourcefn write_string_string_map(
&mut self,
schema: &Schema<'_>,
values: &HashMap<String, String>,
) -> Result<(), SerdeError>
fn write_string_string_map( &mut self, schema: &Schema<'_>, values: &HashMap<String, String>, ) -> Result<(), SerdeError>
Writes a map with string keys and string values.