|
| 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() |
8 | 14 |
|
9 | | -internal val gson: Gson = createGsonSerializer() |
| 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 | +} |
10 | 21 |
|
11 | | -private fun createGsonSerializer(): Gson { |
12 | | - return GsonBuilder().create() // Do not call serializeNulls() to omit null values |
| 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() |
| 42 | + |
| 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() |
| 48 | + |
| 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 { |
| 54 | + |
16 | 55 | override fun readMsgpackArray(unpacker: MessageUnpacker): Array<Any> { |
17 | | - TODO("Not yet implemented") |
| 56 | + val objectMessagesCount = unpacker.unpackArrayHeader() |
| 57 | + return Array(objectMessagesCount) { unpacker.readObjectMessage() } |
18 | 58 | } |
19 | 59 |
|
20 | 60 | override fun writeMsgpackArray(objects: Array<out Any>?, packer: MessagePacker) { |
21 | | - TODO("Not yet implemented") |
| 61 | + val objectMessages: Array<ObjectMessage> = objects as Array<ObjectMessage> |
| 62 | + packer.packArrayHeader(objectMessages.size) |
| 63 | + objectMessages.forEach { it.writeTo(packer) } |
22 | 64 | } |
23 | 65 |
|
24 | 66 | override fun readFromJsonArray(json: JsonArray): Array<Any> { |
25 | | - TODO("Not yet implemented") |
| 67 | + return json.map { element -> |
| 68 | + if (element.isJsonObject) element.asJsonObject.toObjectMessage() |
| 69 | + else throw JsonParseException("Expected JsonObject, but found: $element") |
| 70 | + }.toTypedArray() |
26 | 71 | } |
27 | 72 |
|
28 | 73 | override fun asJsonArray(objects: Array<out Any>?): JsonArray { |
29 | | - TODO("Not yet implemented") |
| 74 | + val objectMessages: Array<ObjectMessage> = objects as Array<ObjectMessage> |
| 75 | + val jsonArray = JsonArray() |
| 76 | + for (objectMessage in objectMessages) { |
| 77 | + jsonArray.add(objectMessage.toJsonObject()) |
| 78 | + } |
| 79 | + return jsonArray |
30 | 80 | } |
31 | 81 | } |
0 commit comments