Test for binary serialization

This commit is contained in:
Alexander Nozik 2018-10-26 16:15:27 +03:00
parent 13a00d0d19
commit a7a1f776a7
5 changed files with 68 additions and 153 deletions

View File

@ -1,6 +1,6 @@
plugins {
id 'kotlin-multiplatform'
id 'kotlinx-serialization'
//id 'kotlinx-serialization'
}
repositories {
maven { url = 'http://dl.bintray.com/kotlin/kotlin-eap' }
@ -19,27 +19,41 @@ kotlin {
commonMain {
dependencies {
api project(":dataforge-meta")
implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
//implementation 'org.jetbrains.kotlin:kotlin-reflect'
//implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
implementation "org.jetbrains.kotlinx:kotlinx-io:$kotlinx_io_version"
}
}
commonTest {}
commonTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test-common'
implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
}
}
jvmMain {
dependencies {
implementation 'com.github.cliftonlabs:json-simple:3.0.2'
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
//implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
implementation "org.jetbrains.kotlinx:kotlinx-io-jvm:$kotlinx_io_version"
}
}
jvmTest {}
jvmTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test'
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
}
}
jsMain {
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serialization_version"
//implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serialization_version"
implementation "org.jetbrains.kotlinx:kotlinx-io-js:$kotlinx_io_version"
}
}
jsTest {}
jsTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test-js'
}
}
// iosMain {
// }
// iosTest {

View File

@ -1,10 +1,7 @@
package hep.dataforge.meta.io
import hep.dataforge.meta.*
import kotlinx.io.core.Input
import kotlinx.io.core.Output
import kotlinx.io.core.readText
import kotlinx.io.core.writeText
import kotlinx.io.core.*
/**
* A format for meta serialization
@ -13,8 +10,18 @@ interface MetaFormat {
val name: String
val key: Short
suspend fun write(meta: Meta, out: Output)
suspend fun read(input: Input): Meta
fun write(meta: Meta, out: Output)
fun read(input: Input): Meta
}
fun MetaFormat.stringify(meta: Meta): String {
val builder = BytePacketBuilder()
write(meta,builder)
return builder.build().readText()
}
fun MetaFormat.parse(str: String): Meta{
return read(ByteReadPacket(str.toByteArray()))
}
///**
@ -34,20 +41,20 @@ object JSONMetaFormat : MetaFormat {
override val name: String = "json"
override val key: Short = 0x4a53//"JS"
override suspend fun write(meta: Meta, out: Output) = writeJson(meta, out)
override suspend fun read(input: Input): Meta = readJson(input)
override fun write(meta: Meta, out: Output) = writeJson(meta, out)
override fun read(input: Input): Meta = readJson(input)
}
object BinaryMetaFormat : MetaFormat {
override val name: String = "bin"
override val key: Short = 0x4249//BI
override suspend fun write(meta: Meta, out: Output) {
override fun write(meta: Meta, out: Output) {
out.writeMeta(meta)
}
override suspend fun read(input: Input): Meta {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
override fun read(input: Input): Meta {
return (input.readMetaItem() as MetaItem.SingleNodeItem).node
}
private fun Output.writeChar(char: Char) = writeByte(char.toByte())

View File

@ -1,128 +0,0 @@
package hep.dataforge.meta.io
import hep.dataforge.meta.*
import kotlinx.serialization.json.*
/*Direct JSON serialization*/
//fun Value.toJson(): JsonElement = if (isList()) {
// JsonArray(list.map { it.toJson() })
//} else {
// when (type) {
// ValueType.NUMBER -> JsonPrimitive(number)
// ValueType.STRING -> JsonPrimitive(string)
// ValueType.BOOLEAN -> JsonPrimitive(boolean)
// ValueType.NULL -> JsonNull
// }
//}
//
//fun Meta.toJSON(): JsonObject {
// val map = this.items.mapValues { (_, value) ->
// when (value) {
// is MetaItem.ValueItem -> value.value.toJson()
// is MetaItem.SingleNodeItem -> value.node.toJSON()
// is MetaItem.MultiNodeItem -> JsonArray(value.nodes.map { it.toJSON() })
// }
// }
// return JsonObject(map)
//}
//
//fun JsonPrimitive.toValue(): Value {
// return when (this) {
// is JsonLiteral -> LazyParsedValue(content)
// is JsonNull -> Null
// }
//}
//
//fun JsonObject.toMeta(): Meta {
// return buildMeta {
// this@toMeta.forEach { (key, value) ->
// when (value) {
// is JsonPrimitive -> set(key, value.toValue())
// is JsonObject -> set(key, value.toMeta())
// is JsonArray -> if (value.all { it is JsonPrimitive }) {
// set(key, ListValue(value.map { (it as JsonPrimitive).toValue() }))
// } else {
// set(
// key,
// value.map {
// if (it is JsonObject) {
// it.toMeta()
// } else {
// buildMeta { "@value" to it.primitive.toValue() }
// }
// }
// )
// }
// }
// }
// }
//}
/*Direct CBOR serialization*/
//fun Meta.toBinary(out: OutputStream) {
// fun CBOR.CBOREncoder.encodeChar(char: Char) {
// encodeNumber(char.toByte().toLong())
// }
//
//
// fun CBOR.CBOREncoder.encodeMeta(meta: Meta) {
// meta.items.forEach { (key, item) ->
// this.startMap()
// encodeString(key)
// when (item) {
// is MetaItem.ValueItem -> {
// encodeChar('V')
// encodeValue(item.value)
// }
// is MetaItem.SingleNodeItem -> {
// startArray()
// encodeMeta(item.node)
// }
// is MetaItem.MultiNodeItem -> {
// startArray()
// item.nodes.forEach {
// encodeMeta(it)
// }
// end()
// }
// }
// }
// }
//
//
// CBOR.CBOREncoder(out).apply {
// encodeMeta(this@toBinary)
// }
//}
//
//fun InputStream.readBinaryMeta(): Meta {
// fun CBOR.CBORDecoder.nextChar(): Char = nextNumber().toByte().toChar()
//
// fun CBOR.CBORDecoder.nextValue(): Value {
// val key = nextChar()
// return when(key){
// 'L' -> {
// val size = startArray()
// val res = (0 until size).map { nextValue() }
// end()
// ListValue(res)
// }
// 'S' -> StringValue(nextString())
// 'N' -> Null
// '+' -> True
// '-' -> False
// 'i' -> NumberValue(nextNumber())
// 'f' -> NumberValue(nextFloat())
// 'd' -> NumberValue(nextDouble())
// else -> error("Unknown binary key: $key")
// }
// }
//
// fun CBOR.CBORDecoder.nextMeta(): Meta{
//
// }
//
//}

