serialization for meta (not fully functional yet)

This commit is contained in:
Alexander Nozik 2018-09-19 10:15:25 +03:00
parent ad9002530d
commit 1926d157ee
20 changed files with 169 additions and 37 deletions

View File

@ -1,5 +1,15 @@
plugins { buildscript {
id 'kotlin-platform-common' version '1.2.70' ext.kotlin_version = '1.2.70'
ext.serialization_version = '0.6.2'
repositories {
jcenter()
maven { url "https://kotlin.bintray.com/kotlinx" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlinx:kotlinx-gradle-serialization-plugin:$serialization_version"
}
} }
description = "The basic interfaces for DataForge meta-data" description = "The basic interfaces for DataForge meta-data"
@ -7,17 +17,10 @@ description = "The basic interfaces for DataForge meta-data"
group 'hep.dataforge' group 'hep.dataforge'
version '0.1.1-SNAPSHOT' version '0.1.1-SNAPSHOT'
subprojects{
repositories { repositories {
mavenCentral() jcenter()
} maven { url "https://kotlin.bintray.com/kotlinx" }
//maven { url 'https://jitpack.io' }
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-common"
testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common"
testCompile "org.jetbrains.kotlin:kotlin-test-common"
}
kotlin {
experimental {
coroutines "enable"
} }
} }

View File

@ -0,0 +1,17 @@
plugins {
id 'kotlin-platform-common'
id 'kotlinx-serialization'
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common"
testCompile "org.jetbrains.kotlin:kotlin-test-common"
}
kotlin {
experimental {
coroutines "enable"
}
}

View File

@ -2,6 +2,7 @@ package hep.dataforge.meta
import hep.dataforge.names.Name import hep.dataforge.names.Name
import hep.dataforge.names.toName import hep.dataforge.names.toName
import kotlinx.serialization.Serializable
/** /**
* A member of the meta tree. Could be represented as one of following: * A member of the meta tree. Could be represented as one of following:
@ -30,6 +31,7 @@ operator fun <M : Meta> List<M>.get(query: String): M? {
* * [MetaItem.SingleNodeItem] single node * * [MetaItem.SingleNodeItem] single node
* * [MetaItem.MultiNodeItem] multi-value node * * [MetaItem.MultiNodeItem] multi-value node
*/ */
@Serializable
interface Meta { interface Meta {
val items: Map<String, MetaItem<out Meta>> val items: Map<String, MetaItem<out Meta>>
} }

View File

@ -0,0 +1,95 @@
package hep.dataforge.meta
import kotlinx.serialization.*
import kotlinx.serialization.internal.SerialClassDescImpl
@Serializer(forClass = Value::class)
object ValueSerializer : KSerializer<Value> {
override val serialClassDesc: KSerialClassDesc = SerialClassDescImpl("Value")
override fun load(input: KInput): Value {
val key = input.readByteValue()
return when (key.toChar()) {
'S' -> StringValue(input.readStringValue())
'd' -> NumberValue(input.readDoubleValue())
'f' -> NumberValue(input.readFloatValue())
'i' -> NumberValue(input.readIntValue())
's' -> NumberValue(input.readShortValue())
'l' -> NumberValue(input.readLongValue())
'b' -> NumberValue(input.readByteValue())
'+' -> True
'-' -> False
'N' -> Null
'L' -> {
val size = input.readIntValue()
val list = (0 until size).map { load(input) }
ListValue(list)
}
else -> error("Unknown value deserialization ket '$key'")
}
}
override fun save(output: KOutput, obj: Value) {
when (obj.type) {
ValueType.NUMBER -> {
val number = obj.number
when (number) {
is Float -> {
output.writeByteValue('f'.toByte())
output.writeFloatValue(number)
}
is Short -> {
output.writeByteValue('s'.toByte())
output.writeShortValue(number)
}
is Int -> {
output.writeByteValue('i'.toByte())
output.writeIntValue(number)
}
is Long -> {
output.writeByteValue('l'.toByte())
output.writeLongValue(number)
}
is Byte -> {
output.writeByteValue('b'.toByte())
output.writeByteValue(number)
}
is Double -> {
output.writeByteValue('d'.toByte())
output.writeDoubleValue(number)
}
else -> {
//TODO add warning
output.writeByteValue('d'.toByte())
output.writeDoubleValue(number.toDouble())
}
}
}
ValueType.STRING -> {
output.writeByteValue('S'.toByte())
output.writeStringValue(obj.string)
}
ValueType.BOOLEAN -> if (obj.boolean) {
output.writeByteValue('+'.toByte())
} else {
output.writeByteValue('-'.toByte())
}
ValueType.NULL -> output.writeByteValue('N'.toByte())
}
}
}
//@Serializer(forClass = Meta::class)
//object MetaSerializer: KSerializer<Meta>{
// override val serialClassDesc: KSerialClassDesc = SerialClassDescImpl("Meta")
//
// override fun load(input: KInput): Meta {
//
// }
//
// override fun save(output: KOutput, obj: Meta) {
// NamedValueOutput()
// }
//
//}

