You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -70,12 +70,6 @@ In 1.0.0. the previous example would instead now become:
70
70
val encoding =Encoding.TEXT_JSON
71
71
```
72
72
73
-
Custom encodings can be created by specifying an `id` and `suffix` string:
74
-
75
-
```kotlin
76
-
val encoding =Encoding(id =123, suffix ="example suffix")
77
-
```
78
-
79
73
## Session-managed declarations
80
74
81
75
Up until 0.11.0, it was up to the user to keep track of their variable declarations to keep them alive, because once the variable declarations were garbage collected, the declarations were closed. This was because each Kotlin variable declaration is associated with a native Rust instance, and in order to avoid leaking the memory of that Rust instance, it was necessary to free it upon dropping the declaration instance. However, this behavior could be counterintuitive, as users were expecting the declaration to keep running despite losing track of the reference to it.
@@ -85,14 +79,16 @@ In this release we introduce a change in which any session declaration is intern
85
79
For instance:
86
80
87
81
```kotlin
88
-
val subscriber = session.declareSubscriber("A/B/C".intoKeyExpr(),
82
+
val keyExprA ="A/B/C".intoKeyExpr().getOrThrow()
83
+
val subscriber = session.declareSubscriber(keyExprA,
89
84
callback = { sample ->println("Receiving sample on 'A/B/C': ${sample.payload}") }).getOrThrow()
90
85
91
-
session.declareSubscriber("A/C/D".intoKeyExpr(),
92
-
callback = { sample ->println("Receiving sample on 'A/C/D': ${sample.payload}") }) // No variable is associated to the declared session, on 0.11.0 it would have been instantanely dropped
86
+
val keyExprB ="A/C/D".intoKeyExpr().getOrThrow()
87
+
session.declareSubscriber(keyExprB,
88
+
callback = { sample ->println("Receiving sample on 'A/C/D': ${sample.payload}") }) // No variable is associated to the declared session, on 0.11.0 it would have been instantly dropped
93
89
```
94
90
95
-
Therfore, when receiving a 'hello' message on `A/**` we would still see:
91
+
Therefore, when receiving a 'hello' message on `A/**` we would still see:
96
92
97
93
```
98
94
>> Receiving sample on 'A/B/C': hello
@@ -160,7 +156,7 @@ Changes:
160
156
161
157
## Reliability
162
158
163
-
The `Reliability` config parameter used on when declaring a subscriber, has been moved. It now must be specified when declaring a `Publisher` or when performing a `Put` or a `Delete`operaton.
159
+
The `Reliability` config parameter used on when declaring a subscriber, has been moved. It now must be specified when declaring a `Publisher` or when performing a `Put` or a `Delete`operation.
164
160
165
161
166
162
## Logging
@@ -202,202 +198,101 @@ We have created a new abstraction with the name of `ZBytes`. This class represen
202
198
-`Value` is replaced by the combination of `ZBytes` and `Encoding`.
203
199
- Replacing `ByteArray` to represent payloads
204
200
205
-
With `ZBytes` we have also introduced a Serialization and Deserialization for conveinent conversion between `ZBytes` and Kotlin types.
201
+
With `ZBytes` we have also introduced a Serialization and Deserialization for convenient conversion between `ZBytes` and Kotlin types.
206
202
207
-
### Serialization
203
+
### Serialization & Deserialization
208
204
209
205
We can serialize primitive types into a `ZBytes` instance, that is, converting the data into bytes processed by the zenoh network:
210
206
211
207
#### Primitive types
212
208
213
-
The following types support serialization:
209
+
(De)Serialization is supported by the following primitive types:
For the primitive types, there are three ways to serialize them into a `ZBytes`, for instance let's suppose
220
-
we want to serialize an `Int`:
221
-
222
-
* using the `into()` syntax:
223
-
```kotlin
224
-
val exampleInt:Int=256
225
-
val zbytes:ZBytes= exampleInt.into()
226
-
```
227
-
228
-
* using the `from()` syntax:
229
-
```kotlin
230
-
val exampleInt:Int=256
231
-
val zbytes:ZBytes=ZBytes.from(exampleInt)
232
-
```
233
-
234
-
* using the serialize syntax:
215
+
For instance:
235
216
```kotlin
236
217
val exampleInt:Int=256
237
-
val zbytes:ZBytes=ZBytes.serialize<Int>(exampleInt).getOrThrow()
218
+
val zbytes:ZBytes= zSerialize<Int>(exampleInt).getOrThrow()
219
+
val deserialization = zDeserialize<Int>(zbytes).getOrThrow()
220
+
check(exampleInt == deserialization)
238
221
```
222
+
239
223
This approach works as well for the other aforementioned types.
240
224
241
-
Using `into()` or `from()` guarantees successful serialization for implemented types.
242
-
Using `serialize` requires a generic parameter, and returns a Result, i.e. it can fail based in the type passed in and contents of the input parameter.
225
+
For serialization, `String` and `ByteArray` the functions `ZBytes::from(string: String)` and `ZBytes::from(bytes: ByteArray)` can be used respectively. Analogously, deserialization, `ZBytes::toString()` and `ZBytes::toByteArray()` can be used.
243
226
244
227
#### Lists
245
228
246
229
Lists are supported, but they must be either:
247
-
- List of `Number`: (`Byte`, `Short`, `Int`, `Long`, `Float`, `Double`)
230
+
- List of numeric types : (`Byte`, `Short`, `Int`, `Long`, `Float`, `Double`, `UByte`, `UShort`, `UInt` and `ULong`)
248
231
- List of `String`
249
232
- List of `ByteArray`
250
-
- List of `IntoZBytes`
233
+
- List of another supported type
251
234
252
235
The serialize syntax must be used:
253
236
```kotlin
254
237
val myList =listOf(1, 2, 5, 8, 13, 21)
255
-
val zbytes =ZBytes.serialize<List<Int>>(myList).getOrThrow()
238
+
val zbytes = zSerialize<List<Int>>(myList).getOrThrow()
239
+
val outputList = zDeserialize<List<Int>>(zbytes).getOrThrow()
240
+
check(myList == outputList)
256
241
```
257
242
258
243
#### Maps
259
244
260
245
Maps are supported as well, with the restriction that their inner types must supported primitives:
261
-
-`Number`
246
+
-Numeric types
262
247
-`String`
263
248
-`ByteArray`
264
-
-`IntoZBytes`
249
+
-Map of another supported types
265
250
266
251
```kotlin
267
252
val myMap:Map<String, Int> =mapOf("foo" to 1, "bar" to 2)
268
-
val zbytes =ZBytes.serialize<Map<String, Int>>(myMap).getOrThrow()
253
+
val zbytes = zSerialize<Map<String, Int>>(myMap).getOrThrow()
254
+
val outputMap = zDeserialize<Map<String, Int>>(zbytes).getOrThrow()
255
+
output(myMap == outputMap)
269
256
```
270
257
271
-
###Deserialization
258
+
#### Pair & Triple
272
259
273
-
#### Primitive types
274
-
275
-
* Numeric: `Byte`, `Short`, `Int`, `Long`, `Float` and `Double`
276
-
*`String`
277
-
*`ByteArray`
278
-
279
-
Example:
280
-
281
-
For these primitive types, you can use the functions `to<Type>`, that is
282
-
-`toByte`
283
-
-`toShort`
284
-
-`toInt`
285
-
-`toLong`
286
-
-`toDouble`
287
-
-`toString`
288
-
-`toByteArray`
289
-
290
-
For instance, for an Int:
291
-
```kotlin
292
-
val example:Int=256
293
-
val zbytes:ZBytes= exampleInt.into()
294
-
val deserializedInt = zbytes.toInt()
295
-
```
296
-
297
-
Alternatively, the deserialize syntax can be used as well:
298
-
```kotlin
299
-
val exampleInt:Int=256
300
-
val zbytes:ZBytes= exampleInt.into()
301
-
val deserializedInt = zbytes.deserialize<Int>().getOrThrow()
302
-
```
303
-
304
-
#### Lists
305
-
306
-
Lists are supported, but they must be deserialized into inner primitive types:
307
-
- List of `Number` (`Byte`, `Short`, `Int`, `Long`, `Float` or `Double`)
308
-
- List of `String`
309
-
- List of `ByteArray`
310
-
311
-
To deserialize into a list, use the deserialize syntax as follows:
312
-
```kotlin
313
-
val inputList =listOf("sample1", "sample2", "sample3")
314
-
payload =ZBytes.serialize(inputList).getOrThrow()
315
-
val outputList = payload.deserialize<List<String>>().getOrThrow()
316
-
```
317
-
318
-
#### Maps
319
-
320
-
Maps are supported as well, with the restriction that their inner types must be one of the following:
321
-
-`Number`
322
-
-`String`
323
-
-`ByteArray`
324
-
325
-
```kotlin
326
-
val inputMap =mapOf("key1" to "value1", "key2" to "value2", "key3" to "value3")
327
-
payload =ZBytes.serialize(inputMap).getOrThrow()
328
-
val outputMap = payload.deserialize<Map<String, String>>().getOrThrow()
329
-
check(inputMap == outputMap)
330
-
```
331
-
332
-
### Custom serialization and deserialization
333
-
334
-
#### Serialization
335
-
336
-
For a user defined class, the class must implement the `IntoZBytes` interface.
337
-
For instance:
338
-
339
-
```kotlin
340
-
classFoo(valcontent:String) : IntoZBytes {
341
-
342
-
/*Inherits: IntoZBytes*/
343
-
overridefuninto(): ZBytes= content.into()
344
-
}
345
-
```
346
-
347
-
This way, we can do:
348
-
```kotlin
349
-
val foo =Foo("bar")
350
-
val serialization =ZBytes.serialize<Foo>(foo).getOrThrow()
351
-
```
352
-
353
-
Implementing the `IntoZBytes` interface on a class allows serializing lists and maps of that type, for instance:
354
-
```kotlin
355
-
val list =listOf(Foo("bar"), Foo("buz"), Foo("fizz"))
356
-
val zbytes =ZBytes.serialize<List<Foo>>(list)
357
-
```
358
-
359
-
#### Deserialization
360
-
361
-
Regarding deserialization for custom objects, for the time being (this API will be expanded to
362
-
provide further utilities) you need to manually convert the ZBytes into the type you want.
363
-
364
-
```kotlin
365
-
val inputFoo =Foo("example")
366
-
payload =ZBytes.serialize(inputFoo).getOrThrow()
367
-
val outputFoo =Foo.from(payload)
368
-
check(inputFoo == outputFoo)
369
-
370
-
// List of Foo.
371
-
val inputListFoo = inputList.map { value ->Foo(value) }
0 commit comments