optional descriptor for descriptor builder

This commit is contained in:
Alexander Nozik 2019-11-01 21:15:55 +03:00
parent 33b1de2865
commit aefee64581
3 changed files with 24 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package hep.dataforge.io
package hep.dataforge.io.serialization
import hep.dataforge.io.serialization.descriptor
import hep.dataforge.io.toJson
import hep.dataforge.io.toMeta
import hep.dataforge.meta.*
import hep.dataforge.names.NameToken
import hep.dataforge.values.*
@ -16,10 +17,10 @@ object ValueSerializer : KSerializer<Value> {
private val valueTypeSerializer = EnumSerializer(ValueType::class)
private val listSerializer by lazy { ArrayListSerializer(ValueSerializer) }
override val descriptor: SerialDescriptor = descriptor("Value") {
override val descriptor: SerialDescriptor = descriptor("hep.dataforge.values.Value") {
boolean("isList")
enum<ValueType>("valueType")
element("value", PolymorphicClassDescriptor)
element("value", null)
}
private fun Decoder.decodeValue(): Value {
@ -69,7 +70,7 @@ object ValueSerializer : KSerializer<Value> {
object MetaItemSerializer : KSerializer<MetaItem<*>> {
override val descriptor: SerialDescriptor = descriptor("MetaItem") {
boolean("isNode")
element("value", PolymorphicClassDescriptor)
element("value", null)
}
@ -93,17 +94,21 @@ object MetaItemSerializer : KSerializer<MetaItem<*>> {
private class DeserializedMeta(override val items: Map<NameToken, MetaItem<*>>) : MetaBase()
/**
* Serialized for meta
*/
@Serializer(Meta::class)
object MetaSerializer : KSerializer<Meta> {
private val mapSerializer =
HashMapSerializer(StringSerializer, MetaItemSerializer)
private val mapSerializer = HashMapSerializer(
StringSerializer,
MetaItemSerializer
)
override val descriptor: SerialDescriptor =
NamedMapClassDescriptor("Meta", StringSerializer.descriptor, MetaItemSerializer.descriptor)
override val descriptor: SerialDescriptor = NamedMapClassDescriptor(
"hep.dataforge.meta.Meta",
StringSerializer.descriptor,
MetaItemSerializer.descriptor
)
override fun deserialize(decoder: Decoder): Meta {
return if (decoder is JsonInput) {

View File

@ -12,12 +12,12 @@ import kotlinx.serialization.internal.*
inline class SerialDescriptorBuilder(private val impl: SerialClassDescImpl) {
fun element(
name: String,
descriptor: SerialDescriptor,
descriptor: SerialDescriptor?,
isOptional: Boolean = false,
vararg annotations: Annotation
) {
impl.addElement(name, isOptional)
impl.pushDescriptor(descriptor)
descriptor?.let { impl.pushDescriptor(descriptor) }
annotations.forEach {
impl.pushAnnotation(it)
}
@ -57,7 +57,7 @@ inline class SerialDescriptorBuilder(private val impl: SerialClassDescImpl) {
fun doubleArray(name: String, isOptional: Boolean = false, vararg annotations: Annotation) =
element(name, DoubleArraySerializer.descriptor, isOptional, *annotations)
inline fun <reified E: Enum<E>> enum(name: String, isOptional: Boolean = false, vararg annotations: Annotation) =
inline fun <reified E : Enum<E>> enum(name: String, isOptional: Boolean = false, vararg annotations: Annotation) =
element(name, EnumSerializer(E::class).descriptor, isOptional, *annotations)
fun classAnnotation(a: Annotation) = impl.pushClassAnnotation(a)
@ -65,7 +65,10 @@ inline class SerialDescriptorBuilder(private val impl: SerialClassDescImpl) {
fun build(): SerialDescriptor = impl
}
inline fun KSerializer<*>.descriptor(name: String, block: SerialDescriptorBuilder.() -> Unit): SerialDescriptor =
inline fun <reified T : Any> KSerializer<T>.descriptor(
name: String,
block: SerialDescriptorBuilder.() -> Unit
): SerialDescriptor =
SerialDescriptorBuilder(SerialClassDescImpl(name)).apply(block).build()
fun Decoder.decodeStructure(

View File

@ -1,5 +1,7 @@
package hep.dataforge.io
import hep.dataforge.io.serialization.MetaItemSerializer
import hep.dataforge.io.serialization.MetaSerializer
import hep.dataforge.io.serialization.NameSerializer
import hep.dataforge.meta.buildMeta
import hep.dataforge.names.toName