Skip to content

Commit ad0ef62

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 ad0ef62

2 files changed

Lines changed: 16 additions & 43 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 & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -617,9 +617,6 @@ private fun ObjectData.writeMsgpack(packer: MessagePacker) {
617617
if (objectId != null) fieldCount++
618618
value?.let {
619619
fieldCount++
620-
if (it.value is JsonElement) {
621-
fieldCount += 1 // For extra "encoding" field
622-
}
623620
}
624621

625622
packer.packMapHeader(fieldCount)
@@ -649,10 +646,8 @@ private fun ObjectData.writeMsgpack(packer: MessagePacker) {
649646
packer.writePayload(v.data)
650647
}
651648
is JsonObject, is JsonArray -> {
652-
packer.packString("string")
653-
packer.packString(v.toString())
654-
packer.packString("encoding")
655649
packer.packString("json")
650+
packer.packString(v.toString())
656651
}
657652
}
658653
}
@@ -665,8 +660,6 @@ private fun readObjectData(unpacker: MessageUnpacker): ObjectData {
665660
val fieldCount = unpacker.unpackMapHeader()
666661
var objectId: String? = null
667662
var value: ObjectValue? = null
668-
var encoding: String? = null
669-
var stringValue: String? = null
670663

671664
for (i in 0 until fieldCount) {
672665
val fieldName = unpacker.unpackString().intern()
@@ -680,33 +673,29 @@ private fun readObjectData(unpacker: MessageUnpacker): ObjectData {
680673
when (fieldName) {
681674
"objectId" -> objectId = unpacker.unpackString()
682675
"boolean" -> value = ObjectValue(unpacker.unpackBoolean())
683-
"string" -> stringValue = unpacker.unpackString()
676+
"string" -> value = ObjectValue(unpacker.unpackString())
684677
"number" -> value = ObjectValue(unpacker.unpackDouble())
685678
"bytes" -> {
686679
val size = unpacker.unpackBinaryHeader()
687680
val bytes = ByteArray(size)
688681
unpacker.readPayload(bytes)
689682
value = ObjectValue(Binary(bytes))
690683
}
691-
"encoding" -> encoding = unpacker.unpackString()
684+
"json" -> {
685+
val jsonString = unpacker.unpackString()
686+
val parsed = JsonParser.parseString(jsonString)
687+
value = ObjectValue(
688+
when {
689+
parsed.isJsonObject -> parsed.asJsonObject
690+
parsed.isJsonArray -> parsed.asJsonArray
691+
else ->
692+
throw ablyException("Invalid JSON string for json field", ErrorCode.MapValueDataTypeUnsupported, HttpStatusCode.InternalServerError)
693+
}
694+
)
695+
}
692696
else -> unpacker.skipValue()
693697
}
694698
}
695699

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-
711700
return ObjectData(objectId = objectId, value = value)
712701
}

0 commit comments

Comments
 (0)