提交 b4088b64 编写于 作者: J Jason Simmons 提交者: GitHub

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

上级 9e59a83a
......@@ -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",
......
......@@ -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
......
......@@ -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
......
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;
}
}
......@@ -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() {
......
......@@ -1073,6 +1073,7 @@ FILE: ../../../flutter/lib/ui/window/window.h
FILE: ../../../flutter/runtime/platform_impl.h
FILE: ../../../flutter/shell/common/skia_event_tracer_impl.cc
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/common/JSONMethodCodec.java
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/common/JSONUtil.java
FILE: ../../../flutter/shell/platform/darwin/desktop/Info.plist
FILE: ../../../flutter/shell/platform/darwin/desktop/flutter_mac.xib
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Flutter.podspec
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册