title: Dart Object Serialization sidebar_position: 0 id: 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, Python, C++, Go, Rust, JavaScript/TypeScript, C#, Swift, Scala, Kotlin, and other Fory-supported languages.
build_runner (generates the serializer code)Add the dependency to your pubspec.yaml:
dependencies: fory: ^1.6.1 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 = ''; @ForyField(type: Int32Type()) int age = 0; Color favoriteColor = Color.red; List<String> tags = <String>[]; } void main() { final fory = Fory(); PersonForyModule.register( fory, Color, name: 'example.Color', ); PersonForyModule.register( fory, Person, name: 'example.Person', ); final person = Person() ..name = 'Ada' ..age = 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
PersonForyModule is generated by build_runner. The name value is how peers in other languages identify the same type; keep it stable once your service is in production. Use . inside name to add a namespace prefix.
An ordinary annotated class includes its concrete superclass and applied-mixin storage. Public and same-library private inherited fields need no parent annotation. See Struct Inheritance for private fields, constructors, mixins, and field inclusion options.
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@ForyStruct(exposePrivateFields: true) — lets another library's generated child serializer access private state owned by this library@ForyStruct(ignoreInheritedPrivateFields: true) — omits all private superclass and applied-mixin storage from this concrete child's schema@ForyStruct(target: Type) — generates an external structural serializer@ForyField(...) — per-field options and canonical type: overrides@ListField(...), @SetField(...), @MapField(...) — container sugar for nested type: treesInt64, Uint64, Float32double with Float16Type or Bfloat16TypeFloat16List, Bfloat16ListLocalDate, Timestamp, Duration| Topic | Description |
|---|---|
| Configuration | Fory options, compatible mode, and safety limits |
| Basic Serialization | Default xlang APIs, registration, and interoperability |
| Code Generation | @ForyStruct, build runner, and generated modules |
| Struct Inheritance | Superclasses, mixins, private fields, and constructors |
| External-Type Serialization | Generated serializers for classes owned by another package |
| Schema Metadata | @ForyField, field IDs, nullability, references, polymorphism |
| Type Registration | ID-based vs name-based registration and registration rules |
| Custom Serializers | Custom Serializer<T> implementations and unions |
| Supported Types | Built-in xlang values, wrappers, collections, and structs |
| Schema Evolution | Compatible structs and evolving schemas |
| Web Platform Support | Dart VM/AOT, Flutter, and web support, limits, and validation |
| gRPC Support | Generated Fory-backed gRPC service companions |
| Troubleshooting | Common errors, diagnostics, and validation steps |
Before decoding bytes from outside the application trust boundary, read Dart Security.