|
| 1 | +--- |
| 2 | +title: Basic Serialization |
| 3 | +sidebar_position: 1 |
| 4 | +id: basic_serialization |
| 5 | +license: | |
| 6 | + Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + contributor license agreements. See the NOTICE file distributed with |
| 8 | + this work for additional information regarding copyright ownership. |
| 9 | + The ASF licenses this file to You under the Apache License, Version 2.0 |
| 10 | + (the "License"); you may not use this file except in compliance with |
| 11 | + the License. You may obtain a copy of the License at |
| 12 | +
|
| 13 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +
|
| 15 | + Unless required by applicable law or agreed to in writing, software |
| 16 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + See the License for the specific language governing permissions and |
| 19 | + limitations under the License. |
| 20 | +--- |
| 21 | + |
| 22 | +This guide covers the core serialization flow in Apache Fory JavaScript. |
| 23 | + |
| 24 | +## Create and Reuse a `Fory` Instance |
| 25 | + |
| 26 | +```ts |
| 27 | +import Fory from "@apache-fory/core"; |
| 28 | + |
| 29 | +const fory = new Fory(); |
| 30 | +``` |
| 31 | + |
| 32 | +Create one instance, register your schemas, and reuse it. A `Fory` instance caches generated serializers and type metadata, so recreating it for every request adds unnecessary overhead. |
| 33 | + |
| 34 | +## Define a Schema with `Type.struct` |
| 35 | + |
| 36 | +The most common path is to define a schema and register it. |
| 37 | + |
| 38 | +```ts |
| 39 | +import Fory, { Type } from "@apache-fory/core"; |
| 40 | + |
| 41 | +const accountType = Type.struct( |
| 42 | + { typeName: "example.account" }, |
| 43 | + { |
| 44 | + id: Type.int64(), |
| 45 | + owner: Type.string(), |
| 46 | + active: Type.bool(), |
| 47 | + nickname: Type.string().setNullable(true), |
| 48 | + }, |
| 49 | +); |
| 50 | + |
| 51 | +const fory = new Fory(); |
| 52 | +const { serialize, deserialize } = fory.register(accountType); |
| 53 | +``` |
| 54 | + |
| 55 | +## Serialize and Deserialize |
| 56 | + |
| 57 | +```ts |
| 58 | +const bytes = serialize({ |
| 59 | + id: 42n, |
| 60 | + owner: "Alice", |
| 61 | + active: true, |
| 62 | + nickname: null, |
| 63 | +}); |
| 64 | + |
| 65 | +const value = deserialize(bytes); |
| 66 | +console.log(value); |
| 67 | +// { id: 42n, owner: 'Alice', active: true, nickname: null } |
| 68 | +``` |
| 69 | + |
| 70 | +The returned `bytes` value is a `Uint8Array`/platform buffer and can be sent over the network or written to storage. |
| 71 | + |
| 72 | +## Root-Level Dynamic Serialization |
| 73 | + |
| 74 | +`Fory` can also serialize dynamic root values without first binding a schema-specific serializer. |
| 75 | + |
| 76 | +```ts |
| 77 | +const fory = new Fory(); |
| 78 | + |
| 79 | +const bytes = fory.serialize( |
| 80 | + new Map([ |
| 81 | + ["name", "Alice"], |
| 82 | + ["age", 30], |
| 83 | + ]), |
| 84 | +); |
| 85 | + |
| 86 | +const value = fory.deserialize(bytes); |
| 87 | +``` |
| 88 | + |
| 89 | +This is convenient for dynamic payloads, but explicit schemas are usually better for stable interfaces and cross-language contracts. |
| 90 | + |
| 91 | +## Primitive Values |
| 92 | + |
| 93 | +```ts |
| 94 | +const fory = new Fory(); |
| 95 | + |
| 96 | +fory.deserialize(fory.serialize(true)); |
| 97 | +// true |
| 98 | + |
| 99 | +fory.deserialize(fory.serialize("hello")); |
| 100 | +// 'hello' |
| 101 | + |
| 102 | +fory.deserialize(fory.serialize(123)); |
| 103 | +// 123 |
| 104 | + |
| 105 | +fory.deserialize(fory.serialize(123n)); |
| 106 | +// 123n |
| 107 | + |
| 108 | +fory.deserialize(fory.serialize(new Date("2021-10-20T09:13:00Z"))); |
| 109 | +// Date |
| 110 | +``` |
| 111 | + |
| 112 | +### Number and `bigint` behavior |
| 113 | + |
| 114 | +JavaScript has both `number` and `bigint`, but xlang distinguishes between 32-bit, 64-bit, floating-point, and tagged integer representations. For any cross-language or long-lived contract, prefer explicit field types in schemas instead of depending on dynamic root-type inference. |
| 115 | + |
| 116 | +- use `Type.int32()` for 32-bit integers |
| 117 | +- use `Type.int64()` for 64-bit integers and pass `bigint` |
| 118 | +- use `Type.float32()` or `Type.float64()` for floating-point values |
| 119 | + |
| 120 | +Dynamic root serialization is convenient, but the exact heuristic for whether a value comes back as `number` or `bigint` should not be treated as a stable API contract. |
| 121 | + |
| 122 | +## Arrays, Maps, and Sets |
| 123 | + |
| 124 | +```ts |
| 125 | +const inventoryType = Type.struct("example.inventory", { |
| 126 | + tags: Type.array(Type.string()), |
| 127 | + counts: Type.map(Type.string(), Type.int32()), |
| 128 | + labels: Type.set(Type.string()), |
| 129 | +}); |
| 130 | + |
| 131 | +const fory = new Fory({ ref: true }); |
| 132 | +const { serialize, deserialize } = fory.register(inventoryType); |
| 133 | + |
| 134 | +const bytes = serialize({ |
| 135 | + tags: ["hot", "new"], |
| 136 | + counts: new Map([ |
| 137 | + ["apple", 3], |
| 138 | + ["pear", 8], |
| 139 | + ]), |
| 140 | + labels: new Set(["featured", "seasonal"]), |
| 141 | +}); |
| 142 | + |
| 143 | +const value = deserialize(bytes); |
| 144 | +``` |
| 145 | + |
| 146 | +## Nested Structs |
| 147 | + |
| 148 | +```ts |
| 149 | +const addressType = Type.struct("example.address", { |
| 150 | + city: Type.string(), |
| 151 | + country: Type.string(), |
| 152 | +}); |
| 153 | + |
| 154 | +const userType = Type.struct("example.user", { |
| 155 | + name: Type.string(), |
| 156 | + address: Type.struct("example.address", { |
| 157 | + city: Type.string(), |
| 158 | + country: Type.string(), |
| 159 | + }), |
| 160 | +}); |
| 161 | + |
| 162 | +const fory = new Fory(); |
| 163 | +const { serialize, deserialize } = fory.register(userType); |
| 164 | + |
| 165 | +const bytes = serialize({ |
| 166 | + name: "Alice", |
| 167 | + address: { city: "Hangzhou", country: "CN" }, |
| 168 | +}); |
| 169 | + |
| 170 | +const user = deserialize(bytes); |
| 171 | +``` |
| 172 | + |
| 173 | +If a nested value can be missing, mark it nullable: |
| 174 | + |
| 175 | +```ts |
| 176 | +const wrapperType = Type.struct("example.wrapper", { |
| 177 | + child: Type.struct("example.child", { |
| 178 | + name: Type.string(), |
| 179 | + }).setNullable(true), |
| 180 | +}); |
| 181 | +``` |
| 182 | + |
| 183 | +## Decorator-Based Registration |
| 184 | + |
| 185 | +TypeScript decorators are also supported. |
| 186 | + |
| 187 | +```ts |
| 188 | +import Fory, { Type } from "@apache-fory/core"; |
| 189 | + |
| 190 | +@Type.struct("example.user") |
| 191 | +class User { |
| 192 | + @Type.int64() |
| 193 | + id!: bigint; |
| 194 | + |
| 195 | + @Type.string() |
| 196 | + name!: string; |
| 197 | +} |
| 198 | + |
| 199 | +const fory = new Fory(); |
| 200 | +const { serialize, deserialize } = fory.register(User); |
| 201 | + |
| 202 | +const user = new User(); |
| 203 | +user.id = 1n; |
| 204 | +user.name = "Alice"; |
| 205 | + |
| 206 | +const copy = deserialize(serialize(user)); |
| 207 | +console.log(copy instanceof User); // true |
| 208 | +``` |
| 209 | + |
| 210 | +## Nullability |
| 211 | + |
| 212 | +Field nullability is explicit in schema-based structs. |
| 213 | + |
| 214 | +```ts |
| 215 | +const nullableType = Type.struct("example.optional_user", { |
| 216 | + name: Type.string(), |
| 217 | + email: Type.string().setNullable(true), |
| 218 | +}); |
| 219 | +``` |
| 220 | + |
| 221 | +If a field is not marked nullable and you try to write `null`, serialization throws. |
| 222 | + |
| 223 | +## Debugging Generated Code |
| 224 | + |
| 225 | +You can inspect generated serializer code with `hooks.afterCodeGenerated`. |
| 226 | + |
| 227 | +```ts |
| 228 | +const fory = new Fory({ |
| 229 | + hooks: { |
| 230 | + afterCodeGenerated(code) { |
| 231 | + console.log(code); |
| 232 | + return code; |
| 233 | + }, |
| 234 | + }, |
| 235 | +}); |
| 236 | +``` |
| 237 | + |
| 238 | +This is useful when debugging schema behavior, field ordering, or generated fast paths. |
| 239 | + |
| 240 | +## Related Topics |
| 241 | + |
| 242 | +- [Type Registration](type-registration.md) |
| 243 | +- [Supported Types](supported-types.md) |
| 244 | +- [References](references.md) |
0 commit comments