release 0.1.0: fixed bug with object/empty serialization, migrated to newest kotlin

This commit is contained in:
Sergey Chernov 2024-01-28 16:06:48 +03:00
parent 47b450c597
commit 3e6c487601
5 changed files with 287 additions and 522 deletions

View File

@ -2,6 +2,12 @@
Multiplatform binary tools collection, including portable serialization of the compact and fast [Bipack] format, and many useful tools to work with binary data, like CRC family checksums, dumps, etc. It works well also in the browser and in native targets. Multiplatform binary tools collection, including portable serialization of the compact and fast [Bipack] format, and many useful tools to work with binary data, like CRC family checksums, dumps, etc. It works well also in the browser and in native targets.
# Recent changes
- 0.1.0: uses modern kotlin 1.9.*, fixes problem with singleton or empty/object serialization
last 1.8 version is 0.0.8, some fixes are not yet backported to it pls leave an issue of needed.
# Usage # Usage
Add our maven: Add our maven:
@ -18,7 +24,7 @@ And add dependecy to the proper place in yuor project like this:
```kotlin ```kotlin
dependencies { dependencies {
// ... // ...
implementation("net.sergeych:mp_bintools:0.0.6") implementation("net.sergeych:mp_bintools:0.1.0")
} }
``` ```

View File

@ -1,14 +1,14 @@
plugins { plugins {
kotlin("multiplatform") version "1.8.20" kotlin("multiplatform") version "1.9.22"
kotlin("plugin.serialization") version "1.8.20" kotlin("plugin.serialization") version "1.9.22"
id("org.jetbrains.dokka") version "1.6.0" id("org.jetbrains.dokka") version "1.9.10"
`maven-publish` `maven-publish`
} }
val serialization_version = "1.3.4" val serialization_version = "1.3.4"
group = "net.sergeych" group = "net.sergeych"
version = "0.0.7" version = "0.1.0"
repositories { repositories {
mavenCentral() mavenCentral()

File diff suppressed because it is too large Load Diff

View File

@ -31,18 +31,20 @@ class BipackDecoder(
override fun decodeBoolean(): Boolean = input.readByte().toInt() != 0 override fun decodeBoolean(): Boolean = input.readByte().toInt() != 0
override fun decodeByte(): Byte = input.readByte() override fun decodeByte(): Byte = input.readByte()
override fun decodeShort(): Short = override fun decodeShort(): Short =
if( fixedNumber ) input.readI16() if (fixedNumber) input.readI16()
else if (nextIsUnsigned) else if (nextIsUnsigned)
input.readNumber<UInt>().toShort() input.readNumber<UInt>().toShort()
else else
input.readNumber() input.readNumber()
override fun decodeInt(): Int = override fun decodeInt(): Int =
if (fixedNumber) input.readI32() if (fixedNumber) input.readI32()
else if (nextIsUnsigned) input.readNumber<UInt>().toInt() else input.readNumber() else if (nextIsUnsigned) input.readNumber<UInt>().toInt() else input.readNumber()
override fun decodeLong(): Long = override fun decodeLong(): Long =
if( fixedNumber ) input.readI64() if (fixedNumber) input.readI64()
else if (nextIsUnsigned) input.readNumber<ULong>().toLong() else input.readNumber() else if (nextIsUnsigned) input.readNumber<ULong>().toLong() else input.readNumber()
override fun decodeFloat(): Float = input.readFloat() override fun decodeFloat(): Float = input.readFloat()
override fun decodeDouble(): Double = input.readDouble() override fun decodeDouble(): Double = input.readDouble()
override fun decodeChar(): Char = Char(input.readNumber<UInt>().toInt()) override fun decodeChar(): Char = Char(input.readNumber<UInt>().toInt())
@ -57,7 +59,7 @@ class BipackDecoder(
override fun decodeEnum(enumDescriptor: SerialDescriptor): Int = input.readNumber<UInt>().toInt() override fun decodeEnum(enumDescriptor: SerialDescriptor): Int = input.readNumber<UInt>().toInt()
override fun decodeElementIndex(descriptor: SerialDescriptor): Int { override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
if (elementIndex >= elementsCount || input.isEnd() == true ) if (elementIndex >= elementsCount)
return CompositeDecoder.DECODE_DONE return CompositeDecoder.DECODE_DONE
nextIsUnsigned = false nextIsUnsigned = false
for (a in descriptor.getElementAnnotations(elementIndex)) { for (a in descriptor.getElementAnnotations(elementIndex)) {
@ -71,11 +73,13 @@ class BipackDecoder(
} }
override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T { override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T {
return if( deserializer == Instant.serializer() ) return if (deserializer == Instant.serializer())
Instant.fromEpochMilliseconds(decodeLong()) as T Instant.fromEpochMilliseconds(decodeLong()) as T
else else
super.decodeSerializableValue(deserializer) super.decodeSerializableValue(deserializer)
} }
override fun decodeSequentially(): Boolean = isCollection override fun decodeSequentially(): Boolean = isCollection
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder { override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {
@ -122,7 +126,11 @@ class BipackDecoder(
super.endStructure(descriptor) super.endStructure(descriptor)
} }
override fun decodeNotNullMark(): Boolean = decodeBoolean() override fun decodeNotNullMark(): Boolean = try {
decodeBoolean()
} catch (_: DataSource.EndOfData) {
false
}
@ExperimentalSerializationApi @ExperimentalSerializationApi
override fun decodeNull(): Nothing? = null override fun decodeNull(): Nothing? = null

View File

@ -46,6 +46,13 @@ data class FoobarF3(val bar: Int, val foo: Int, val other: Int = -1)
@CrcProtected() @CrcProtected()
data class FoobarFP1(val bar: Int, val foo: Int, val other: Int = -1) data class FoobarFP1(val bar: Int, val foo: Int, val other: Int = -1)
@Serializable
sealed class SC1 {
@Serializable
class Nested: SC1()
}
class BipackEncoderTest { class BipackEncoderTest {
@Serializable @Serializable
@ -349,7 +356,7 @@ class BipackEncoderTest {
@Test @Test
fun testInstant() { fun testInstant() {
val x = Clock.System.now() val x = Clock.System.now()
// println( BipackEncoder.encode(x).toDump() ) println( BipackEncoder.encode(x).toDump() )
val y = BipackDecoder.decode<Instant>(BipackEncoder.encode(x)) val y = BipackDecoder.decode<Instant>(BipackEncoder.encode(x))
assertEquals(x.toEpochMilliseconds(), y.toEpochMilliseconds()) assertEquals(x.toEpochMilliseconds(), y.toEpochMilliseconds())
} }
@ -405,4 +412,30 @@ class BipackEncoderTest {
println(sf) println(sf)
} }
@Serializable
enum class TU1 {
N1, N2, N3, N4
}
@Test
fun testUnsignedEnums() {
val p1 = BipackEncoder.encode(TU1.N4)
// val p2 = BipackEncoder.encode(3u)
println(p1.toDump())
// println(p2.toDump())
val t2 = BipackDecoder.decode<TU1>(p1)
assertEquals(TU1.N4, t2)
assertEquals(0x0cu, p1[0].toUByte())
}
@Test
fun testClosedSerialization() {
val x: SC1 = SC1.Nested()
val b = BipackEncoder.encode(x)
println(b.toDump())
val y = BipackDecoder.decode<SC1>(b)
println(y)
}
} }