@@ -8,6 +8,8 @@ import org.msgpack.core.MessagePack
88import org.msgpack.core.MessagePacker
99import org.msgpack.core.MessageUnpacker
1010import org.msgpack.jackson.dataformat.MessagePackFactory
11+ import java.lang.reflect.Type
12+ import java.util.*
1113
1214// Gson instance for JSON serialization/deserialization
1315internal val gson: Gson = GsonBuilder ().create()
@@ -86,3 +88,51 @@ internal class DefaultLiveObjectSerializer : LiveObjectSerializer {
8688 return jsonArray
8789 }
8890}
91+
92+ internal class ObjectDataJsonSerializer : JsonSerializer <ObjectData >, JsonDeserializer <ObjectData > {
93+ override fun serialize (src : ObjectData ? , typeOfSrc : Type ? , context : JsonSerializationContext ? ): JsonElement {
94+ val obj = JsonObject ()
95+ src?.objectId?.let { obj.addProperty(" objectId" , it) }
96+
97+ src?.value?.let { value ->
98+ when (val v = value.value) {
99+ is Boolean -> obj.addProperty(" boolean" , v)
100+ is String -> obj.addProperty(" string" , v)
101+ is Number -> obj.addProperty(" number" , v)
102+ is Binary -> obj.addProperty(" bytes" , Base64 .getEncoder().encodeToString(v.data))
103+ // Spec: OD4c5
104+ is JsonObject , is JsonArray -> {
105+ obj.addProperty(" string" , v.toString())
106+ obj.addProperty(" encoding" , " json" )
107+ }
108+ }
109+ }
110+ return obj
111+ }
112+
113+ override fun deserialize (json : JsonElement ? , typeOfT : Type ? , context : JsonDeserializationContext ? ): ObjectData {
114+ val obj = json?.asJsonObject ? : throw JsonParseException (" Expected JsonObject" )
115+ val objectId = if (obj.has(" objectId" )) obj.get(" objectId" ).asString else null
116+ val encoding = if (obj.has(" encoding" )) obj.get(" encoding" ).asString else null
117+ val value = when {
118+ obj.has(" boolean" ) -> ObjectValue (obj.get(" boolean" ).asBoolean)
119+ // Spec: OD5b3
120+ obj.has(" string" ) && encoding == " json" -> {
121+ val jsonStr = obj.get(" string" ).asString
122+ val parsed = JsonParser .parseString(jsonStr)
123+ ObjectValue (
124+ when {
125+ parsed.isJsonObject -> parsed.asJsonObject
126+ parsed.isJsonArray -> parsed.asJsonArray
127+ else -> throw JsonParseException (" Invalid JSON string for encoding=json" )
128+ }
129+ )
130+ }
131+ obj.has(" string" ) -> ObjectValue (obj.get(" string" ).asString)
132+ obj.has(" number" ) -> ObjectValue (obj.get(" number" ).asNumber)
133+ obj.has(" bytes" ) -> ObjectValue (Binary (Base64 .getDecoder().decode(obj.get(" bytes" ).asString)))
134+ else -> null
135+ }
136+ return ObjectData (objectId, value)
137+ }
138+ }
0 commit comments