|
| 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