Backport JSONObject.wrap in order to support Jellybean (flutter/engine#3566)

This commit is contained in:
Jason Simmons
2017-04-06 10:15:06 -07:00
committed by GitHub
parent 2933cfa76f
commit b3ce239c83
5 changed files with 71 additions and 5 deletions

View File

@@ -80,6 +80,7 @@ java_library("flutter_shell_java") {
"io/flutter/plugin/common/FlutterMethodChannel.java",
"io/flutter/plugin/common/JSONMessageCodec.java",
"io/flutter/plugin/common/JSONMethodCodec.java",
"io/flutter/plugin/common/JSONUtil.java",
"io/flutter/plugin/common/MessageCodec.java",
"io/flutter/plugin/common/MethodCall.java",
"io/flutter/plugin/common/MethodCodec.java",

View File

@@ -26,7 +26,7 @@ public final class JSONMessageCodec implements MessageCodec<Object> {
if (message == null) {
return null;
}
return StringCodec.INSTANCE.encodeMessage(JSONObject.wrap(message).toString());
return StringCodec.INSTANCE.encodeMessage(JSONUtil.wrap(message).toString());
}
@Override

View File

@@ -21,7 +21,7 @@ public final class JSONMethodCodec implements MethodCodec {
try {
final JSONObject map = new JSONObject();
map.put("method", methodCall.method);
map.put("args", JSONObject.wrap(methodCall.arguments));
map.put("args", JSONUtil.wrap(methodCall.arguments));
return JSONMessageCodec.INSTANCE.encodeMessage(map);
} catch (JSONException e) {
throw new IllegalArgumentException("Invalid JSON", e);
@@ -49,7 +49,7 @@ public final class JSONMethodCodec implements MethodCodec {
@Override
public ByteBuffer encodeSuccessEnvelope(Object result) {
return JSONMessageCodec.INSTANCE
.encodeMessage(new JSONArray().put(JSONObject.wrap(result)));
.encodeMessage(new JSONArray().put(JSONUtil.wrap(result)));
}
@Override
@@ -58,7 +58,7 @@ public final class JSONMethodCodec implements MethodCodec {
return JSONMessageCodec.INSTANCE.encodeMessage(new JSONArray()
.put(errorCode)
.put(errorMessage)
.put(JSONObject.wrap(errorDetails)));
.put(JSONUtil.wrap(errorDetails)));
}
@Override

View File

@@ -0,0 +1,64 @@
package io.flutter.plugin.common;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
public class JSONUtil {
private JSONUtil() {
}
/**
* Backport of {@link JSONObject#wrap(Object)} for use on pre-KitKat
* systems.
*/
public static Object wrap(Object o) {
if (o == null) {
return JSONObject.NULL;
}
if (o instanceof JSONArray || o instanceof JSONObject) {
return o;
}
if (o.equals(JSONObject.NULL)) {
return o;
}
try {
if (o instanceof Collection) {
JSONArray result = new JSONArray();
for (Object e : (Collection) o)
result.put(wrap(e));
return result;
} else if (o.getClass().isArray()) {
JSONArray result = new JSONArray();
int length = Array.getLength(o);
for (int i = 0; i < length; i++)
result.put(wrap(Array.get(o, i)));
return result;
}
if (o instanceof Map) {
JSONObject result = new JSONObject();
for (Map.Entry<?, ?> entry: ((Map<?, ?>) o).entrySet())
result.put((String) entry.getKey(), wrap(entry.getValue()));
return result;
}
if (o instanceof Boolean ||
o instanceof Byte ||
o instanceof Character ||
o instanceof Double ||
o instanceof Float ||
o instanceof Integer ||
o instanceof Long ||
o instanceof Short ||
o instanceof String) {
return o;
}
if (o.getClass().getPackage().getName().startsWith("java.")) {
return o.toString();
}
} catch (Exception ignored) {
}
return null;
}
}

View File

@@ -15,6 +15,7 @@ import io.flutter.plugin.common.FlutterMethodChannel;
import io.flutter.plugin.common.FlutterMethodChannel.MethodCallHandler;
import io.flutter.plugin.common.FlutterMethodChannel.Response;
import io.flutter.plugin.common.JSONMethodCodec;
import io.flutter.plugin.common.JSONUtil;
import io.flutter.plugin.common.MethodCall;
import io.flutter.view.FlutterView;
@@ -137,7 +138,7 @@ public class TextInputPlugin implements MethodCallHandler {
}
void setLatestEditingState(Map<String, Object> state) {
mLatestState = (JSONObject) JSONObject.wrap(state);
mLatestState = (JSONObject) JSONUtil.wrap(state);
}
private void clearTextInputClient() {