title: Dart Serialization Guide sidebar_position: 0 id: dart_serialization_index 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
Apache Fory™ Dart lets you serialize Dart objects to bytes and deserialize them back — including across services written in Java, Go, C#, Python, and other Fory-supported languages.
build_runner (generates the serializer code)Add the dependency to your pubspec.yaml:
dependencies: fory: ^0.17.0 dev_dependencies: build_runner: ^2.4.0
Define your model, run the generator once, then serialize:
import 'package:fory/fory.dart'; part 'person.fory.dart'; enum Color { red, blue, } @ForyStruct() class Person { Person(); String name = ''; Int32 age = Int32(0); Color favoriteColor = Color.red; List<String> tags = <String>[]; } void main() { final fory = Fory(); PersonFory.register( fory, Color, namespace: 'example', typeName: 'Color', ); PersonFory.register( fory, Person, namespace: 'example', typeName: 'Person', ); final person = Person() ..name = 'Ada' ..age = Int32(36) ..favoriteColor = Color.blue ..tags = <String>['engineer', 'mathematician']; final bytes = fory.serialize(person); final roundTrip = fory.deserialize<Person>(bytes); print(roundTrip.name); }
Generate the companion file before running the program:
dart run build_runner build --delete-conflicting-outputs
PersonFory is generated by build_runner. The namespace and typeName values are how peers in other languages identify the same type — keep them stable once your service is in production.
Fory(...) — create a serializer instance; create once and reuse itfory.serialize(value) — returns Uint8List bytesfory.deserialize<T>(bytes) — returns a T@ForyStruct() — marks a class for code generation@ForyField(...) — per-field options (skip, ID, nullability, references)Int8, Int16, Int32, Int64, Uint8, Uint16, Uint32, Uint64Float16, Bfloat16, Float32Float16List, Bfloat16ListLocalDate, Timestamp, Duration| Topic | Description |
|---|---|
| Configuration | Runtime options, compatible mode, and safety limits |
| Basic Serialization | serialize, deserialize, generated registration, root graphs |
| Code Generation | @ForyStruct, build runner, and generated namespaces |
| Type Registration | ID-based vs name-based registration and registration rules |
| Custom Serializers | Manual Serializer<T> implementations and unions |
| Field Configuration | @ForyField, field IDs, nullability, references, polymorphism |
| Supported Types | Built-in xlang values, wrappers, collections, and structs |
| Schema Evolution | Compatible structs and evolving schemas |
| Cross-Language | Interoperability rules and field alignment |
| Web Platform Support | Dart VM/AOT, Flutter, and web support, limits, and validation |
| Troubleshooting | Common errors, diagnostics, and validation steps |