diff --git a/dataforge-meta-io/build.gradle b/dataforge-meta-io/build.gradle index 26c0493d..00ecb138 100644 --- a/dataforge-meta-io/build.gradle +++ b/dataforge-meta-io/build.gradle @@ -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 { diff --git a/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/MetaFormat.kt b/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/MetaFormat.kt index 0f0a662d..7fd73460 100644 --- a/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/MetaFormat.kt +++ b/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/MetaFormat.kt @@ -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()) @@ -155,7 +162,7 @@ object BinaryMetaFormat : MetaFormat { (1..length).forEach { _ -> val name = readString() val item = readMetaItem() - set(name,item) + set(name, item) } } MetaItem.SingleNodeItem(meta) diff --git a/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/Serializers.kt b/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/Serializers.kt deleted file mode 100644 index d1811a72..00000000 --- a/dataforge-meta-io/src/commonMain/kotlin/hep/dataforge/meta/io/Serializers.kt +++ /dev/null @@ -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{ -// -// } -// -//} \ No newline at end of file diff --git a/dataforge-meta-io/src/commonTest/kotlin/hep/dataforge/meta/io/MetaFormatTest.kt b/dataforge-meta-io/src/commonTest/kotlin/hep/dataforge/meta/io/MetaFormatTest.kt new file mode 100644 index 00000000..1a4e9060 --- /dev/null +++ b/dataforge-meta-io/src/commonTest/kotlin/hep/dataforge/meta/io/MetaFormatTest.kt @@ -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) + } + +} \ No newline at end of file diff --git a/dataforge-meta/build.gradle b/dataforge-meta/build.gradle index bba6b93b..f37628a2 100644 --- a/dataforge-meta/build.gradle +++ b/dataforge-meta/build.gradle @@ -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 {