3333import java .util .Set ;
3434
3535public class Serialisation {
36+ public static final String TAG = Serialisation .class .getName ();
3637 public static final JsonParser gsonParser ;
3738 public static final GsonBuilder gsonBuilder ;
3839 public static final Gson gson ;
@@ -194,6 +195,7 @@ public static void gsonToMsgpack(JsonElement json, MessagePacker packer) {
194195 } else if (json .isJsonPrimitive ()) {
195196 gsonToMsgpack ((JsonPrimitive )json , packer );
196197 } else {
198+ Log .e (TAG , "Unsupported JsonElement type: " + json .getClass ().getName ());
197199 throw new RuntimeException ("unreachable" );
198200 }
199201 }
@@ -204,7 +206,10 @@ private static void gsonToMsgpack(JsonArray array, MessagePacker packer) {
204206 for (JsonElement elem : array ) {
205207 gsonToMsgpack (elem , packer );
206208 }
207- } catch (IOException e ) {}
209+ } catch (IOException e ) {
210+ // Handle IOException, possibly log it or rethrow as a runtime exception
211+ Log .e (TAG , "Error packing JsonArray to MsgPack" , e );
212+ }
208213 }
209214
210215 private static void gsonToMsgpack (JsonObject object , MessagePacker packer ) {
@@ -215,13 +220,17 @@ private static void gsonToMsgpack(JsonObject object, MessagePacker packer) {
215220 packer .packString (entry .getKey ());
216221 gsonToMsgpack (entry .getValue (), packer );
217222 }
218- } catch (IOException e ) {}
223+ } catch (IOException e ) {
224+ Log .e (TAG , "Error packing JsonObject to MsgPack" , e );
225+ }
219226 }
220227
221228 private static void gsonToMsgpack (JsonNull n , MessagePacker packer ) {
222229 try {
223230 packer .packNil ();
224- } catch (IOException e ) {}
231+ } catch (IOException e ) {
232+ Log .e (TAG , "Error packing JsonNull to MsgPack" , e );
233+ }
225234 }
226235
227236 private static void gsonToMsgpack (JsonPrimitive primitive , MessagePacker packer ) {
@@ -244,11 +253,24 @@ private static void gsonToMsgpack(JsonPrimitive primitive, MessagePacker packer)
244253 packer .packByte (number .byteValue ());
245254 } else {
246255 packer .packString (primitive .getAsString ());
256+ Log .e (TAG , "Unsupported number type: " + number .getClass ().getName ());
247257 }
248258 } else {
249- packer .packString (primitive .getAsString ());
259+ String value = primitive .getAsString ();
260+ if (Base64Coder .isBase64 (value )) {
261+ byte [] decodedData = Base64Coder .decode (value );
262+ packer .packBinaryHeader (decodedData .length );
263+ packer .writePayload (decodedData );
264+ } else {
265+ packer .packString (value );
266+ }
267+ if (!primitive .isString ()) {
268+ Log .e (TAG , "Unsupported JsonPrimitive type: " + primitive .getClass ().getName ());
269+ }
250270 }
251- } catch (IOException e ) {}
271+ } catch (Exception e ) {
272+ Log .e (TAG , "Error packing JsonPrimitive to MsgPack" , e );
273+ }
252274 }
253275
254276 public static JsonElement msgpackToGson (Value value ) {
0 commit comments