Skip to content

Commit a0adff9

Browse files
committed
Fix StringOrBoolean + test for both
1 parent 377c1b5 commit a0adff9

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package io.rebble.libpebblecommon.metadata
2-
import kotlinx.serialization.Serializable
3-
import kotlinx.serialization.Serializer
4-
import kotlinx.serialization.KSerializer
2+
import kotlinx.serialization.*
53
import kotlinx.serialization.encoding.Decoder
64
import kotlinx.serialization.encoding.Encoder
5+
import kotlinx.serialization.json.JsonContentPolymorphicSerializer
6+
import kotlinx.serialization.json.JsonDecoder
7+
import kotlinx.serialization.json.JsonElement
8+
import kotlinx.serialization.json.jsonPrimitive
79

810
@Serializable
911
data class StringOrBoolean(val value: Boolean) {
@@ -13,10 +15,13 @@ data class StringOrBoolean(val value: Boolean) {
1315
encoder.encodeString(if (value.value) "true" else "false")
1416
}
1517

16-
override fun deserialize(decoder: Decoder): StringOrBoolean = try {
17-
StringOrBoolean(decoder.decodeString() == "true")
18-
} catch (e: Error) {
19-
StringOrBoolean(decoder.decodeBoolean())
18+
override fun deserialize(decoder: Decoder): StringOrBoolean {
19+
require(decoder is JsonDecoder)
20+
val element = decoder.decodeJsonElement()
21+
if (element.jsonPrimitive.content != "true" && element.jsonPrimitive.content != "false") {
22+
throw SerializationException("StringOrBoolean value is not a boolean keyword")
23+
}
24+
return StringOrBoolean(element.jsonPrimitive.content == "true")
2025
}
2126
}
2227
}

src/commonMain/kotlin/io/rebble/libpebblecommon/util/SerializationUtil.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ package io.rebble.libpebblecommon.util
22

33
import io.rebble.libpebblecommon.metadata.pbw.appinfo.PbwAppInfo
44
import io.rebble.libpebblecommon.metadata.pbw.manifest.PbwManifest
5+
import kotlinx.serialization.SerializationException
56
import kotlinx.serialization.decodeFromString
67
import kotlinx.serialization.encodeToString
78
import kotlinx.serialization.json.Json
89

910
object SerializationUtil {
1011
private val json = Json { ignoreUnknownKeys = true }
12+
@Throws(SerializationException::class)
1113
fun serializeAppInfo(appInfo: PbwAppInfo): String = json.encodeToString(appInfo)
14+
@Throws(SerializationException::class)
1215
fun deserializeAppInfo(jsonString: String): PbwAppInfo = json.decodeFromString(jsonString)
1316

17+
@Throws(SerializationException::class)
1418
fun serializeManifest(manifest: PbwManifest): String = json.encodeToString(manifest)
19+
@Throws(SerializationException::class)
1520
fun deserializeManifest(jsonString: String): PbwManifest = json.decodeFromString(jsonString)
1621
}

src/commonTest/kotlin/io/rebble/libpebblecommon/metadata/pbw/appinfo/TestAppInfo.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import kotlinx.serialization.encodeToString
55
import kotlinx.serialization.json.Json
66
import kotlin.test.Test
77
import kotlin.test.assertEquals
8+
import io.rebble.libpebblecommon.metadata.StringOrBoolean
89

910
class TestAppInfo {
1011
companion object {
1112
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\": {}}"
12-
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\"}]}}"
13+
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\"}]}}"
1314

1415
val APP_INFO_OBJ_SIMPLICITY = PbwAppInfo(
1516
uuid = "54eada19-67fd-4947-a7c5-256f24e3a7d7",
@@ -22,7 +23,7 @@ class TestAppInfo {
2223
media = listOf(
2324
Media(
2425
resourceFile = "images/menu_icon_simplicity.png",
25-
menuIcon = true,
26+
menuIcon = StringOrBoolean(true),
2627
name = "IMAGE_MENU_ICON",
2728
type = "png"
2829
)
@@ -45,7 +46,7 @@ class TestAppInfo {
4546
media = listOf(
4647
Media(
4748
resourceFile = "icon.png",
48-
menuIcon = true,
49+
menuIcon = StringOrBoolean(true),
4950
name = "ICON",
5051
type = "bitmap"
5152
),

0 commit comments

Comments
 (0)