Skip to content

Latest commit

 

History

History
171 lines (126 loc) · 4.57 KB

File metadata and controls

171 lines (126 loc) · 4.57 KB
title Cross-Language Serialization
sidebar_position 8
id cross_language
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.

Apache Fory™ supports seamless data exchange across multiple languages including Java, Python, C++, Go, and JavaScript.

Enable Cross-Language Mode

use fory::Fory;

// Enable cross-language mode
let mut fory = Fory::builder()
    .compatible(true)
    .xlang(true).build();

// Register types with consistent IDs across languages
fory.register::<MyStruct>(100);

// Or use namespace-based registration
fory.register_by_namespace::<MyStruct>("com.example", "MyStruct");

Type Registration for Cross-Language

Register by ID

For fast, compact serialization with consistent IDs across languages:

let mut fory = Fory::builder()
    .compatible(true)
    .xlang(true).build();

fory.register::<User>(100);  // Same ID in Java, Python, etc.

Register by Namespace

For more flexible type naming:

fory.register_by_namespace::<User>("com.example", "User");

Cross-Language Example

Rust (Serializer)

use fory::Fory;
use fory::ForyObject;

#[derive(ForyObject)]
struct Person {
    name: String,
    age: i32,
}

let mut fory = Fory::builder()
    .compatible(true)
    .xlang(true).build();

fory.register::<Person>(100);

let person = Person {
    name: "Alice".to_string(),
    age: 30,
};

let bytes = fory.serialize(&person)?;
// bytes can be deserialized by Java, Python, etc.

Java (Deserializer)

import org.apache.fory.*;
import org.apache.fory.config.*;

public class Person {
    public String name;
    public int age;
}

Fory fory = Fory.builder()
    .withLanguage(Language.XLANG)
    .withRefTracking(true)
    .build();

fory.register(Person.class, 100);  // Same ID as Rust

Person person = (Person) fory.deserialize(bytesFromRust);

Python (Deserializer)

import pyfory
from dataclasses import dataclass

@dataclass
class Person:
    name: str
    age: pyfory.int32

fory = pyfory.Fory(xlang=True, ref=True)
fory.register_type(Person, type_id=100)  # Same ID as Rust

person = fory.deserialize(bytes_from_rust)

Type Mapping

See xlang_type_mapping.md for complete type mapping across languages.

Common Type Mappings

Rust Java Python
i32 int int32
i64 long int64
f32 float float32
f64 double float64
Float16 Float16 float16
BFloat16 BFloat16 bfloat16
String String str
Vec<T> List<T> List[T]
Vec<Float16> Float16List float16array
Vec<BFloat16> BFloat16List bfloat16array
[Float16; N] Float16List float16array
[BFloat16; N] BFloat16List bfloat16array
HashMap<K,V> Map<K,V> Dict[K,V]
Option<T> nullable T Optional[T]

Best Practices

  1. Use consistent type IDs across all languages
  2. Enable compatible mode for schema evolution
  3. Register all types before serialization
  4. Test cross-language compatibility during development

See Also

Related Topics