Skip to content

Commit

Permalink
Android: encoding of Gson element handled
Browse files Browse the repository at this point in the history
  • Loading branch information
tiholic committed Jul 24, 2020
1 parent 4ccbf7b commit 08b53d7
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions android/src/main/java/io/ably/flutter/plugin/AblyMessageCodec.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.ably.flutter.plugin;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.ByteArrayOutputStream;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

Expand Down Expand Up @@ -118,9 +121,21 @@ protected void writeValue(ByteArrayOutputStream stream, Object value) {
return;
}
}
if(value instanceof JsonElement){
WriteJsonElement(stream, (JsonElement) value);
return;
}
super.writeValue(stream, value);
}

private void WriteJsonElement(ByteArrayOutputStream stream, JsonElement value){
if(value instanceof JsonObject){
super.writeValue(stream, (new Gson()).fromJson(value, Map.class));
} else if(value instanceof JsonArray){
super.writeValue(stream, (new Gson()).fromJson(value, ArrayList.class));
}
}

/**
* Dart int types get delivered to Java as Integer, unless '32 bits not enough' in which case
* they are delivered as Long.
Expand Down Expand Up @@ -270,15 +285,8 @@ private Map<String, Object> encodeChannelMessage(Message c){
writeValueToJson(jsonMap, PlatformConstants.TxMessage.id, c.id);
writeValueToJson(jsonMap, PlatformConstants.TxMessage.clientId, c.clientId);
writeValueToJson(jsonMap, PlatformConstants.TxMessage.connectionId, c.connectionId);
writeValueToJson(jsonMap, PlatformConstants.TxMessage.timestamp, new Date(c.timestamp));
writeValueToJson(jsonMap, PlatformConstants.TxMessage.timestamp, c.timestamp);
writeValueToJson(jsonMap, PlatformConstants.TxMessage.name, c.name);
System.out.println("MESAAGE DATA FROM ANDROID");
System.out.println(c.data);
System.out.println(c.data instanceof Number);
System.out.println(c.data instanceof String);
System.out.println(c.data instanceof Array);
System.out.println(c.data instanceof List);
System.out.println(c.data instanceof Map);
writeValueToJson(jsonMap, PlatformConstants.TxMessage.data, c.data);
writeValueToJson(jsonMap, PlatformConstants.TxMessage.encoding, c.encoding);
writeValueToJson(jsonMap, PlatformConstants.TxMessage.extras, c.extras);
Expand Down

0 comments on commit 08b53d7

Please sign in to comment.