88import com .google .gson .JsonObject ;
99import com .google .gson .JsonParser ;
1010import com .google .gson .JsonPrimitive ;
11+ import com .google .gson .JsonSerializationContext ;
12+ import com .google .gson .JsonSerializer ;
1113import io .ably .lib .http .HttpCore ;
1214import io .ably .lib .platform .Platform ;
1315import io .ably .lib .types .AblyException ;
2729import java .io .IOException ;
2830import java .io .UnsupportedEncodingException ;
2931import java .lang .reflect .Array ;
32+ import java .lang .reflect .Type ;
3033import java .math .BigDecimal ;
3134import java .math .BigInteger ;
3235import java .util .Map ;
3336import java .util .Set ;
3437
3538public class Serialisation {
39+ public static final String TAG = Serialisation .class .getName ();
3640 public static final JsonParser gsonParser ;
3741 public static final GsonBuilder gsonBuilder ;
3842 public static final Gson gson ;
@@ -48,6 +52,7 @@ public class Serialisation {
4852 gsonBuilder .registerTypeAdapter (PresenceMessage .class , new PresenceMessage .Serializer ());
4953 gsonBuilder .registerTypeAdapter (PresenceMessage .Action .class , new PresenceMessage .ActionSerializer ());
5054 gsonBuilder .registerTypeAdapter (ProtocolMessage .Action .class , new ProtocolMessage .ActionSerializer ());
55+ gsonBuilder .registerTypeAdapter (Base64EncodedJsonPrimitive .class , new Base64EncodedJsonPrimitive .Serializer ());
5156 gson = gsonBuilder .create ();
5257
5358 msgpackPackerConfig = Platform .name .equals ("android" ) ?
@@ -193,18 +198,36 @@ public static void gsonToMsgpack(JsonElement json, MessagePacker packer) {
193198 gsonToMsgpack ((JsonNull )json , packer );
194199 } else if (json .isJsonPrimitive ()) {
195200 gsonToMsgpack ((JsonPrimitive )json , packer );
196- } else {
201+ } else if (json instanceof Base64EncodedJsonPrimitive ) {
202+ gsonToMsgpack ((Base64EncodedJsonPrimitive )json , packer );
203+ }
204+ else {
205+ Log .e (TAG , "Unsupported JsonElement type: " + json .getClass ().getName ());
197206 throw new RuntimeException ("unreachable" );
198207 }
199208 }
200209
210+ public static void gsonToMsgpack (Base64EncodedJsonPrimitive json , MessagePacker packer ) {
211+ try {
212+ String value = json .getAsString ();
213+ byte [] decodedData = Base64Coder .decode (value );
214+ packer .packBinaryHeader (decodedData .length );
215+ packer .writePayload (decodedData );
216+ } catch (IOException e ) {
217+ throw new RuntimeException (e );
218+ }
219+ }
220+
201221 private static void gsonToMsgpack (JsonArray array , MessagePacker packer ) {
202222 try {
203223 packer .packArrayHeader (array .size ());
204224 for (JsonElement elem : array ) {
205225 gsonToMsgpack (elem , packer );
206226 }
207- } catch (IOException e ) {}
227+ } catch (IOException e ) {
228+ // Handle IOException, possibly log it or rethrow as a runtime exception
229+ Log .e (TAG , "Error packing JsonArray to MsgPack" , e );
230+ }
208231 }
209232
210233 private static void gsonToMsgpack (JsonObject object , MessagePacker packer ) {
@@ -215,13 +238,17 @@ private static void gsonToMsgpack(JsonObject object, MessagePacker packer) {
215238 packer .packString (entry .getKey ());
216239 gsonToMsgpack (entry .getValue (), packer );
217240 }
218- } catch (IOException e ) {}
241+ } catch (IOException e ) {
242+ Log .e (TAG , "Error packing JsonObject to MsgPack" , e );
243+ }
219244 }
220245
221246 private static void gsonToMsgpack (JsonNull n , MessagePacker packer ) {
222247 try {
223248 packer .packNil ();
224- } catch (IOException e ) {}
249+ } catch (IOException e ) {
250+ Log .e (TAG , "Error packing JsonNull to MsgPack" , e );
251+ }
225252 }
226253
227254 private static void gsonToMsgpack (JsonPrimitive primitive , MessagePacker packer ) {
@@ -244,11 +271,14 @@ private static void gsonToMsgpack(JsonPrimitive primitive, MessagePacker packer)
244271 packer .packByte (number .byteValue ());
245272 } else {
246273 packer .packString (primitive .getAsString ());
274+ Log .e (TAG , "Unsupported number type: " + number .getClass ().getName ());
247275 }
248276 } else {
249277 packer .packString (primitive .getAsString ());
250278 }
251- } catch (IOException e ) {}
279+ } catch (Exception e ) {
280+ Log .e (TAG , "Error packing JsonPrimitive to MsgPack" , e );
281+ }
252282 }
253283
254284 public static JsonElement msgpackToGson (Value value ) {
@@ -286,4 +316,29 @@ public static JsonElement msgpackToGson(Value value) {
286316 return null ;
287317 }
288318 }
319+
320+ public static class Base64EncodedJsonPrimitive extends JsonElement {
321+ private final String value ;
322+
323+ public Base64EncodedJsonPrimitive (String value ) {
324+ this .value = value ;
325+ }
326+
327+ @ Override
328+ public String getAsString () {
329+ return value ;
330+ }
331+
332+ @ Override
333+ public JsonElement deepCopy () {
334+ return null ;
335+ }
336+
337+ public static class Serializer implements JsonSerializer <Base64EncodedJsonPrimitive > {
338+ @ Override
339+ public JsonElement serialize (Base64EncodedJsonPrimitive src , Type typeOfSrc , JsonSerializationContext context ) {
340+ return new JsonPrimitive (src .getAsString ());
341+ }
342+ }
343+ }
289344}
0 commit comments