11package io.ably.lib.objects.serialization
22
33import com.google.gson.JsonArray
4- import com.google.gson.JsonElement
54import com.google.gson.JsonObject
65import com.google.gson.JsonParser
76import 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