Skip to content

Commit 4a3c71c

Browse files
authored
docs: update readme (#3575)
## Why? ## What does this PR do? ## Related issues ## AI Contribution Checklist - [ ] Substantial AI assistance was used in this PR: `yes` / `no` - [ ] If `yes`, I included a completed [AI Contribution Checklist](https://github.com/apache/fory/blob/main/AI_POLICY.md#9-contributor-checklist-for-ai-assisted-prs) in this PR description and the required `AI Usage Disclosure`. - [ ] If `yes`, my PR description includes the required `ai_review` summary and screenshot evidence of the final clean AI review results from both fresh reviewers on the current PR diff or current HEAD after the latest code changes. ## Does this PR introduce any user-facing change? - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark
1 parent 6d0faff commit 4a3c71c

5 files changed

Lines changed: 474 additions & 183 deletions

File tree

dart/README.md

Lines changed: 69 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,97 @@
11
# Apache Fory™ Dart
22

3-
## Overview
3+
Apache Fory™ Dart is the Dart xlang runtime for Apache Fory™. It reads and
4+
writes Fory's cross-language wire format and works in both Dart and Flutter
5+
applications. Because Flutter prohibits `dart:mirrors`, the runtime uses static
6+
code generation for type handling.
47

5-
This PR adds Dart language support to Apache Fory™, implementing a comprehensive serialization solution for Dart and Flutter applications. Apache Fory™ Dart consists of approximately 15,000 lines of code and provides an efficient serialization mechanism that works within Flutter's reflection limitations.
8+
The publishable package lives at `packages/fory/`. See its
9+
[README](packages/fory/README.md) for the full user-facing documentation
10+
including getting started, API reference, and code examples.
611

7-
## Implementation Approach
8-
9-
Dart supports reflection, but Flutter explicitly prohibits it. To address this constraint, Apache Fory™ Dart uses a combination of:
10-
11-
1. Core serialization/deserialization logic
12-
2. Static code generation for type handling
13-
14-
This approach ensures compatibility with Flutter while maintaining the performance and flexibility expected from Apache Fory™.
15-
16-
## Features
17-
18-
- XLANG mode support for cross-language serialization
19-
- Reference tracking for handling object graphs
20-
- Support for primitive types, collections, and custom classes
21-
- Serializer registration system
22-
- Code generation for class and enum serialization
23-
- Support for nested collections with automatic generic type conversion
24-
- Custom serializer registration
25-
- Support for using ByteWriter/ByteReader as serialization sources
26-
27-
## Usage Examples
12+
## Project Structure
2813

29-
### Basic Class Serialization
14+
| Directory | Description |
15+
| -------------------------------- | --------------------------------------- |
16+
| `packages/fory/lib/` | Core runtime and public API |
17+
| `packages/fory/lib/src/codegen/` | Build-runner code generator |
18+
| `packages/fory/example/` | Annotated example with generated output |
19+
| `packages/fory/test/` | Unit and integration tests |
20+
| `test/` | Cross-language integration tests |
21+
22+
## Type Mapping
23+
24+
| Fory xlang type | Dart type |
25+
| --------------- | ------------------------ |
26+
| bool | `bool` |
27+
| int8 | `fory.Int8` (wrapper) |
28+
| int16 | `fory.Int16` (wrapper) |
29+
| int32 | `fory.Int32` (wrapper) |
30+
| int64 | `int` |
31+
| float16 | `fory.Float16` (wrapper) |
32+
| float32 | `fory.Float32` (wrapper) |
33+
| float64 | `double` |
34+
| string | `String` |
35+
| binary | `Uint8List` |
36+
| local_date | `LocalDate` |
37+
| timestamp | `Timestamp` |
38+
| list | `List` |
39+
| set | `Set` |
40+
| map | `Map` |
41+
| enum | `enum` |
42+
| named_struct | `class` |
43+
| bool_array | `List<bool>` |
44+
| int8_array | `Int8List` |
45+
| int16_array | `Int16List` |
46+
| int32_array | `Int32List` |
47+
| int64_array | `Int64List` |
48+
| float32_array | `Float32List` |
49+
| float64_array | `Float64List` |
50+
51+
## Quick Start
52+
53+
Annotate your model and run the code generator:
3054

3155
```dart
3256
import 'package:fory/fory.dart';
3357
34-
part 'example.g.dart';
58+
part 'person.fory.dart';
3559
36-
@foryClass
37-
class SomeClass with _$SomeClassFory {
38-
late int id;
39-
late String name;
40-
late Map<String, double> map;
60+
@ForyStruct()
61+
class Person {
62+
Person();
4163
42-
SomeClass(this.id, this.name, this.map);
43-
44-
SomeClass.noArgs();
64+
String name = '';
65+
Int32 age = Int32(0);
4566
}
4667
```
4768

48-
After annotating your class with `@foryClass`, run:
49-
5069
```bash
51-
dart run build_runner build
70+
dart run build_runner build --delete-conflicting-outputs
5271
```
5372

54-
This generates the necessary code in `example.g.dart` and creates the `_$SomeClassFory` mixin.
55-
56-
### Serializing and Deserializing
73+
Serialize and deserialize:
5774

5875
```dart
59-
Fory fory = Fory(ref: true);
60-
fory.register($SomeClass, typename: "example.SomeClass");
61-
SomeClass obj = SomeClass(1, 'SomeClass', {'a': 1.0});
62-
63-
// Serialize
64-
Uint8List bytes = fory.serialize(obj);
76+
final fory = Fory();
77+
PersonFory.register(fory, Person, namespace: 'example', typeName: 'Person');
6578
66-
// Deserialize
67-
obj = fory.deserialize(bytes) as SomeClass;
79+
final bytes = fory.serialize(Person()..name = 'Ada'..age = Int32(36));
80+
final roundTrip = fory.deserialize<Person>(bytes);
6881
```
6982

70-
### Enum Serialization
71-
72-
```dart
73-
import 'package:fory/fory.dart';
83+
## Development
7484

75-
part 'example.g.dart';
85+
Run tests from the workspace root:
7686

77-
@foryEnum
78-
enum EnumFoo {
79-
A,
80-
B
81-
}
87+
```bash
88+
cd packages/fory
89+
dart test
8290
```
8391

84-
Registration is similar to classes:
92+
Run the code generator on the example:
8593

86-
```dart
87-
fory.register($EnumFoo, typename: "example.EnumFoo");
94+
```bash
95+
cd packages/fory
96+
dart run build_runner build --delete-conflicting-outputs
8897
```
89-
90-
## Type Support
91-
92-
Apache Fory™ Dart currently supports the following type mappings in XLANG mode:
93-
94-
| Fory Type | Dart Type |
95-
| ------------- | ------------------------------------------ |
96-
| bool | bool |
97-
| int8 | fory.Int8 |
98-
| int16 | fory.Int16 |
99-
| int32 | fory.Int32 |
100-
| var_int32 | fory.Int32 |
101-
| int64 | int |
102-
| var_int64 | int |
103-
| sli_int64 | int |
104-
| float32 | fory.Float32 |
105-
| float64 | double |
106-
| string | String |
107-
| enum | Enum |
108-
| named_enum | Enum |
109-
| named_struct | class |
110-
| list | List |
111-
| set | Set (LinkedHashSet, HashSet, SplayTreeSet) |
112-
| map | Map (LinkedHashMap, HashMap, SplayTreeMap) |
113-
| timestamp | fory.TimeStamp |
114-
| local_date | fory.LocalDate |
115-
| binary | Uint8List |
116-
| bool_array | BoolList |
117-
| int8_array | Int8List |
118-
| int16_array | Int16List |
119-
| int32_array | Int32List |
120-
| int64_array | Int64List |
121-
| float32_array | Float32List |
122-
| float64_array | Float64List |
123-
124-
## Project Structure
125-
126-
The implementation is organized into three main components:
127-
128-
1. **Codegen**: Located at `dart/packages/fory/lib/src/codegen`
129-
Handles static code generation for serialization/deserialization.
130-
131-
2. **ForyCore**: Located at `dart/packages/fory/lib/src`
132-
Contains the core serialization and deserialization logic.
133-
134-
3. **ForyTest**: Located at `dart/fory-test`
135-
Comprehensive test suite for Apache Fory™ Dart functionality.
136-
137-
## Testing Approach
138-
139-
The test suite is inspired by Apache Fory™ Java's testing approach and includes:
140-
141-
- **Data Type Tests**: Validates custom data types implemented for Dart
142-
- **Code Generation Tests**: Ensures correctness of the generated static code
143-
- **Buffer Tests**: Validates correct memory handling for primitive types
144-
- **Cross-Language Tests**: Tests functionality against other Apache Fory™ implementations
145-
- **Performance Tests**: Simple benchmarks for serialization/deserialization performance

dart/packages/fory/README.md

Lines changed: 77 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# Apache Fory Dart
1+
# Apache Fory Dart
22

3-
Apache Fory Dart is the Dart xlang runtime for Apache Fory. It reads and writes
4-
Fory's cross-language wire format and is designed around generated serializers
5-
for annotated Dart models, with customized serializers available for advanced use
3+
Apache Fory™ Dart is the Dart xlang runtime for
4+
[Apache Fory™](https://github.com/apache/fory). It reads and writes Fory's
5+
cross-language wire format and is designed around generated serializers for
6+
annotated Dart models, with customized serializers available for advanced use
67
cases.
78

89
## Features
@@ -21,7 +22,7 @@ Add `fory` to your package dependencies.
2122

2223
```yaml
2324
dependencies:
24-
fory: ^0.17.0-dev
25+
fory: ^0.17.0
2526

2627
dev_dependencies:
2728
build_runner: ^2.4.13
@@ -89,7 +90,9 @@ dart run build_runner build --delete-conflicting-outputs
8990

9091
## Type Registration
9192

92-
Generated types register through the generated library namespace.
93+
Generated types register through the generated library namespace. The namespace
94+
class is named `<FileName>Fory` based on the source file that contains the
95+
annotated types.
9396

9497
```dart
9598
PersonFory.register(fory, Person, id: 100);
@@ -115,8 +118,6 @@ Keep the same registration identity on all runtimes that exchange the type.
115118

116119
## Configuration
117120

118-
Configure the runtime through `Config`.
119-
120121
```dart
121122
final fory = Fory(
122123
compatible: true,
@@ -126,14 +127,13 @@ final fory = Fory(
126127
);
127128
```
128129

129-
Key options:
130-
131-
- `compatible`: enables compatible struct encoding and decoding
132-
- `checkStructVersion`: enables struct-version validation in
133-
schema-consistent mode
134-
- `maxDepth`: limits nesting depth for one operation
135-
- `maxCollectionSize`: limits collection and map payload sizes
136-
- `maxBinarySize`: limits binary payload size
130+
| Option | Default | Description |
131+
| -------------------- | ---------- | ------------------------------------------------------- |
132+
| `compatible` | `false` | Enables compatible struct encoding for schema evolution |
133+
| `checkStructVersion` | `true` | Validates struct version in schema-consistent mode |
134+
| `maxDepth` | `256` | Maximum nesting depth per operation |
135+
| `maxCollectionSize` | `1 << 20` | Maximum collection and map payload size |
136+
| `maxBinarySize` | `64 << 20` | Maximum binary payload size |
137137

138138
## Reference Tracking
139139

@@ -158,6 +158,18 @@ class NodeList {
158158
}
159159
```
160160

161+
## Field Annotations
162+
163+
`@ForyField()` controls per-field serialization behavior:
164+
165+
| Option | Description |
166+
| ---------- | ------------------------------------------------ |
167+
| `skip` | Skip the field during serialization |
168+
| `id` | Stable field ID for compatible-mode evolution |
169+
| `nullable` | Override nullability inference |
170+
| `ref` | Enable reference tracking for this field |
171+
| `dynamic` | Control whether runtime type metadata is written |
172+
161173
## Customized Serializers
162174

163175
Use `Serializer<T>` when a type cannot use generated struct support or when you
@@ -205,21 +217,57 @@ void main() {
205217
}
206218
```
207219

220+
## Type Mapping
221+
222+
Dart has no native fixed-width 8/16/32-bit integer or single-precision float
223+
types. Fory Dart provides thin wrapper types (`Int8`, `Int16`, `Int32`, `UInt8`,
224+
`UInt16`, `UInt32`, `Float16`, `Float32`) imported from `package:fory/fory.dart`
225+
to represent these xlang wire types.
226+
227+
| Fory xlang type | Dart type |
228+
| --------------- | ------------------------ |
229+
| bool | `bool` |
230+
| int8 | `fory.Int8` (wrapper) |
231+
| int16 | `fory.Int16` (wrapper) |
232+
| int32 | `fory.Int32` (wrapper) |
233+
| int64 | `int` |
234+
| float16 | `fory.Float16` (wrapper) |
235+
| float32 | `fory.Float32` (wrapper) |
236+
| float64 | `double` |
237+
| string | `String` |
238+
| binary | `Uint8List` |
239+
| local_date | `LocalDate` |
240+
| timestamp | `Timestamp` |
241+
| list | `List` |
242+
| set | `Set` |
243+
| map | `Map` |
244+
| enum | `enum` |
245+
| named_struct | `class` |
246+
| bool_array | `List<bool>` |
247+
| int8_array | `Int8List` |
248+
| int16_array | `Int16List` |
249+
| int32_array | `Int32List` |
250+
| int64_array | `Int64List` |
251+
| float32_array | `Float32List` |
252+
| float64_array | `Float64List` |
253+
208254
## Public API
209255

210256
The main exported API includes:
211257

212-
- `Fory`
213-
- `Config`
214-
- `Buffer`
215-
- `WriteContext`
216-
- `ReadContext`
217-
- `Serializer`
218-
- `UnionSerializer`
219-
- `ForyStruct`
220-
- `ForyField`
221-
- Numeric and temporal wrappers such as `Int8`, `Int16`, `Int32`, `UInt8`,
222-
`UInt16`, `UInt32`, `Float16`, `Float32`, `LocalDate`, and `Timestamp`
258+
- `Fory` — main serialization facade
259+
- `Config` — runtime configuration
260+
- `ForyStruct`, `ForyField` — struct annotations
261+
- `ForyUnion` — union type annotation
262+
- `Serializer`, `UnionSerializer`, `EnumSerializer` — serializer base classes
263+
- `Buffer`, `WriteContext`, `ReadContext` — low-level I/O
264+
- `TypeSpec`, `ListType`, `MapType`, `ValueType` — nested container type
265+
annotations
266+
- `Int32Type`, `Int64Type`, `Uint32Type`, `Uint64Type` — numeric encoding
267+
overrides
268+
- Numeric wrappers: `Int8`, `Int16`, `Int32`, `UInt8`, `UInt16`, `UInt32`,
269+
`Float16`, `Float32`
270+
- Temporal wrappers: `LocalDate`, `Timestamp`
223271

224272
## Cross-Language Notes
225273

@@ -230,5 +278,5 @@ The main exported API includes:
230278
- Use wrappers or numeric field annotations when the exact xlang wire type
231279
matters.
232280

233-
For the xlang wire format and type mapping details, see the Apache Fory
234-
specification in the main repository.
281+
For the xlang wire format and type mapping details, see the
282+
[Apache Fory specification](https://github.com/apache/fory/tree/main/docs/specification).

0 commit comments

Comments
 (0)