Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization feature #158

Merged
merged 88 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
e275db2
Alignment: bytes package
DariusIMP Oct 21, 2024
bd94fe3
Alignment: encoding update
DariusIMP Oct 21, 2024
9b62e1e
Alignment: config package
DariusIMP Oct 21, 2024
e9ba08a
Alignment: replacing all exceptions with ZError
DariusIMP Oct 21, 2024
941efe8
Alignment: ext package
DariusIMP Oct 21, 2024
1ee563b
Alignment: jni callbacks
DariusIMP Oct 21, 2024
95a13e5
Alignment: config
DariusIMP Oct 21, 2024
7f372b9
Alignment: JNIKeyExpr
DariusIMP Oct 21, 2024
9cb1dcc
Alignment: JNIPublisher
DariusIMP Oct 21, 2024
4390040
Alignment: JNIQuery
DariusIMP Oct 21, 2024
c9e0416
Alignment: JNIScout
DariusIMP Oct 21, 2024
5b2194f
Alignment: JNISession
DariusIMP Oct 21, 2024
5ed9777
Alignment JNIZBytes & JNIZenohId
DariusIMP Oct 22, 2024
aee2546
Alignment: KeyExpr & SetIntersectionLevel
DariusIMP Oct 22, 2024
52300e5
Alignment: pubsub package
DariusIMP Oct 22, 2024
afe3766
Alignment: Publisher & qos package
DariusIMP Oct 22, 2024
c68cef3
Alignment: query package - wip
DariusIMP Oct 22, 2024
a643bca
Alignment: adding scouting package
DariusIMP Oct 22, 2024
1d2121c
Alignment: removing Value
DariusIMP Oct 22, 2024
377661a
Alignment: logger
DariusIMP Oct 23, 2024
5d3705c
Alignmment: Zenoh.kt - wip
DariusIMP Oct 23, 2024
ba0fd31
Alignment: publisher - adding encoding and reliability to builder
DariusIMP Oct 23, 2024
14844c6
Alignment: subscriber
DariusIMP Oct 23, 2024
383f86e
Alignment: sample
DariusIMP Oct 23, 2024
eb9eeb8
Alignment: query & reply - wip
DariusIMP Oct 25, 2024
1f9b13d
Alignment: Put & Session
DariusIMP Oct 28, 2024
283c1f3
Alignment: fix 'IntoSelector'
DariusIMP Oct 28, 2024
6dcfe69
Alignment: updating zenoh-jni
DariusIMP Oct 29, 2024
68b18cf
Alignment: wip - converting tests to java tests. Added config tests.
DariusIMP Nov 12, 2024
4869b07
Alignment: wip - converting tests to java tests. Added delete test.
DariusIMP Nov 12, 2024
f2d666d
Alignment: wip - converting tests to java tests. Added encoding tests.
DariusIMP Nov 13, 2024
8badd0c
Alignment: wip - converting tests to java tests. Added get tests.
DariusIMP Nov 13, 2024
5f1c920
Alignment: wip - converting tests to java tests. Added key expr tests.
DariusIMP Nov 13, 2024
ce8147a
Alignment: wip - converting tests to java tests. Added parameters tests.
DariusIMP Nov 13, 2024
faccac9
Alignment: wip - converting tests to java tests. Added publisher tests.
DariusIMP Nov 13, 2024
6b9d6e1
Alignment: wip - converting tests to java tests. Added Queryable tests.
DariusIMP Nov 13, 2024
163b953
Alignment: wip - converting tests to java tests. Added Put tests.
DariusIMP Nov 14, 2024
790a131
Alignment: wip - Added Scouting tests + adding scouting builder.
DariusIMP Nov 14, 2024
f94ee4b
Alignment: wrapping up scouting, closing queue upon scout close.
DariusIMP Nov 18, 2024
5426942
Alignment: adding selector tests
DariusIMP Nov 18, 2024
98db96e
Alignment: fix SessionInfo + adding tests
DariusIMP Nov 18, 2024
1fa7d9d
Alignment: fix Scouting queue test
DariusIMP Nov 18, 2024
9a859e1
amend! Alignment: fix SessionInfo + adding tests
DariusIMP Nov 18, 2024
384ba33
Alignment: session tests
DariusIMP Nov 18, 2024
2424498
Alignment: user attachment tests
DariusIMP Nov 20, 2024
025bcf4
Alignment: subscriber tests
DariusIMP Nov 20, 2024
f5565f2
Alignment: removing the zenoh-ext package (to be added later in anoth…
DariusIMP Nov 21, 2024
14a615e
Alignment: Publisher config params
DariusIMP Nov 22, 2024
5d53262
Alignment: Subscriber config params
DariusIMP Nov 22, 2024
a98180d
Alignment: Queryable config params
DariusIMP Nov 25, 2024
66f158f
Alignment: Subscriber config params refactor
DariusIMP Nov 25, 2024
d9eca7d
Alignment: Queryable declaration and Query.reply config params.
DariusIMP Nov 26, 2024
bebd45f
Alignment: Get config params
DariusIMP Nov 26, 2024
73fad03
Alignment: Subscriber config params refactor
DariusIMP Nov 26, 2024
9962353
Fix config test
DariusIMP Nov 27, 2024
6e6c86d
Alignment - Scouting config params
DariusIMP Nov 27, 2024
95f86aa
Alignment: adding Liveliness
DariusIMP Nov 28, 2024
8c59360
Gitignore update
DariusIMP Nov 28, 2024
6b35c68
Alignment: fix logging
DariusIMP Nov 28, 2024
b580619
Alignment: publisher put and delete config param
DariusIMP Nov 29, 2024
2cc4260
Alignment: examples - adding picocli for CLI args
DariusIMP Nov 29, 2024
931b980
Alignment: examples - adding missing examples
DariusIMP Nov 29, 2024
bbf20c2
Alignment: examples - adding ping and pong examples
DariusIMP Nov 29, 2024
da98b95
Alignment: examples refactor + refactor queryable config logic
DariusIMP Dec 2, 2024
e18807a
Alignment: fix publisher put encoding fallback
DariusIMP Dec 2, 2024
997108a
Alignment: removing SubscriberConfig.kt
DariusIMP Dec 4, 2024
4e1bc01
Alignment: renaming PublisherConfig to PublisherOptions
DariusIMP Dec 4, 2024
7affa19
Alignment: renaming DeleteConfig to DeleteOptions and removing 'build…
DariusIMP Dec 4, 2024
3b916ed
Alignment: renaming GetConfig to GetOptions and removing 'builder' fu…
DariusIMP Dec 4, 2024
4d9dfe8
Alignment: renaming PutConfig to PutOptions and removing 'builder' fu…
DariusIMP Dec 4, 2024
569438f
Alignment: renaming ReplyConfig to ReplyOptions and removing 'builder…
DariusIMP Dec 5, 2024
94bbab4
Alignment: renaming ReplyDelConfig to ReplyDelOptions and removing 'b…
DariusIMP Dec 5, 2024
07f22dc
Alignment: renaming ReplyErrConfig to ReplyErrOptions and removing 'b…
DariusIMP Dec 5, 2024
c5d92dd
Alignment: renaming QueryableConfig to QueryableOptions and removing …
DariusIMP Dec 5, 2024
752c453
Alignment: renaming ScoutConfig to ScoutOptions and removing 'builder…
DariusIMP Dec 5, 2024
0577e5b
Alignment: renaming Liveliness.SubscriberConfig + renaming variables
DariusIMP Dec 5, 2024
1e72dd6
Alignment: queryable options refactor
DariusIMP Dec 5, 2024
1dffad8
Alignment: removing Resolvable
DariusIMP Dec 5, 2024
e3cdd11
Alignment: splitting Queryable, Subscriber and Get into Handler and C…
DariusIMP Dec 10, 2024
795fbff
Alignment: removing JNIZBytes
DariusIMP Dec 11, 2024
2f74120
Alignment: removing unused kotlin json dependency
DariusIMP Dec 11, 2024
ec297d2
feat(serialization): implementation of the serialization
DariusIMP Dec 12, 2024
3f932ad
feat(serialization): implementation of the deserialization
DariusIMP Dec 13, 2024
e16d2b7
feat(serialization): adding ZBytes examples.
DariusIMP Dec 13, 2024
d2e2a3d
feat(serialization): cargo clippy
DariusIMP Dec 13, 2024
36cad63
feat(serialization): ZSerializer and ZDeserializer classes
DariusIMP Dec 23, 2024
704320c
Merge branch 'main' of github.com:eclipse-zenoh/zenoh-java into seria…
DariusIMP Dec 23, 2024
c2e7449
serialization: zenoh-ext cfg feature for zbytes on zenoh-jni
DariusIMP Dec 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ dependencies {
implementation("commons-net:commons-net:3.9.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
implementation("info.picocli:picocli:4.7.4")
implementation("com.google.guava:guava:33.3.1-jre")
}

tasks {
val examples = listOf(
"ZBytesExamples",
"ZDelete",
"ZGet",
"ZGetLiveliness",
Expand Down
192 changes: 192 additions & 0 deletions examples/src/main/java/io/zenoh/ZBytesExamples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
//
// Copyright (c) 2023 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]>
//

package io.zenoh;

import io.zenoh.bytes.ZBytes;
import io.zenoh.ext.ZDeserializer;
import io.zenoh.ext.ZSerializer;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class ZBytesExamples {

public static void main(String[] args) {

/*
* ZBytes
*
* A ZBytes instance can be created from a String and from a Byte Array with the `ZBytes.from(string: String)`
* and `ZBytes.from(bytes: byte[])` functions.
*
* A ZBytes can be converted back into a [String] with the functions [ZBytes.toString] and [ZBytes.tryToString].
* Similarly, with [ZBytes.toBytes] you can get the inner byte representation.
*/

var exampleString = "example string";
var zbytesA = ZBytes.from(exampleString);
var outputA = zbytesA.toString();
assert exampleString.equals(outputA);

var exampleBytes = new byte[]{1, 2, 3, 4, 5};
var zbytesB = ZBytes.from(exampleBytes);
var outputB = zbytesB.toBytes();
assert Arrays.equals(exampleBytes, outputB);

/*
* Serialization and deserialization.
*
* Additionally, the Zenoh API provides a series of serialization and deserialization utilities for processing
* the received payloads.
*
* Serialization and deserialization supports the following types:
* - Boolean
* - Byte
* - Byte Array
* - Short
* - Int
* - Long
* - Float
* - Double
* - String
* - List
* - Map
*
* For List and Map, the inner types can be a combination of the above types, including themselves.
*
* These serialization and deserialization utilities can be used across the Zenoh ecosystem with Zenoh
* versions based on other supported languages such as Rust, Python, C and C++.
* This works when the types are equivalent (a `Byte` corresponds to an `i8` in Rust, a `Short` to an `i16`, etc).
*/

// Boolean example
Boolean input1 = true;
ZSerializer<Boolean> serializer1 = new ZSerializer<>() {};
ZBytes zbytes1 = serializer1.serialize(input1);

ZDeserializer<Boolean> deserializer1 = new ZDeserializer<>() {};
Boolean output1 = deserializer1.deserialize(zbytes1);
assert input1.equals(output1);

// Byte example
Byte input2 = 126;
ZSerializer<Byte> serializer2 = new ZSerializer<>() {};
ZBytes zbytes2 = serializer2.serialize(input2);

ZDeserializer<Byte> deserializer2 = new ZDeserializer<>() {};
Byte output2 = deserializer2.deserialize(zbytes2);
assert input2.equals(output2);

// Short example
Short input3 = 256;
ZSerializer<Short> serializer3 = new ZSerializer<>() {};
ZBytes zbytes3 = serializer3.serialize(input3);

ZDeserializer<Short> deserializer3 = new ZDeserializer<>() {};
Short output3 = deserializer3.deserialize(zbytes3);
assert input3.equals(output3);

// Int example
Integer input4 = 123456;
ZSerializer<Integer> serializer4 = new ZSerializer<>() {};
ZBytes zbytes4 = serializer4.serialize(input4);

ZDeserializer<Integer> deserializer4 = new ZDeserializer<>() {};
Integer output4 = deserializer4.deserialize(zbytes4);
assert input4.equals(output4);

// Long example
Long input5 = 123456789L;
ZSerializer<Long> serializer5 = new ZSerializer<>() {};
ZBytes zbytes5 = serializer5.serialize(input5);

ZDeserializer<Long> deserializer5 = new ZDeserializer<>() {};
Long output5 = deserializer5.deserialize(zbytes5);
assert input5.equals(output5);

// Float example
Float input6 = 123.45f;
ZSerializer<Float> serializer6 = new ZSerializer<>() {};
ZBytes zbytes6 = serializer6.serialize(input6);

ZDeserializer<Float> deserializer6 = new ZDeserializer<>() {};
Float output6 = deserializer6.deserialize(zbytes6);
assert input6.equals(output6);

// Double example
Double input7 = 12345.6789;
ZSerializer<Double> serializer7 = new ZSerializer<>() {};
ZBytes zbytes7 = serializer7.serialize(input7);

ZDeserializer<Double> deserializer7 = new ZDeserializer<>() {};
Double output7 = deserializer7.deserialize(zbytes7);
assert input7.equals(output7);

// List example
List<Integer> input12 = List.of(1, 2, 3, 4, 5);
ZSerializer<List<Integer>> serializer12 = new ZSerializer<>() {};
ZBytes zbytes12 = serializer12.serialize(input12);

ZDeserializer<List<Integer>> deserializer12 = new ZDeserializer<>() {};
List<Integer> output12 = deserializer12.deserialize(zbytes12);
assert input12.equals(output12);

// String example
String input13 = "Hello, World!";
ZSerializer<String> serializer13 = new ZSerializer<>() {};
ZBytes zbytes13 = serializer13.serialize(input13);

ZDeserializer<String> deserializer13 = new ZDeserializer<>() {};
String output13 = deserializer13.deserialize(zbytes13);
assert input13.equals(output13);

// ByteArray example
byte[] input14 = new byte[]{1, 2, 3, 4, 5};
ZSerializer<byte[]> serializer14 = new ZSerializer<>() {};
ZBytes zbytes14 = serializer14.serialize(input14);

ZDeserializer<byte[]> deserializer14 = new ZDeserializer<>() {};
byte[] output14 = deserializer14.deserialize(zbytes14);
assert Arrays.equals(input14, output14);

// Map example
Map<String, Integer> input15 = Map.of("one", 1, "two", 2, "three", 3);
ZSerializer<Map<String, Integer>> serializer15 = new ZSerializer<>() {};
ZBytes zbytes15 = serializer15.serialize(input15);

ZDeserializer<Map<String, Integer>> deserializer15 = new ZDeserializer<>() {};
Map<String, Integer> output15 = deserializer15.deserialize(zbytes15);
assert input15.equals(output15);

// Nested List example
List<List<Integer>> input18 = List.of(List.of(1, 2, 3));
ZSerializer<List<List<Integer>>> serializer18 = new ZSerializer<>() {};
ZBytes zbytes18 = serializer18.serialize(input18);

ZDeserializer<List<List<Integer>>> deserializer18 = new ZDeserializer<>() {};
List<List<Integer>> output18 = deserializer18.deserialize(zbytes18);
assert input18.equals(output18);

// Combined types example
List<Map<String, Integer>> input19 = List.of(Map.of("a", 1, "b", 2));
ZSerializer<List<Map<String, Integer>>> serializer19 = new ZSerializer<>() {};
ZBytes zbytes19 = serializer19.serialize(input19);

ZDeserializer<List<Map<String, Integer>>> deserializer19 = new ZDeserializer<>() {};
List<Map<String, Integer>> output19 = deserializer19.deserialize(zbytes19);
assert input19.equals(output19);
}
}
1 change: 1 addition & 0 deletions zenoh-java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ kotlin {
val commonMain by getting {
dependencies {
implementation("commons-net:commons-net:3.9.0")
implementation("com.google.guava:guava:33.3.1-jre")
}
}
val commonTest by getting {
Expand Down
110 changes: 110 additions & 0 deletions zenoh-java/src/commonMain/kotlin/io/zenoh/ext/ZDeserializer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// Copyright (c) 2023 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]>
//

package io.zenoh.ext

import com.google.common.reflect.TypeToken
import io.zenoh.bytes.ZBytes
import io.zenoh.jni.JNIZBytes

/**
* Zenoh deserializer.
*
* This class is a utility for deserializing [ZBytes] into elements of type [T].
*
* This class supports the following types:
* - [Boolean]
* - [Byte]
* - [Short]
* - [Int]
* - [Long]
* - [Float]
* - [Double]
* - [List]
* - [String]
* - [ByteArray]
* - [Map]
*
* For List and Map, the inner types can be a combination of the above types, including themselves.
*
* Due to Java's type erasure, an actual implementation of this abstract class needs to be created (see the examples below).
*
* This deserialization utility can be used across the Zenoh ecosystem with Zenoh
* versions based on other supported languages such as Rust, Python, C and C++.
* This works when the types are equivalent (a `Byte` corresponds to an `i8` in Rust, a `Short` to an `i16`, etc).
*
* # Examples
*
* Example for a basic type, in this case an integer:
* ```java
* Integer input = 123456;
* ZSerializer<Integer> serializer = new ZSerializer<>() {};
* ZBytes zbytes = serializer.serialize(input);
*
* ZDeserializer<Integer> deserializer = new ZDeserializer<>() {};
* Integer output = deserializer.deserialize(zbytes);
* assert input.equals(output);
* ```
*
* Examples for parameterized types:
* - List
* ```java
* List<Integer> input = List.of(1, 2, 3, 4, 5);
* ZSerializer<List<Integer>> serializer = new ZSerializer<>() {};
* ZBytes zbytes = serializer.serialize(input12);
*
* ZDeserializer<List<Integer>> deserializer = new ZDeserializer<>() {};
* List<Integer> output = deserializer.deserialize(zbytes);
* assert input.equals(output);
* ```
*
* - Map
* ```java
* Map<String, Integer> input = Map.of("one", 1, "two", 2, "three", 3);
* ZSerializer<Map<String, Integer>> serializer = new ZSerializer<>() {};
* ZBytes zbytes = serializer.serialize(input);
*
* ZDeserializer<Map<String, Integer>> deserializer = new ZDeserializer<>() {};
* Map<String, Integer> output = deserializer.deserialize(zbytes);
* assert input.equals(output);
* ```
*
* As mentioned, for List and Map, the inner types can be a combination of the above types, including themselves.
* Here's an example with a List of Maps:
* ```java
* List<Map<String, Integer>> input = List.of(Map.of("a", 1, "b", 2));
* ZSerializer<List<Map<String, Integer>>> serializer = new ZSerializer<>() {};
* ZBytes zbytes = serializer.serialize(input);
*
* ZDeserializer<List<Map<String, Integer>>> deserializer = new ZDeserializer<>() {};
* List<Map<String, Integer>> output = deserializer.deserialize(zbytes);
* assert input.equals(output);
* ```
*
* For more examples, see the ZBytesExamples in the examples.
*
* @param T The deserialization type.
* @see ZBytes
* @see ZSerializer
*/
abstract class ZDeserializer<T>: TypeToken<T>() {

/**
* Deserialize the [zbytes] into an element of type [T].
*/
fun deserialize(zbytes: ZBytes): T {
@Suppress("UNCHECKED_CAST")
return JNIZBytes.deserializeViaJNI(zbytes, this.type) as T
}
}
Loading
Loading