chaokunyang commented on PR #3394:
URL: https://github.com/apache/fory/pull/3394#issuecomment-4109763744
> compatible mode
compatible mode are supported in fory js, there are lots of compatible tests
in javascript/test/crossLanguage.test.ts, which you can take as reference. For
example:
```javascript
test("test_skip_id_custom", () => {
const fory1 = new Fory({
compatible: true
});
@Type.ext(103)
class MyExt {
id: number = 0;
}
fory1.registerSerializer(MyExt, {
write: (value: MyExt, writer: BinaryWriter, fory: Fory) => {
writer.writeVarInt32(value.id);
},
read: (result: MyExt, reader: BinaryReader, fory: Fory) => {
result.id = reader.readVarInt32();
}
});
// Define empty wrapper for deserialization
@Type.struct(104)
class Empty { }
fory1.registerSerializer(Empty);
const fory2 = new Fory({
compatible: true
});
// Define Color enum
const Color = {
Green: 0,
Red: 1,
Blue: 2,
White: 3,
};
fory2.registerSerializer(Type.enum(101, Color));
@Type.struct(102, {
id: Type.varInt32()
})
class MyStruct {
id: number = 0;
}
fory2.registerSerializer(MyStruct);
fory2.registerSerializer(MyExt, {
write: (value: MyExt, writer: BinaryWriter, fory: Fory) => {
writer.writeVarInt32(value.id);
},
read: (result: MyExt, reader: BinaryReader, fory: Fory) => {
result.id = reader.readVarInt32();
}
});
@Type.struct(104, {
color: Type.enum(101, Color),
myStruct: Type.struct(102),
myExt: Type.ext(103)
})
class MyWrapper {
color: number = 0;
myStruct: MyStruct = new MyStruct();
myExt: MyExt = new MyExt();
}
fory2.registerSerializer(MyWrapper);
// Deserialize empty from Java
let cursor = 0;
const deserializedEmpty = fory1.deserialize(content.subarray(cursor));
cursor += fory1.binaryReader.readGetCursor();
expect(deserializedEmpty instanceof Empty).toEqual(true);
// Create wrapper object
const wrapper = new MyWrapper();
wrapper.color = Color.White;
wrapper.myStruct = new MyStruct();
wrapper.myStruct.id = 42;
wrapper.myExt = new MyExt();
wrapper.myExt.id = 43;
// Serialize wrapper
const serializedData = fory2.serialize(wrapper);
writeToFile(serializedData as Buffer);
});
```
Please fix all errors
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]