View File

@ -1,5 +1,7 @@
package hep.dataforge.meta package hep.dataforge.meta
import kotlinx.serialization.Serializable
/** /**
* The list of supported Value types. * The list of supported Value types.
@ -16,6 +18,7 @@ enum class ValueType {
* *
* Value can represent a list of value objects. * Value can represent a list of value objects.
*/ */
@Serializable(with = ValueSerializer::class)
interface Value { interface Value {
/** /**
* Get raw value of this value * Get raw value of this value

View File

@ -12,7 +12,8 @@ class MetaDelegateTest {
@Test @Test
fun delegateTest() { fun delegateTest() {
val testObject = object : SimpleConfigurable(Config()) { val testObject = object : Specification {
override val config: Config = Config()
var myValue by string() var myValue by string()
var safeValue by number(2.2) var safeValue by number(2.2)
var enumValue by enum(TestEnum.YES) var enumValue by enum(TestEnum.YES)

View File

@ -0,0 +1,22 @@
package scientifik.kplot.remote
import hep.dataforge.meta.buildMeta
import hep.dataforge.meta.get
import hep.dataforge.meta.value
import kotlinx.serialization.json.JSON
import kotlin.test.Test
class SerializationTest{
@Test
fun testMetaSerialization(){
val meta = buildMeta {
"a" to 2
"b" to {
"c" to "ddd"
"d" to 2.2
}
}
val json = JSON.stringify(meta["a"]?.value!!)
println(json)
}
}

View File

@ -1,17 +1,12 @@
plugins { plugins {
id 'kotlin-platform-js' id 'kotlin-platform-js'
} //id 'kotlinx-serialization'
group 'hep.dataforge'
version '0.1.0-SNAPSHOT'
repositories {
mavenCentral()
} }
dependencies { dependencies {
expectedBy rootProject expectedBy project(":dataforge-meta-common")
compile "org.jetbrains.kotlin:kotlin-stdlib-js" compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serialization_version"
testCompile "org.jetbrains.kotlin:kotlin-test-js" testCompile "org.jetbrains.kotlin:kotlin-test-js"
} }

View File

@ -1,21 +1,15 @@
plugins { plugins {
id 'kotlin-platform-jvm' id 'kotlin-platform-jvm'
} id 'kotlinx-serialization'
group 'hep.dataforge'
version '0.1.0-SNAPSHOT'
repositories {
mavenCentral()
} }
dependencies { dependencies {
expectedBy rootProject expectedBy project(":dataforge-meta-common")
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testCompile "junit:junit:4.12" compile "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
testCompile "org.jetbrains.kotlin:kotlin-test" testCompile "org.jetbrains.kotlin:kotlin-test"
testCompile "org.jetbrains.kotlin:kotlin-test-junit" testCompile "org.jetbrains.kotlin:kotlin-test-junit5"
} }
compileKotlin { compileKotlin {

View File

@ -4,18 +4,18 @@ pluginManagement {
if (requested.id.id == "kotlin-platform-common") { if (requested.id.id == "kotlin-platform-common") {
useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}") useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}")
} }
if (requested.id.id == "kotlin-platform-jvm") { if (requested.id.id == "kotlin-platform-jvm") {
useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}") useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}")
} }
if (requested.id.id == "kotlin-platform-js") { if (requested.id.id == "kotlin-platform-js") {
useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}") useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}")
} }
} }
} }
} }
rootProject.name = 'dataforge-meta' rootProject.name = 'dataforge-meta'
include ":dataforge-meta-common"
include ":dataforge-meta-jvm" include ":dataforge-meta-jvm"
include ":dataforge-meta-js" include ":dataforge-meta-js"