mirror of
https://github.com/encounter/engine.git
synced 2026-03-30 11:09:55 -07:00
74de13c0bd
New concepts: FlutterMessageChannel (basic message send/receive superseding existing FlutterView methods), FlutterMethodChannel (method invocation and event streams), pluggable codecs for messages and method calls: unencoded binary, string, json, and 'standard' flutter binary encoding.
48 lines
1.5 KiB
Java
48 lines
1.5 KiB
Java
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 {
|
|
public static final JSONMethodCodec INSTANCE = new JSONMethodCodec();
|
|
|
|
private JSONMethodCodec() {
|
|
}
|
|
|
|
@Override
|
|
public MethodCall decodeMethodCall(ByteBuffer message) {
|
|
try {
|
|
final Object json = JSONMessageCodec.INSTANCE.decodeMessage(message);
|
|
if (json instanceof JSONArray) {
|
|
final JSONArray pair = (JSONArray) json;
|
|
if (pair.length() == 2 && pair.get(0) instanceof String) {
|
|
return new MethodCall(pair.getString(0), pair.get(1));
|
|
}
|
|
}
|
|
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(JSONObject.wrap(result)));
|
|
}
|
|
|
|
@Override
|
|
public ByteBuffer encodeErrorEnvelope(String errorCode, String errorMessage, Object errorDetails) {
|
|
return JSONMessageCodec.INSTANCE.encodeMessage(new JSONArray()
|
|
.put(errorCode)
|
|
.put(errorMessage)
|
|
.put(JSONObject.wrap(errorDetails)));
|
|
}
|
|
}
|