View File

@ -0,0 +1,22 @@
package hep.dataforge.meta.io
import hep.dataforge.meta.buildMeta
import kotlin.test.Test
import kotlin.test.assertEquals
class MetaFormatTest{
@Test
fun testBinaryMetaFormat(){
val meta = buildMeta {
"a" to 22
"node" to {
"b" to "DDD"
"c" to 11.1
}
}
val string = BinaryMetaFormat.stringify(meta)
val result = BinaryMetaFormat.parse(string)
assertEquals(meta,result)
}
}

View File

@ -23,8 +23,8 @@ kotlin {
}
commonTest {
dependencies {
api 'org.jetbrains.kotlin:kotlin-test-common'
api 'org.jetbrains.kotlin:kotlin-test-annotations-common'
implementation 'org.jetbrains.kotlin:kotlin-test-common'
implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
}
}
jvmMain {
@ -34,8 +34,8 @@ kotlin {
}
jvmTest {
dependencies {
api 'org.jetbrains.kotlin:kotlin-test'
api 'org.jetbrains.kotlin:kotlin-test-junit'
implementation 'org.jetbrains.kotlin:kotlin-test'
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
}
}
jsMain {
@ -45,7 +45,7 @@ kotlin {
}
jsTest {
dependencies {
api 'org.jetbrains.kotlin:kotlin-test-js'
implementation 'org.jetbrains.kotlin:kotlin-test-js'
}
}
// iosMain {