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
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 Xlang Serialization.
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 Serialization.
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.