Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
uses: actions/checkout@v4
with:
repository: ZettaScaleLabs/zenoh-flat-jni
ref: 249fe9e34ac5d05bc442355fd216a324114e2621
ref: 71736f2bffcd9889f213dcc83b31a696e2abf51e
path: zenoh-flat-jni

- name: Check out zenoh-flat
Expand Down
55 changes: 55 additions & 0 deletions zenoh-java/src/commonMain/kotlin/io/zenoh/ext/JavaTypeSerde.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// Copyright (c) 2026 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, <zenoh@zettascale.tech>
//

package io.zenoh.ext

import io.zenoh.exceptions.ZError
import io.zenoh.jni.bytes.SerializationCodec
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type

/**
* Build a [SerializationCodec.SerdeType] from a Guava `TypeToken`'s
* [java.lang.reflect.Type] — the reflection adapter for zenoh-java's serializer.
* Handles `Class<?>` and `ParameterizedType` for `List`/`Map`; the Kotlin-only
* unsigned value classes and `Pair`/`Triple` erase in the Java reflection
* representation and are not expressible here. Throws [ZError] on an
* unsupported type (argument preparation — before the byte codec runs).
*/
internal fun serdeTypeOfJava(type: Type): SerializationCodec.SerdeType = when (type) {
is Class<*> -> when (type.name) {
"java.lang.Boolean", "boolean" -> SerializationCodec.SerdeType.Bool
"java.lang.Byte", "byte" -> SerializationCodec.SerdeType.I8
"java.lang.Short", "short" -> SerializationCodec.SerdeType.I16
"java.lang.Integer", "int" -> SerializationCodec.SerdeType.I32
"java.lang.Long", "long" -> SerializationCodec.SerdeType.I64
"java.lang.Float", "float" -> SerializationCodec.SerdeType.F32
"java.lang.Double", "double" -> SerializationCodec.SerdeType.F64
"java.lang.String" -> SerializationCodec.SerdeType.Str
"[B" -> SerializationCodec.SerdeType.Bytes
else -> throw ZError("Unsupported type: ${type.name}")
}
is ParameterizedType -> {
val raw = type.rawType as? Class<*> ?: throw ZError("Unsupported raw type: ${type.rawType}")
val args = type.actualTypeArguments
when {
List::class.java.isAssignableFrom(raw) && args.size == 1 ->
SerializationCodec.SerdeType.ZList(serdeTypeOfJava(args[0]))
Map::class.java.isAssignableFrom(raw) && args.size == 2 ->
SerializationCodec.SerdeType.ZMap(serdeTypeOfJava(args[0]), serdeTypeOfJava(args[1]))
else -> throw ZError("Unsupported parameterized type: ${raw.name}")
}
}
else -> throw ZError("Unsupported type: $type")
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import com.google.common.reflect.TypeToken
import io.zenoh.bytes.IntoZBytes
import io.zenoh.bytes.ZBytes
import io.zenoh.exceptions.throwZError0
import io.zenoh.jni.bytes.deserializeViaJNI
import io.zenoh.jni.bytes.SerializationCodec

/**
* Zenoh deserializer.
Expand Down Expand Up @@ -107,6 +107,6 @@ abstract class ZDeserializer<T>: TypeToken<T>() {
*/
fun deserialize(zbytes: IntoZBytes): T {
@Suppress("UNCHECKED_CAST")
return deserializeViaJNI(zbytes.into().bytes, this.type, throwZError0) as T
return SerializationCodec.deserialize(zbytes.into().bytes, serdeTypeOfJava(this.type), throwZError0) as T
}
}
4 changes: 2 additions & 2 deletions zenoh-java/src/commonMain/kotlin/io/zenoh/ext/ZSerializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package io.zenoh.ext
import com.google.common.reflect.TypeToken
import io.zenoh.bytes.ZBytes
import io.zenoh.exceptions.throwZError0
import io.zenoh.jni.bytes.serializeViaJNI
import io.zenoh.jni.bytes.SerializationCodec

/**
* Zenoh serializer.
Expand Down Expand Up @@ -105,6 +105,6 @@ abstract class ZSerializer<T>: TypeToken<T>() {
* Serialize [t] into a [ZBytes].
*/
fun serialize(t: T): ZBytes {
return ZBytes.from(serializeViaJNI(t as Any, this.type, throwZError0))
return ZBytes.from(SerializationCodec.serialize(t as Any, serdeTypeOfJava(this.type), throwZError0))
}
}
117 changes: 0 additions & 117 deletions zenoh-java/src/jvmTest/kotlin/io/zenoh/EncodingCorrespondenceTest.kt

This file was deleted.

167 changes: 0 additions & 167 deletions zenoh-java/src/jvmTest/kotlin/io/zenoh/ParametersCorrespondenceTest.kt

This file was deleted.

Loading
Loading