title: Basic Serialization sidebar_position: 1 id: basic_serialization license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
This page covers basic object graph serialization and supported types.
Apache Fory™ provides automatic serialization of complex object graphs, preserving the structure and relationships between objects. The #[derive(ForyObject)] macro generates efficient serialization code at compile time, eliminating runtime overhead.
Key capabilities:
Option<T>use fory::{Fory, Error}; use fory::ForyObject; use std::collections::HashMap; #[derive(ForyObject, Debug, PartialEq)] struct Person { name: String, age: i32, address: Address, hobbies: Vec<String>, metadata: HashMap<String, String>, } #[derive(ForyObject, Debug, PartialEq)] struct Address { street: String, city: String, country: String, } let mut fory = Fory::default(); fory.register::<Address>(100); fory.register::<Person>(200); let person = Person { name: "John Doe".to_string(), age: 30, address: Address { street: "123 Main St".to_string(), city: "New York".to_string(), country: "USA".to_string(), }, hobbies: vec!["reading".to_string(), "coding".to_string()], metadata: HashMap::from([ ("role".to_string(), "developer".to_string()), ]), }; let bytes = fory.serialize(&person); let decoded: Person = fory.deserialize(&bytes)?; assert_eq!(person, decoded);
| Rust Type | Description |
|---|---|
bool | Boolean |
i8, i16, i32, i64 | Signed integers |
f32, f64 | Floating point |
BFloat16 | 16-bit brain floating point |
String | UTF-8 string |
| Rust Type | Description |
|---|---|
Vec<T> | Dynamic array |
VecDeque<T> | Double-ended queue |
LinkedList<T> | Doubly-linked list |
HashMap<K, V> | Hash map |
BTreeMap<K, V> | Ordered map |
HashSet<T> | Hash set |
BTreeSet<T> | Ordered set |
BinaryHeap<T> | Binary heap |
Option<T> | Optional value |
Vec<BFloat16> is the dense xlang carrier for bfloat16_array payloads.
| Rust Type | Description |
|---|---|
Box<T> | Heap allocation |
Rc<T> | Reference counting (shared refs tracked) |
Arc<T> | Thread-safe reference counting (shared refs tracked) |
RcWeak<T> | Weak reference to Rc<T> (breaks circular refs) |
ArcWeak<T> | Weak reference to Arc<T> (breaks circular refs) |
RefCell<T> | Interior mutability (runtime borrow checking) |
Mutex<T> | Thread-safe interior mutability |
| Rust Type | Description |
|---|---|
chrono::NaiveDate | Date without timezone |
chrono::NaiveDateTime | Timestamp without timezone |
| Macro | Description |
|---|---|
#[derive(ForyObject)] | Object graph serialization |
#[derive(ForyRow)] | Row-based serialization |
use fory::{Fory, Reader}; let mut fory = Fory::default(); fory.register::<MyStruct>(1)?; let obj = MyStruct { /* ... */ }; // Basic serialize/deserialize let bytes = fory.serialize(&obj)?; let decoded: MyStruct = fory.deserialize(&bytes)?; // Serialize to existing buffer let mut buf: Vec<u8> = vec![]; fory.serialize_to(&mut buf, &obj)?; // Deserialize from reader let mut reader = Reader::new(&buf); let decoded: MyStruct = fory.deserialize_from(&mut reader)?;