|
| 1 | +@file:Suppress("UNCHECKED_CAST") |
| 2 | + |
1 | 3 | package io.ably.lib.objects |
2 | 4 |
|
3 | | -import com.google.gson.Gson |
4 | | -import com.google.gson.GsonBuilder |
5 | | -import com.google.gson.JsonArray |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper |
| 6 | +import com.google.gson.* |
| 7 | +import org.msgpack.core.MessagePack |
6 | 8 | import org.msgpack.core.MessagePacker |
7 | 9 | import org.msgpack.core.MessageUnpacker |
| 10 | +import org.msgpack.jackson.dataformat.MessagePackFactory |
| 11 | + |
| 12 | +// Gson instance for JSON serialization/deserialization |
| 13 | +internal val gson: Gson = GsonBuilder().create() |
| 14 | + |
| 15 | +// Jackson ObjectMapper for MessagePack serialization (respects @JsonProperty annotations) |
| 16 | +private val msgpackMapper = ObjectMapper(MessagePackFactory()) |
| 17 | + |
| 18 | +internal fun ObjectMessage.toJsonObject(): JsonObject { |
| 19 | + return gson.toJsonTree(this).asJsonObject |
| 20 | +} |
| 21 | + |
| 22 | +internal fun JsonObject.toObjectMessage(): ObjectMessage { |
| 23 | + return gson.fromJson(this, ObjectMessage::class.java) |
| 24 | +} |
| 25 | + |
| 26 | +internal fun ObjectMessage.writeTo(packer: MessagePacker) { |
| 27 | + // Jackson automatically creates the correct msgpack map structure |
| 28 | + val msgpackBytes = msgpackMapper.writeValueAsBytes(this) |
| 29 | + |
| 30 | + // Parse the msgpack bytes to get the structured value |
| 31 | + val tempUnpacker = MessagePack.newDefaultUnpacker(msgpackBytes) |
| 32 | + val msgpackValue = tempUnpacker.unpackValue() |
| 33 | + tempUnpacker.close() |
| 34 | + |
| 35 | + // Pack the structured value using the provided packer |
| 36 | + packer.packValue(msgpackValue) |
| 37 | +} |
| 38 | + |
| 39 | +internal fun MessageUnpacker.readObjectMessage(): ObjectMessage { |
| 40 | + // Read the msgpack value from the unpacker |
| 41 | + val msgpackValue = this.unpackValue() |
8 | 42 |
|
9 | | -internal val gson: Gson = createGsonSerializer() |
| 43 | + // Convert the msgpack value back to bytes |
| 44 | + val tempPacker = MessagePack.newDefaultBufferPacker() |
| 45 | + tempPacker.packValue(msgpackValue) |
| 46 | + val msgpackBytes = tempPacker.toByteArray() |
| 47 | + tempPacker.close() |
10 | 48 |
|
11 | | -private fun createGsonSerializer(): Gson { |
12 | | - return GsonBuilder().create() // Do not call serializeNulls() to omit null values |
| 49 | + // Let Jackson deserialize the msgpack bytes back to ObjectMessage |
| 50 | + return msgpackMapper.readValue(msgpackBytes, ObjectMessage::class.java) |
13 | 51 | } |
14 | 52 |
|
15 | 53 | internal class DefaultLiveObjectSerializer : LiveObjectSerializer { |
16 | 54 | override fun readMsgpackArray(unpacker: MessageUnpacker): Array<Any> { |
17 | | - TODO("Not yet implemented") |
| 55 | + val objectMessagesCount = unpacker.unpackArrayHeader() |
| 56 | + val objectMessages = mutableListOf<ObjectMessage>() |
| 57 | + for (i in 0 until objectMessagesCount) { |
| 58 | + objectMessages.add(unpacker.readObjectMessage()) |
| 59 | + } |
| 60 | + return objectMessages.toTypedArray() |
18 | 61 | } |
19 | 62 |
|
20 | 63 | override fun writeMsgpackArray(objects: Array<out Any>?, packer: MessagePacker) { |
21 | | - TODO("Not yet implemented") |
| 64 | + val objectMessages: Array<ObjectMessage> = objects as Array<ObjectMessage> |
| 65 | + packer.packArrayHeader(objectMessages.size) |
| 66 | + for (objectMessage in objectMessages) { |
| 67 | + objectMessage.writeTo(packer) |
| 68 | + } |
22 | 69 | } |
23 | 70 |
|
24 | 71 | override fun readFromJsonArray(json: JsonArray): Array<Any> { |
25 | | - TODO("Not yet implemented") |
| 72 | + val objectMessages = mutableListOf<ObjectMessage>() |
| 73 | + for (element in json) { |
| 74 | + if (element.isJsonObject) { |
| 75 | + objectMessages.add(element.asJsonObject.toObjectMessage()) |
| 76 | + } else { |
| 77 | + throw JsonParseException("Expected JsonObject, but found: $element") |
| 78 | + } |
| 79 | + } |
| 80 | + return objectMessages.toTypedArray() |
26 | 81 | } |
27 | 82 |
|
28 | 83 | override fun asJsonArray(objects: Array<out Any>?): JsonArray { |
29 | | - TODO("Not yet implemented") |
| 84 | + val objectMessages: Array<ObjectMessage> = objects as Array<ObjectMessage> |
| 85 | + val jsonArray = JsonArray() |
| 86 | + for (objectMessage in objectMessages) { |
| 87 | + jsonArray.add(objectMessage.toJsonObject()) |
| 88 | + } |
| 89 | + return jsonArray |
30 | 90 | } |
31 | 91 | } |
0 commit comments