Files
engine/shell/platform/android/io/flutter/plugin/common/JSONMethodCodec.java
T

93 lines
3.4 KiB
Java
Raw Normal View History

package io.flutter.plugin.common;
import java.nio.ByteBuffer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* A {@link MethodCodec} using UTF-8 encoded JSON method calls and result envelopes.
* Values supported as methods arguments and result payloads are those supported by
* {@link JSONMessageCodec}.
*/
public final class JSONMethodCodec implements MethodCodec {
// This codec must match the Dart codec of the same name in package flutter/services.
2017-03-17 09:04:59 +01:00
public static final JSONMethodCodec INSTANCE = new JSONMethodCodec();
2017-03-17 09:04:59 +01:00
private JSONMethodCodec() {
}
2017-03-17 09:04:59 +01:00
@Override
public ByteBuffer encodeMethodCall(MethodCall methodCall) {
try {
final JSONObject map = new JSONObject();
map.put("method", methodCall.method);
map.put("args", JSONUtil.wrap(methodCall.arguments));
2017-03-17 09:04:59 +01:00
return JSONMessageCodec.INSTANCE.encodeMessage(map);
} catch (JSONException e) {
throw new IllegalArgumentException("Invalid JSON", e);
}
}
2017-03-17 09:04:59 +01:00
@Override
public MethodCall decodeMethodCall(ByteBuffer message) {
try {
final Object json = JSONMessageCodec.INSTANCE.decodeMessage(message);
if (json instanceof JSONObject) {
final JSONObject map = (JSONObject) json;
final Object method = map.get("method");
final Object arguments = unwrapNull(map.opt("args"));
2017-03-17 09:04:59 +01:00
if (method instanceof String) {
return new MethodCall((String) method, arguments);
}
}
throw new IllegalArgumentException("Invalid method call: " + json);
} catch (JSONException e) {
throw new IllegalArgumentException("Invalid JSON", e);
}
}
@Override
public ByteBuffer encodeSuccessEnvelope(Object result) {
return JSONMessageCodec.INSTANCE
.encodeMessage(new JSONArray().put(JSONUtil.wrap(result)));
2017-03-17 09:04:59 +01:00
}
@Override
public ByteBuffer encodeErrorEnvelope(String errorCode, String errorMessage,
Object errorDetails) {
return JSONMessageCodec.INSTANCE.encodeMessage(new JSONArray()
.put(errorCode)
.put(JSONUtil.wrap(errorMessage))
.put(JSONUtil.wrap(errorDetails)));
2017-03-17 09:04:59 +01:00
}
@Override
public Object decodeEnvelope(ByteBuffer envelope) {
try {
final Object json = JSONMessageCodec.INSTANCE.decodeMessage(envelope);
if (json instanceof JSONArray) {
final JSONArray array = (JSONArray) json;
if (array.length() == 1) {
return unwrapNull(array.opt(0));
2017-03-17 09:04:59 +01:00
}
if (array.length() == 3) {
final Object code = array.get(0);
final Object message = unwrapNull(array.opt(1));
final Object details = unwrapNull(array.opt(2));
2017-03-17 09:04:59 +01:00
if (code instanceof String && (message == null || message instanceof String)) {
throw new FlutterException((String) code, (String) message, details);
}
}
}
throw new IllegalArgumentException("Invalid envelope: " + json);
2017-03-17 09:04:59 +01:00
} catch (JSONException e) {
throw new IllegalArgumentException("Invalid JSON", e);
}
}
Object unwrapNull(Object value) {
return (value == JSONObject.NULL) ? null : value;
}
}