MCPcopy Create free account
hub / github.com/apache/fory / serialize

Method serialize

rust/fory-core/src/fory.rs:556–570  ·  view source on GitHub ↗

Serializes a value of type `T` into a byte vector. # Type Parameters `T` - The type of the value to serialize. Must implement `Serializer`. # Arguments `record` - A reference to the value to serialize. # Returns A `Vec ` containing the serialized data. # Examples ```rust, ignore use fory::Fory; use fory::{ForyEnum, ForyStruct, ForyUnion}; #[derive(ForyStruct)] struct Point { x: i32, y:

(&self, record: &T)

Source from the content-addressed store, hash-verified

554 /// let bytes = fory.serialize(&point).unwrap();
555 /// ```
556 pub fn serialize<T: Serializer>(&self, record: &T) -> Result<Vec<u8>, Error> {
557 self.with_write_context(
558 |context| match self.serialize_with_context(record, context) {
559 Ok(_) => {
560 let result = context.writer.dump();
561 context.writer.reset();
562 Ok(result)
563 }
564 Err(err) => {
565 context.writer.reset();
566 Err(err)
567 }
568 },
569 )
570 }
571
572 /// Serializes a value of type `T` into the provided byte buffer.
573 ///

Calls 4

with_write_contextMethod · 0.80
resetMethod · 0.65
dumpMethod · 0.45