Skip to content

Commit 4b084f8

Browse files
committed
[ECO-5426] Updated json values to be passed into json key
- Removed unnecessary `encoding` field used to detect json values
1 parent 529efd8 commit 4b084f8

2 files changed

Lines changed: 16 additions & 44 deletions

File tree

live-objects/src/main/kotlin/io/ably/lib/objects/serialization/JsonSerialization.kt

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@ internal class ObjectDataJsonSerializer : JsonSerializer<ObjectData>, JsonDeseri
5252
is String -> obj.addProperty("string", v)
5353
is Number -> obj.addProperty("number", v.toDouble())
5454
is Binary -> obj.addProperty("bytes", Base64.getEncoder().encodeToString(v.data))
55-
// Spec: OD4c5
56-
is JsonObject, is JsonArray -> {
57-
obj.addProperty("string", v.toString())
58-
obj.addProperty("encoding", "json")
59-
}
55+
is JsonObject, is JsonArray -> obj.addProperty("json", v.toString()) // Spec: OD4c5
6056
}
6157
}
6258
return obj
@@ -65,24 +61,12 @@ internal class ObjectDataJsonSerializer : JsonSerializer<ObjectData>, JsonDeseri
6561
override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): ObjectData {
6662
val obj = if (json.isJsonObject) json.asJsonObject else throw JsonParseException("Expected JsonObject")
6763
val objectId = if (obj.has("objectId")) obj.get("objectId").asString else null
68-
val encoding = if (obj.has("encoding")) obj.get("encoding").asString else null
6964
val value = when {
7065
obj.has("boolean") -> ObjectValue(obj.get("boolean").asBoolean)
71-
// Spec: OD5b3
72-
obj.has("string") && encoding == "json" -> {
73-
val jsonStr = obj.get("string").asString
74-
val parsed = JsonParser.parseString(jsonStr)
75-
ObjectValue(
76-
when {
77-
parsed.isJsonObject -> parsed.asJsonObject
78-
parsed.isJsonArray -> parsed.asJsonArray
79-
else -> throw JsonParseException("Invalid JSON string for encoding=json")
80-
}
81-
)
82-
}
8366
obj.has("string") -> ObjectValue(obj.get("string").asString)
8467
obj.has("number") -> ObjectValue(obj.get("number").asDouble)
8568
obj.has("bytes") -> ObjectValue(Binary(Base64.getDecoder().decode(obj.get("bytes").asString)))
69+
obj.has("json") -> ObjectValue(JsonParser.parseString(obj.get("json").asString))
8670
else -> {
8771
if (objectId != null)
8872
null

live-objects/src/main/kotlin/io/ably/lib/objects/serialization/MsgpackSerialization.kt

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.ably.lib.objects.serialization
22

33
import com.google.gson.JsonArray
4-
import com.google.gson.JsonElement
54
import com.google.gson.JsonObject
65
import com.google.gson.JsonParser
76
import io.ably.lib.objects.*
@@ -617,9 +616,6 @@ private fun ObjectData.writeMsgpack(packer: MessagePacker) {
617616
if (objectId != null) fieldCount++
618617
value?.let {
619618
fieldCount++
620-
if (it.value is JsonElement) {
621-
fieldCount += 1 // For extra "encoding" field
622-
}
623619
}
624620

625621
packer.packMapHeader(fieldCount)
@@ -649,10 +645,8 @@ private fun ObjectData.writeMsgpack(packer: MessagePacker) {
649645
packer.writePayload(v.data)
650646
}
651647
is JsonObject, is JsonArray -> {
652-
packer.packString("string")
653-
packer.packString(v.toString())
654-
packer.packString("encoding")
655648
packer.packString("json")
649+
packer.packString(v.toString())
656650
}
657651
}
658652
}
@@ -665,8 +659,6 @@ private fun readObjectData(unpacker: MessageUnpacker): ObjectData {
665659
val fieldCount = unpacker.unpackMapHeader()
666660
var objectId: String? = null
667661
var value: ObjectValue? = null
668-
var encoding: String? = null
669-
var stringValue: String? = null
670662

671663
for (i in 0 until fieldCount) {
672664
val fieldName = unpacker.unpackString().intern()
@@ -680,33 +672,29 @@ private fun readObjectData(unpacker: MessageUnpacker): ObjectData {
680672
when (fieldName) {
681673
"objectId" -> objectId = unpacker.unpackString()
682674
"boolean" -> value = ObjectValue(unpacker.unpackBoolean())
683-
"string" -> stringValue = unpacker.unpackString()
675+
"string" -> value = ObjectValue(unpacker.unpackString())
684676
"number" -> value = ObjectValue(unpacker.unpackDouble())
685677
"bytes" -> {
686678
val size = unpacker.unpackBinaryHeader()
687679
val bytes = ByteArray(size)
688680
unpacker.readPayload(bytes)
689681
value = ObjectValue(Binary(bytes))
690682
}
691-
"encoding" -> encoding = unpacker.unpackString()
683+
"json" -> {
684+
val jsonString = unpacker.unpackString()
685+
val parsed = JsonParser.parseString(jsonString)
686+
value = ObjectValue(
687+
when {
688+
parsed.isJsonObject -> parsed.asJsonObject
689+
parsed.isJsonArray -> parsed.asJsonArray
690+
else ->
691+
throw ablyException("Invalid JSON string for json field", ErrorCode.MapValueDataTypeUnsupported, HttpStatusCode.InternalServerError)
692+
}
693+
)
694+
}
692695
else -> unpacker.skipValue()
693696
}
694697
}
695698

696-
// Handle string with encoding if needed
697-
if (stringValue != null && encoding == "json") {
698-
val parsed = JsonParser.parseString(stringValue)
699-
value = ObjectValue(
700-
when {
701-
parsed.isJsonObject -> parsed.asJsonObject
702-
parsed.isJsonArray -> parsed.asJsonArray
703-
else ->
704-
throw ablyException("Invalid JSON string for encoding=json", ErrorCode.MapValueDataTypeUnsupported, HttpStatusCode.InternalServerError)
705-
}
706-
)
707-
} else if (stringValue != null) {
708-
value = ObjectValue(stringValue)
709-
}
710-
711699
return ObjectData(objectId = objectId, value = value)
712700
}

0 commit comments

Comments
 (0)