| 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 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
This page covers the Java xlang quickstart. Xlang mode is the default Java wire format and is the right first choice for cross-language payloads.
For a single-threaded xlang runtime, set the mode explicitly:
import org.apache.fory.Fory;
Fory fory = Fory.builder()
.withXlang(true)
.requireClassRegistration(true)
.build();For a thread-safe runtime, build ThreadSafeFory from the same builder:
import org.apache.fory.ThreadSafeFory;
ThreadSafeFory fory = Fory.builder()
.withXlang(true)
.requireClassRegistration(true)
.buildThreadSafeFory();Default Java xlang mode also defaults to compatible schema mode, so independently deployed services
can add and remove fields when their schema metadata remains compatible. Use
withCompatible(false) only when every peer updates schema together and you want schema-consistent
xlang payloads.
Register application classes with the same type identity on every peer. Numeric IDs are compact and fast, while namespace/type-name registration is easier to coordinate across independently owned services.
import org.apache.fory.annotation.ForyField;
public class User {
@ForyField(id = 0)
public String name;
@ForyField(id = 1)
public int age;
}
Fory fory = Fory.builder()
.withXlang(true)
.requireClassRegistration(true)
.build();
fory.register(User.class, "example", "User");Use field IDs for long-lived schemas so field identity is stable even if Java field names change. See Schema Metadata for Java annotations, nullability, reference tracking, and enum metadata.
User user = new User();
user.name = "Alice";
user.age = 30;
byte[] bytes = fory.serialize(user);
User decoded = fory.deserialize(bytes, User.class);When xlang bytes cross runtimes, every runtime must register the same type identity and compatible field metadata. The shared rules live in Xlang, while Java-specific API calls are in Cross-Language.
For same-language Java/JVM traffic, native mode is usually the better fit:
Fory fory = Fory.builder()
.withXlang(false)
.build();Native mode supports the broad Java object serialization surface, including JDK serialization hooks, object copy, and native-mode zero-copy buffers. See Native Mode.
withRefTracking(true)preserves shared references and circular references.requireClassRegistration(true)keeps the default registered-type policy.withCompatible(true)enables compatible mode explicitly for native-mode schema evolution. Xlang mode already uses compatible mode by default.withAsyncCompilation(true)enables asynchronous serializer compilation where supported.
- Reuse Fory instances: Creating Fory is expensive, always reuse instances
- Use appropriate thread safety: Choose between single-thread and thread-safe based on your needs
- Register classes: Keep type identity stable across every xlang peer
- Configure reference tracking: Enable it only when the object graph needs identity or cycles
- Configuration - All ForyBuilder options
- Native Mode - Java-only serialization features
- Schema Metadata - Field IDs, nullability, reference tracking, and enum IDs
- Cross-Language - Java xlang interoperability
- Troubleshooting - Common API usage issues