From a0adff99752e582b7fa0277e82bdf9d4b1b56942 Mon Sep 17 00:00:00 2001 From: crc-32 Date: Sat, 5 Mar 2022 03:23:16 +0000 Subject: [PATCH] Fix StringOrBoolean + test for both --- .../metadata/StringOrBoolean.kt | 19 ++++++++++++------- .../libpebblecommon/util/SerializationUtil.kt | 5 +++++ .../metadata/pbw/appinfo/TestAppInfo.kt | 7 ++++--- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/commonMain/kotlin/io/rebble/libpebblecommon/metadata/StringOrBoolean.kt b/src/commonMain/kotlin/io/rebble/libpebblecommon/metadata/StringOrBoolean.kt index 2ef721c..527dc11 100644 --- a/src/commonMain/kotlin/io/rebble/libpebblecommon/metadata/StringOrBoolean.kt +++ b/src/commonMain/kotlin/io/rebble/libpebblecommon/metadata/StringOrBoolean.kt @@ -1,9 +1,11 @@ package io.rebble.libpebblecommon.metadata -import kotlinx.serialization.Serializable -import kotlinx.serialization.Serializer -import kotlinx.serialization.KSerializer +import kotlinx.serialization.* import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.JsonContentPolymorphicSerializer +import kotlinx.serialization.json.JsonDecoder +import kotlinx.serialization.json.JsonElement +import kotlinx.serialization.json.jsonPrimitive @Serializable data class StringOrBoolean(val value: Boolean) { @@ -13,10 +15,13 @@ data class StringOrBoolean(val value: Boolean) { encoder.encodeString(if (value.value) "true" else "false") } - override fun deserialize(decoder: Decoder): StringOrBoolean = try { - StringOrBoolean(decoder.decodeString() == "true") - } catch (e: Error) { - StringOrBoolean(decoder.decodeBoolean()) + override fun deserialize(decoder: Decoder): StringOrBoolean { + require(decoder is JsonDecoder) + val element = decoder.decodeJsonElement() + if (element.jsonPrimitive.content != "true" && element.jsonPrimitive.content != "false") { + throw SerializationException("StringOrBoolean value is not a boolean keyword") + } + return StringOrBoolean(element.jsonPrimitive.content == "true") } } } \ No newline at end of file diff --git a/src/commonMain/kotlin/io/rebble/libpebblecommon/util/SerializationUtil.kt b/src/commonMain/kotlin/io/rebble/libpebblecommon/util/SerializationUtil.kt index 081fc60..5df6744 100644 --- a/src/commonMain/kotlin/io/rebble/libpebblecommon/util/SerializationUtil.kt +++ b/src/commonMain/kotlin/io/rebble/libpebblecommon/util/SerializationUtil.kt @@ -2,15 +2,20 @@ package io.rebble.libpebblecommon.util import io.rebble.libpebblecommon.metadata.pbw.appinfo.PbwAppInfo import io.rebble.libpebblecommon.metadata.pbw.manifest.PbwManifest +import kotlinx.serialization.SerializationException import kotlinx.serialization.decodeFromString import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json object SerializationUtil { private val json = Json { ignoreUnknownKeys = true } + @Throws(SerializationException::class) fun serializeAppInfo(appInfo: PbwAppInfo): String = json.encodeToString(appInfo) + @Throws(SerializationException::class) fun deserializeAppInfo(jsonString: String): PbwAppInfo = json.decodeFromString(jsonString) + @Throws(SerializationException::class) fun serializeManifest(manifest: PbwManifest): String = json.encodeToString(manifest) + @Throws(SerializationException::class) fun deserializeManifest(jsonString: String): PbwManifest = json.decodeFromString(jsonString) } \ No newline at end of file diff --git a/src/commonTest/kotlin/io/rebble/libpebblecommon/metadata/pbw/appinfo/TestAppInfo.kt b/src/commonTest/kotlin/io/rebble/libpebblecommon/metadata/pbw/appinfo/TestAppInfo.kt index 990e6c2..fe7369b 100644 --- a/src/commonTest/kotlin/io/rebble/libpebblecommon/metadata/pbw/appinfo/TestAppInfo.kt +++ b/src/commonTest/kotlin/io/rebble/libpebblecommon/metadata/pbw/appinfo/TestAppInfo.kt @@ -5,11 +5,12 @@ import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import kotlin.test.Test import kotlin.test.assertEquals +import io.rebble.libpebblecommon.metadata.StringOrBoolean class TestAppInfo { companion object { const val APP_INFO_JSON_SIMPLICITY = "{\"versionLabel\": \"3.2\", \"companyName\": \"Pebble Technology\", \"targetPlatforms\": [\"aplite\", \"basalt\", \"chalk\"], \"resources\": {\"media\": [{\"name\": \"IMAGE_MENU_ICON\", \"type\": \"png\", \"file\": \"images/menu_icon_simplicity.png\", \"menuIcon\": true}]}, \"sdkVersion\": \"3\", \"longName\": \"Simplicity\", \"watchapp\": {\"watchface\": true}, \"projectType\": \"native\", \"capabilities\": [\"\"], \"uuid\": \"54eada19-67fd-4947-a7c5-256f24e3a7d7\", \"shortName\": \"Simplicity\", \"appKeys\": {}}" - const val APP_INFO_JSON_DIALER_FOR_PEBBLE = "{\"targetPlatforms\":[\"aplite\",\"basalt\",\"chalk\"],\"projectType\":\"native\",\"messageKeys\":{},\"companyName\":\"matejdro\",\"enableMultiJS\":false,\"watchapp\":{\"onlyShownOnCommunication\":false,\"hiddenApp\":false,\"watchface\":false},\"versionLabel\":\"3.3\",\"longName\":\"Dialer\",\"shortName\":\"Dialer\",\"name\":\"Dialer\",\"sdkVersion\":\"3\",\"displayName\":\"Dialer\",\"uuid\":\"158a074d-85ce-43d2-ab7d-14416ddc1058\",\"appKeys\":{},\"capabilities\":[],\"resources\":{\"media\":[{\"menuIcon\":true,\"type\":\"bitmap\",\"name\":\"ICON\",\"file\":\"icon.png\"},{\"type\":\"bitmap\",\"name\":\"ANSWER\",\"file\":\"answer.png\"},{\"type\":\"bitmap\",\"name\":\"ENDCALL\",\"file\":\"endcall.png\"},{\"type\":\"bitmap\",\"name\":\"MIC_OFF\",\"file\":\"mic_off.png\"},{\"type\":\"bitmap\",\"name\":\"MIC_ON\",\"file\":\"micon.png\"},{\"type\":\"bitmap\",\"name\":\"SPEAKER_ON\",\"file\":\"speakeron.png\"},{\"type\":\"bitmap\",\"name\":\"SPEAKER_OFF\",\"file\":\"speakeroff.png\"},{\"type\":\"bitmap\",\"name\":\"VOLUME_UP\",\"file\":\"volumeup.png\"},{\"type\":\"bitmap\",\"name\":\"VOLUME_DOWN\",\"file\":\"volumedown.png\"},{\"type\":\"bitmap\",\"name\":\"CALL_HISTORY\",\"file\":\"callhistory.png\"},{\"type\":\"bitmap\",\"name\":\"CONTACTS\",\"file\":\"contacts.png\"},{\"type\":\"bitmap\",\"name\":\"CONTACT_GROUP\",\"file\":\"contactgroup.png\"},{\"type\":\"bitmap\",\"name\":\"INCOMING_CALL\",\"file\":\"incomingcall.png\"},{\"type\":\"bitmap\",\"name\":\"OUTGOING_CALL\",\"file\":\"outgoingcall.png\"},{\"type\":\"bitmap\",\"name\":\"MISSED_CALL\",\"file\":\"missedcall.png\"},{\"type\":\"bitmap\",\"name\":\"MESSAGE\",\"file\":\"message.png\"},{\"type\":\"bitmap\",\"name\":\"CALL\",\"file\":\"call.png\"}]}}" + const val APP_INFO_JSON_DIALER_FOR_PEBBLE = "{\"targetPlatforms\":[\"aplite\",\"basalt\",\"chalk\"],\"projectType\":\"native\",\"messageKeys\":{},\"companyName\":\"matejdro\",\"enableMultiJS\":false,\"watchapp\":{\"onlyShownOnCommunication\":false,\"hiddenApp\":false,\"watchface\":false},\"versionLabel\":\"3.3\",\"longName\":\"Dialer\",\"shortName\":\"Dialer\",\"name\":\"Dialer\",\"sdkVersion\":\"3\",\"displayName\":\"Dialer\",\"uuid\":\"158a074d-85ce-43d2-ab7d-14416ddc1058\",\"appKeys\":{},\"capabilities\":[],\"resources\":{\"media\":[{\"menuIcon\":\"true\",\"type\":\"bitmap\",\"name\":\"ICON\",\"file\":\"icon.png\"},{\"type\":\"bitmap\",\"name\":\"ANSWER\",\"file\":\"answer.png\"},{\"type\":\"bitmap\",\"name\":\"ENDCALL\",\"file\":\"endcall.png\"},{\"type\":\"bitmap\",\"name\":\"MIC_OFF\",\"file\":\"mic_off.png\"},{\"type\":\"bitmap\",\"name\":\"MIC_ON\",\"file\":\"micon.png\"},{\"type\":\"bitmap\",\"name\":\"SPEAKER_ON\",\"file\":\"speakeron.png\"},{\"type\":\"bitmap\",\"name\":\"SPEAKER_OFF\",\"file\":\"speakeroff.png\"},{\"type\":\"bitmap\",\"name\":\"VOLUME_UP\",\"file\":\"volumeup.png\"},{\"type\":\"bitmap\",\"name\":\"VOLUME_DOWN\",\"file\":\"volumedown.png\"},{\"type\":\"bitmap\",\"name\":\"CALL_HISTORY\",\"file\":\"callhistory.png\"},{\"type\":\"bitmap\",\"name\":\"CONTACTS\",\"file\":\"contacts.png\"},{\"type\":\"bitmap\",\"name\":\"CONTACT_GROUP\",\"file\":\"contactgroup.png\"},{\"type\":\"bitmap\",\"name\":\"INCOMING_CALL\",\"file\":\"incomingcall.png\"},{\"type\":\"bitmap\",\"name\":\"OUTGOING_CALL\",\"file\":\"outgoingcall.png\"},{\"type\":\"bitmap\",\"name\":\"MISSED_CALL\",\"file\":\"missedcall.png\"},{\"type\":\"bitmap\",\"name\":\"MESSAGE\",\"file\":\"message.png\"},{\"type\":\"bitmap\",\"name\":\"CALL\",\"file\":\"call.png\"}]}}" val APP_INFO_OBJ_SIMPLICITY = PbwAppInfo( uuid = "54eada19-67fd-4947-a7c5-256f24e3a7d7", @@ -22,7 +23,7 @@ class TestAppInfo { media = listOf( Media( resourceFile = "images/menu_icon_simplicity.png", - menuIcon = true, + menuIcon = StringOrBoolean(true), name = "IMAGE_MENU_ICON", type = "png" ) @@ -45,7 +46,7 @@ class TestAppInfo { media = listOf( Media( resourceFile = "icon.png", - menuIcon = true, + menuIcon = StringOrBoolean(true), name = "ICON", type = "bitmap" ),