-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialization made compatible with zenoh-ext serialization API (#114)
* serialization implementation * add @thi.ng/leb128 dependency * fix ci errors * deserialization clean up * update docs; add generic serialization constraint; * move zenoh ext to a submodule * typo fix * docs update * format * make Object ZPartialDeserializer require a new() constructor, rather than an object instance * add support for specifying number or bigint serialization format * add support for Uint8Array serialization * add support for typed arrays
- Loading branch information
1 parent
a62a3be
commit b98bc97
Showing
16 changed files
with
2,275 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// | ||
// Copyright (c) 2025 ZettaScale Technology | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// | ||
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
import { ZBytes } from "@eclipse-zenoh/zenoh-ts"; | ||
import { ZBytesSerializer, ZBytesDeserializer, ZSerializeable, ZDeserializeable, zserialize, zdeserialize, ZS, ZD, NumberFormat, BigIntFormat } from "@eclipse-zenoh/zenoh-ts/ext"; | ||
|
||
|
||
class MyStruct implements ZSerializeable, ZDeserializeable { | ||
v1: number; | ||
v2: string; | ||
v3: number[]; | ||
|
||
constructor(v1?: number, v2?: string, v3?: number[]) { | ||
if (typeof v1 !== `undefined`) { | ||
this.v1 = v1; | ||
} else { | ||
this.v1 = 0; | ||
} | ||
if (typeof v2 !== `undefined`) { | ||
this.v2 = v2; | ||
} else { | ||
this.v2 = "" | ||
} | ||
if (typeof v3 !== `undefined`) { | ||
this.v3 = v3; | ||
} else { | ||
this.v3 = new Array<number>() | ||
} | ||
} | ||
|
||
serialize_with_zserializer(serializer: ZBytesSerializer): void { | ||
serializer.serialize(this.v1, ZS.number(NumberFormat.Int32)) | ||
serializer.serialize(this.v2) | ||
serializer.serialize(this.v3, ZS.array(ZS.number(NumberFormat.Int8))) | ||
} | ||
|
||
deserialize_with_zdeserializer(deserializer: ZBytesDeserializer): void { | ||
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 { | ||
return JSON.stringify(this) | ||
} | ||
|
||
} | ||
|
||
export async function main() { | ||
// using raw data | ||
// string | ||
{ | ||
let input = "test" | ||
let payload = new ZBytes(input) | ||
let output = payload.to_string() | ||
console.log(`Input: ${input}, Output: ${output}`) | ||
} | ||
// Uint8Array | ||
{ | ||
let input = new Uint8Array([1, 2, 3, 4]) | ||
let payload = new ZBytes(input) | ||
let output = payload.to_bytes() | ||
console.log(`Input: ${input}, Output: ${output}`) | ||
} | ||
|
||
// serialization | ||
// array | ||
{ | ||
let input = [1, 2, 3, 4] | ||
// 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}`) | ||
} | ||
// typed array | ||
{ | ||
let input = new Int32Array([1, 2, 3, 4]) | ||
let payload = zserialize(input) | ||
let output = zdeserialize(ZD.int32array(), payload) | ||
console.log(`Input: ${input}, Output: ${output}`) | ||
} | ||
// map | ||
{ | ||
let input = new Map<bigint, string>() | ||
input.set(0n, "abc") | ||
input.set(1n, "def") | ||
// 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(1234, "test", [1, 2, 3, 4]) | ||
let payload = zserialize(input) | ||
let output = zdeserialize(ZD.object(MyStruct), payload) | ||
console.log(`Input: ${input.to_string()}, Output: ${output.to_string()}`) | ||
} | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
import { deserialize_string, ReplyError, Config, Receiver, RecvErr, Sample, Session, QueryTarget } from "@eclipse-zenoh/zenoh-ts"; | ||
import { ReplyError, Config, Receiver, RecvErr, Sample, Session, QueryTarget } from "@eclipse-zenoh/zenoh-ts"; | ||
import { Duration, Milliseconds } from 'typed-duration' | ||
const { milliseconds } = Duration | ||
import { BaseParseArgs } from "./parse_args.ts"; | ||
|
@@ -30,10 +30,10 @@ export async function main() { | |
// let resp = reply.result(); | ||
// if (resp instanceof Sample) { | ||
// let sample: Sample = resp; | ||
// console.warn(">> Received ('", sample.keyexpr(), ":", sample.payload().deserialize(deserialize_string), "')"); | ||
// console.warn(">> Received ('", sample.keyexpr(), ":", sample.payload().to_string()), "')"); | ||
// } else { | ||
// let reply_error: ReplyError = resp; | ||
// console.warn(">> Received (ERROR: '", reply_error.payload().deserialize(deserialize_string), "')"); | ||
// console.warn(">> Received (ERROR: '", reply_error.payload().to_string(), "')"); | ||
// } | ||
// }; | ||
|
||
|
@@ -57,10 +57,10 @@ export async function main() { | |
const resp = reply.result(); | ||
if (resp instanceof Sample) { | ||
const sample: Sample = resp; | ||
console.warn(">> Received ('", sample.keyexpr(), ":", sample.payload().deserialize(deserialize_string), "')"); | ||
console.warn(">> Received ('", sample.keyexpr(), ":", sample.payload().to_string(), "')"); | ||
} else { | ||
const reply_error: ReplyError = resp; | ||
console.warn(">> Received (ERROR: '{", reply_error.payload().deserialize(deserialize_string), "}')"); | ||
console.warn(">> Received (ERROR: '{", reply_error.payload().to_string(), "}')"); | ||
} | ||
} | ||
reply = await receiver.receive(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
import { deserialize_string, ReplyError, Config, Receiver, RecvErr, Sample, Session, QueryTarget, Selector, } from "@eclipse-zenoh/zenoh-ts"; | ||
import { ReplyError, Config, Receiver, RecvErr, Sample, Session, QueryTarget, Selector, } from "@eclipse-zenoh/zenoh-ts"; | ||
import { Duration, Milliseconds } from 'typed-duration' | ||
import { BaseParseArgs } from "./parse_args.ts"; | ||
|
||
|
@@ -41,10 +41,10 @@ export async function main() { | |
const resp = reply.result(); | ||
if (resp instanceof Sample) { | ||
const sample: Sample = resp; | ||
console.warn(">> Received ('", sample.keyexpr(), ":", sample.payload().deserialize(deserialize_string), "')"); | ||
console.warn(">> Received ('", sample.keyexpr(), ":", sample.payload().to_string(), "')"); | ||
} else { | ||
const reply_error: ReplyError = resp; | ||
console.warn(">> Received (ERROR: '{", reply_error.payload().deserialize(deserialize_string), "}')"); | ||
console.warn(">> Received (ERROR: '{", reply_error.payload().to_string(), "}')"); | ||
} | ||
} | ||
reply = await receiver.receive(); | ||
|
Oops, something went wrong.