Skip to content

Commit 34c3bbe

Browse files
committed
Implemented Json and MsgPack serializers for path based liveobjects
1 parent c8a283d commit 34c3bbe

3 files changed

Lines changed: 1020 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.ably.lib.`object`.serialization
2+
3+
import com.google.gson.*
4+
import io.ably.lib.objects.*
5+
6+
import io.ably.lib.objects.ObjectMessage
7+
import org.msgpack.core.MessagePacker
8+
import org.msgpack.core.MessageUnpacker
9+
10+
/**
11+
* Default implementation of {@link ObjectsSerializer} that handles serialization/deserialization
12+
* of ObjectMessage arrays for both JSON and MessagePack formats using Jackson and Gson.
13+
* Dynamically loaded by ObjectsHelper#getSerializer() to avoid hard dependencies.
14+
*/
15+
@Suppress("unused") // Used via reflection in ObjectsHelper
16+
internal class DefaultObjectsSerializer : ObjectsSerializer {
17+
18+
override fun readMsgpackArray(unpacker: MessageUnpacker): Array<Any> {
19+
val objectMessagesCount = unpacker.unpackArrayHeader()
20+
return Array(objectMessagesCount) { readObjectMessage(unpacker) }
21+
}
22+
23+
override fun writeMsgpackArray(objects: Array<out Any>, packer: MessagePacker) {
24+
val objectMessages = objects.map { it as ObjectMessage }
25+
packer.packArrayHeader(objectMessages.size)
26+
objectMessages.forEach { it.writeMsgpack(packer) }
27+
}
28+
29+
override fun readFromJsonArray(json: JsonArray): Array<Any> {
30+
return json.map { element ->
31+
if (element.isJsonObject) element.asJsonObject.toObjectMessage()
32+
else throw JsonParseException("Expected JsonObject, but found: $element")
33+
}.toTypedArray()
34+
}
35+
36+
override fun asJsonArray(objects: Array<out Any>): JsonArray {
37+
val objectMessages = objects.map { it as ObjectMessage }
38+
val jsonArray = JsonArray()
39+
for (objectMessage in objectMessages) {
40+
jsonArray.add(objectMessage.toJsonObject())
41+
}
42+
return jsonArray
43+
}
44+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.ably.lib.`object`.serialization
2+
3+
import com.google.gson.*
4+
import io.ably.lib.objects.ObjectsMapSemantics
5+
import io.ably.lib.objects.ObjectData
6+
import io.ably.lib.objects.ObjectMessage
7+
import io.ably.lib.objects.ObjectOperationAction
8+
import io.ably.lib.objects.serialization.EnumCodeTypeAdapter
9+
import java.lang.reflect.Type
10+
import kotlin.enums.EnumEntries
11+
12+
// Gson instance for JSON serialization/deserialization
13+
internal val gson = GsonBuilder()
14+
.registerTypeAdapter(ObjectOperationAction::class.java, EnumCodeTypeAdapter({ it.code }, ObjectOperationAction.entries))
15+
.registerTypeAdapter(ObjectsMapSemantics::class.java, EnumCodeTypeAdapter({ it.code }, ObjectsMapSemantics.entries))
16+
.create()
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 class EnumCodeTypeAdapter<T : Enum<T>>(
27+
private val getCode: (T) -> Int,
28+
private val enumValues: EnumEntries<T>
29+
) : JsonSerializer<T>, JsonDeserializer<T> {
30+
31+
override fun serialize(src: T, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
32+
return JsonPrimitive(getCode(src))
33+
}
34+
35+
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): T {
36+
val code = json.asInt
37+
return enumValues.firstOrNull { getCode(it) == code } ?: enumValues.firstOrNull { getCode(it) == -1 }
38+
?: throw JsonParseException("Unknown enum code: $code and no Unknown fallback found")
39+
}
40+
}
41+
42+
internal class ObjectDataJsonSerializer : JsonSerializer<ObjectData>, JsonDeserializer<ObjectData> {
43+
override fun serialize(src: ObjectData, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement {
44+
val obj = JsonObject()
45+
src.objectId?.let { obj.addProperty("objectId", it) }
46+
src.string?.let { obj.addProperty("string", it) }
47+
src.number?.let { obj.addProperty("number", it) }
48+
src.boolean?.let { obj.addProperty("boolean", it) }
49+
src.bytes?.let { obj.addProperty("bytes", it) }
50+
src.json?.let { obj.addProperty("json", it.toString()) } // Spec: OD4c5
51+
return obj
52+
}
53+
54+
override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): ObjectData {
55+
val obj = if (json.isJsonObject) json.asJsonObject else throw JsonParseException("Expected JsonObject")
56+
val objectId = if (obj.has("objectId")) obj.get("objectId").asString else null
57+
val string = if (obj.has("string")) obj.get("string").asString else null
58+
val number = if (obj.has("number")) obj.get("number").asDouble else null
59+
val boolean = if (obj.has("boolean")) obj.get("boolean").asBoolean else null
60+
val bytes = if (obj.has("bytes")) obj.get("bytes").asString else null
61+
val json = if (obj.has("json")) JsonParser.parseString(obj.get("json").asString) else null
62+
63+
if (objectId == null && string == null && number == null && boolean == null && bytes == null && json == null) {
64+
throw JsonParseException("Since objectId is not present, at least one of the value fields must be present")
65+
}
66+
return ObjectData(objectId = objectId, string = string, number = number, boolean = boolean, bytes = bytes, json = json)
67+
}
68+
}

0 commit comments

Comments
 (0)