From da2c59fdfe1698080b668a1b3b2dc58854f8f8fe Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 30 Apr 2019 21:04:55 +0300 Subject: [PATCH] Fixed JsonMetaForat and ListValue equality --- .../kotlin/hep/dataforge/io/JsonMetaFormat.kt | 14 +++++++++----- .../kotlin/hep/dataforge/io/MetaFormatTest.kt | 1 + .../kotlin/hep/dataforge/values/Value.kt | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/JsonMetaFormat.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/JsonMetaFormat.kt index bd6fa81a..9d7b5739 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/JsonMetaFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/JsonMetaFormat.kt @@ -32,11 +32,15 @@ object JsonMetaFormat : MetaFormat { } fun Value.toJson(): JsonElement { - return when (type) { - ValueType.NUMBER -> JsonPrimitive(number) - ValueType.STRING -> JsonPrimitive(string) - ValueType.BOOLEAN -> JsonPrimitive(boolean) - ValueType.NULL -> JsonNull + return 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 + } } } diff --git a/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaFormatTest.kt b/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaFormatTest.kt index fbaa238e..e177e4d8 100644 --- a/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaFormatTest.kt +++ b/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaFormatTest.kt @@ -26,6 +26,7 @@ class MetaFormatTest { "node" to { "b" to "DDD" "c" to 11.1 + "array" to doubleArrayOf(1.0,2.0,3.0) } } val string = meta.asString(JsonMetaFormat) diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt index c6e63645..89d1519c 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt @@ -178,6 +178,22 @@ class ListValue(override val list: List) : Value { override val string: String get() = list.first().string override fun toString(): String = value.toString() + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is Value) return false + val otherList = other.list + if (list.size != otherList.size) return false + + + return (0 until list.size).all { list[it] == otherList[it] } + } + + override fun hashCode(): Int { + return list.hashCode() + } + + } /**