跳到主要内容
版本:dev

Cross-Language Serialization

Fory Swift can exchange payloads with other Fory runtimes using the xlang protocol.

let fory = Fory(xlang: true, trackRef: false, compatible: true)

Register Types with Shared Identity

ID-based registration

@ForyObject
struct Order {
var id: Int64 = 0
var amount: Double = 0
}

let fory = Fory(xlang: true, compatible: true)
fory.register(Order.self, id: 100)

Name-based registration

try fory.register(Order.self, namespace: "com.example", name: "Order")

Cross-language Rules

  • Keep type registration mapping consistent across languages
  • Use compatible mode when independently evolving schemas
  • Register all user-defined concrete types used by dynamic fields (Any, any Serializer)

Swift IDL Workflow

Generate Swift models directly from Fory IDL/Proto/FBS inputs:

foryc schema.fdl --swift_out ./Sources/Generated

Generated Swift code includes:

  • @ForyObject models and @ForyField(id: ...) metadata
  • Tagged union enums (associated-value enum cases)
  • ForyRegistration.register(_:) helpers with transitive import registration
  • toBytes / fromBytes helpers on generated types

Use generated registration before cross-language serialization:

let fory = Fory(xlang: true, trackRef: true, compatible: true)
try Addressbook.ForyRegistration.register(fory)

let payload = try fory.serialize(book)
let decoded: Addressbook.AddressBook = try fory.deserialize(payload)

Run Swift IDL Integration Tests

cd integration_tests/idl_tests
./run_swift_tests.sh

This runs Swift roundtrip matrix tests and Java peer roundtrip checks (IDL_PEER_LANG=swift).

Debugging Cross-language Tests

Enable debug output when running xlang tests:

ENABLE_FORY_DEBUG_OUTPUT=1 FORY_SWIFT_JAVA_CI=1 mvn -T16 test -Dtest=org.apache.fory.xlang.SwiftXlangTest