Skip to content

Commit

Permalink
add support for specifying number or bigint serialization format
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Jan 31, 2025
1 parent 36e8d33 commit 6edc469
Show file tree
Hide file tree
Showing 3 changed files with 474 additions and 80 deletions.
40 changes: 24 additions & 16 deletions zenoh-ts/examples/deno/src/z_bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
//

import { ZBytes } from "@eclipse-zenoh/zenoh-ts";
import { ZBytesSerializer, ZBytesDeserializer, ZSerializeable, ZDeserializeable, zserialize, zdeserialize, ZSerDe } from "@eclipse-zenoh/zenoh-ts/ext";
import { ZBytesSerializer, ZBytesDeserializer, ZSerializeable, ZDeserializeable, zserialize, zdeserialize, ZS, ZD, NumberFormat, BigIntFormat } from "@eclipse-zenoh/zenoh-ts/ext";


class MyStruct implements ZSerializeable, ZDeserializeable {
v1: bigint;
v1: number;
v2: string;
v3: number[];

constructor(v1?: bigint, v2?: string, v3?: number[]) {
constructor(v1?: number, v2?: string, v3?: number[]) {
if (typeof v1 !== `undefined`) {
this.v1 = v1;
} else {
this.v1 = 0n;
this.v1 = 0;
}
if (typeof v2 !== `undefined`) {
this.v2 = v2;
Expand All @@ -40,15 +40,15 @@ class MyStruct implements ZSerializeable, ZDeserializeable {
}

serialize_with_zserializer(serializer: ZBytesSerializer): void {
serializer.serialize(this.v1)
serializer.serialize(this.v1, ZS.number(NumberFormat.Int32))
serializer.serialize(this.v2)
serializer.serialize(this.v3)
serializer.serialize(this.v3, ZS.array(ZS.number(NumberFormat.Int8)))
}

deserialize_with_zdeserializer(deserializer: ZBytesDeserializer): void {
this.v1 = deserializer.deserialize(ZSerDe.bigint())
this.v2 = deserializer.deserialize(ZSerDe.string())
this.v3 = deserializer.deserialize(ZSerDe.array(ZSerDe.number()))
this.v1 = deserializer.deserialize(ZD.number(NumberFormat.Uint32))
this.v2 = deserializer.deserialize(ZD.string())
this.v3 = deserializer.deserialize(ZD.array(ZD.number(NumberFormat.Int8)))
}

to_string(): string {
Expand Down Expand Up @@ -77,28 +77,36 @@ export async function main() {
// serialization
// array
{
let input = [0.5, 1.0, 3.0, 5.5]
let payload = zserialize(input)
let output = zdeserialize(ZSerDe.array(ZSerDe.number()), payload)
let input = [1, 2, 3, 5]
// by default number is serialized/deserialized as 64-bit float,
// other formats, like Int32, for example, must be specified explicitly
let payload = zserialize(input, ZS.array(ZS.number(NumberFormat.Int32)))
let output = zdeserialize(ZD.array(ZD.number(NumberFormat.Int32)), payload)
// let payload = zserialize(input)
// let output = zdeserialize(ZD.array(ZD.number()), payload)
console.log(`Input: ${input}, Output: ${output}`)
}
// map
{
let input = new Map<bigint, string>()
input.set(0n, "abc")
input.set(1n, "def")
let payload = zserialize(input)
let output = zdeserialize(ZSerDe.map(ZSerDe.bigint(), ZSerDe.string()), payload)
// by default bigint is serialized/deserialized as 64-bit signed integer,
// other formats, like UInt64, for example, must be specified explicitly
let payload = zserialize(input, ZS.map(ZS.bigint(BigIntFormat.Uint64), ZS.string()))
let output = zdeserialize(ZD.map(ZD.bigint(BigIntFormat.Uint64), ZD.string()), payload)
// let payload = zserialize(input)
// let output = zdeserialize(ZD.map(ZD.bigint(), ZD.string()), payload)
console.log(`Input:`)
console.table(input)
console.log(`Output:`)
console.table(output)
}
// Custom class
{
let input = new MyStruct(1234n, "test", [1, 2, 3, 4])
let input = new MyStruct(1234, "test", [1, 2, 3, 4])
let payload = zserialize(input)
let output = zdeserialize(ZSerDe.object(MyStruct), payload)
let output = zdeserialize(ZD.object(MyStruct), payload)
console.log(`Input: ${input.to_string()}, Output: ${output.to_string()}`)
}
}
Expand Down
6 changes: 3 additions & 3 deletions zenoh-ts/src/ext/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// ZettaScale Zenoh Team, <[email protected]>
//

// Serialziation
import { ZBytesSerializer, ZBytesDeserializer, ZSerializeable, ZDeserializeable, zserialize, zdeserialize, ZSerDe } from "./serialization.js"
// Serialization
import { ZBytesSerializer, ZBytesDeserializer, ZSerializeable, ZDeserializeable, zserialize, zdeserialize, NumberFormat, BigIntFormat, ZS, ZD } from "./serialization.js"



// Exports
export { ZBytesSerializer, ZBytesDeserializer, ZSerializeable, ZDeserializeable, zserialize, zdeserialize, ZSerDe }
export { ZBytesSerializer, ZBytesDeserializer, ZSerializeable, ZDeserializeable, zserialize, zdeserialize, NumberFormat, BigIntFormat, ZS, ZD }
Loading

0 comments on commit 6edc469

Please sign in to comment.