Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Attributes | Package Functions | List of all members
io.flutter.plugin.common.JSONMethodCodec Class Reference
Inheritance diagram for io.flutter.plugin.common.JSONMethodCodec:
io.flutter.plugin.common.MethodCodec

Public Member Functions

ByteBuffer encodeMethodCall (@NonNull MethodCall methodCall)
 
MethodCall decodeMethodCall (@NonNull ByteBuffer message)
 
ByteBuffer encodeSuccessEnvelope (@Nullable Object result)
 
ByteBuffer encodeErrorEnvelope ( @NonNull String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails)
 
ByteBuffer encodeErrorEnvelopeWithStacktrace ( @NonNull String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails, @Nullable String errorStacktrace)
 
Object decodeEnvelope (@NonNull ByteBuffer envelope)
 
ByteBuffer encodeMethodCall (@NonNull MethodCall methodCall)
 
MethodCall decodeMethodCall (@NonNull ByteBuffer methodCall)
 
ByteBuffer encodeSuccessEnvelope (@Nullable Object result)
 
ByteBuffer encodeErrorEnvelope ( @NonNull String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails)
 
ByteBuffer encodeErrorEnvelopeWithStacktrace ( @NonNull String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails, @Nullable String errorStacktrace)
 
Object decodeEnvelope (@NonNull ByteBuffer envelope)
 

Static Public Attributes

static final JSONMethodCodec INSTANCE = new JSONMethodCodec()
 

Package Functions

Object unwrapNull (Object value)
 

Detailed Description

A MethodCodec using UTF-8 encoded JSON method calls and result envelopes.

This codec is guaranteed to be compatible with the corresponding JSONMethodCodec on the Dart side. These parts of the Flutter SDK are evolved synchronously.

Values supported as methods arguments and result payloads are those supported by JSONMessageCodec.

Definition at line 24 of file JSONMethodCodec.java.

Member Function Documentation

◆ decodeEnvelope()

Object io.flutter.plugin.common.JSONMethodCodec.decodeEnvelope ( @NonNull ByteBuffer  envelope)
inline

Decodes a result envelope from binary.

Parameters
envelopethe binary encoding of a result envelope as a ByteBuffer.
Returns
the enveloped result Object.
Exceptions
FlutterExceptionif the envelope was an error envelope.

Implements io.flutter.plugin.common.MethodCodec.

Definition at line 96 of file JSONMethodCodec.java.

96 {
97 try {
98 final Object json = JSONMessageCodec.INSTANCE.decodeMessage(envelope);
99 if (json instanceof JSONArray) {
100 final JSONArray array = (JSONArray) json;
101 if (array.length() == 1) {
102 return unwrapNull(array.opt(0));
103 }
104 if (array.length() == 3) {
105 final Object code = array.get(0);
106 final Object message = unwrapNull(array.opt(1));
107 final Object details = unwrapNull(array.opt(2));
108 if (code instanceof String && (message == null || message instanceof String)) {
109 throw new FlutterException((String) code, (String) message, details);
110 }
111 }
112 }
113 throw new IllegalArgumentException("Invalid envelope: " + json);
114 } catch (JSONException e) {
115 throw new IllegalArgumentException("Invalid JSON", e);
116 }
117 }
Win32Message message

◆ decodeMethodCall()

MethodCall io.flutter.plugin.common.JSONMethodCodec.decodeMethodCall ( @NonNull ByteBuffer  methodCall)
inline

Decodes a message call from binary.

Parameters
methodCallthe binary encoding of the method call as a ByteBuffer.
Returns
a MethodCall representation of the bytes between the given buffer's current position and its limit.

Implements io.flutter.plugin.common.MethodCodec.

Definition at line 45 of file JSONMethodCodec.java.

45 {
46 try {
47 final Object json = JSONMessageCodec.INSTANCE.decodeMessage(message);
48 if (json instanceof JSONObject) {
49 final JSONObject map = (JSONObject) json;
50 final Object method = map.get("method");
51 final Object arguments = unwrapNull(map.opt("args"));
52 if (method instanceof String) {
53 return new MethodCall((String) method, arguments);
54 }
55 }
56 throw new IllegalArgumentException("Invalid method call: " + json);
57 } catch (JSONException e) {
58 throw new IllegalArgumentException("Invalid JSON", e);
59 }
60 }
static ::testing::Matcher< GBytes * > MethodCall(const std::string &name, ::testing::Matcher< FlValue * > args)
SI auto map(std::index_sequence< I... >, Fn &&fn, const Args &... args) -> skvx::Vec< sizeof...(I), decltype(fn(args[0]...))>
Definition: SkVx.h:680

◆ encodeErrorEnvelope()

ByteBuffer io.flutter.plugin.common.JSONMethodCodec.encodeErrorEnvelope ( @NonNull String  errorCode,
@Nullable String  errorMessage,
@Nullable Object  errorDetails 
)
inline

Encodes an error result into a binary envelope message.

Parameters
errorCodeAn error code String.
errorMessageAn error message String, possibly null.
errorDetailsError details, possibly null. Consider supporting Throwable in your codec. This is the most common value passed to this field.
Returns
a ByteBuffer containing the encoding between position 0 and the current position.

Implements io.flutter.plugin.common.MethodCodec.

Definition at line 70 of file JSONMethodCodec.java.

71 {
72 return JSONMessageCodec.INSTANCE.encodeMessage(
73 new JSONArray()
74 .put(errorCode)
75 .put(JSONUtil.wrap(errorMessage))
76 .put(JSONUtil.wrap(errorDetails)));
77 }

◆ encodeErrorEnvelopeWithStacktrace()

ByteBuffer io.flutter.plugin.common.JSONMethodCodec.encodeErrorEnvelopeWithStacktrace ( @NonNull String  errorCode,
@Nullable String  errorMessage,
@Nullable Object  errorDetails,
@Nullable String  errorStacktrace 
)
inline

Encodes an error result into a binary envelope message with the native stacktrace.

Parameters
errorCodeAn error code String.
errorMessageAn error message String, possibly null.
errorDetailsError details, possibly null. Consider supporting Throwable in your codec. This is the most common value passed to this field.
errorStacktracePlatform stacktrace for the error. possibly null.
Returns
a ByteBuffer containing the encoding between position 0 and the current position.

Implements io.flutter.plugin.common.MethodCodec.

Definition at line 81 of file JSONMethodCodec.java.

85 {
86 return JSONMessageCodec.INSTANCE.encodeMessage(
87 new JSONArray()
88 .put(errorCode)
89 .put(JSONUtil.wrap(errorMessage))
90 .put(JSONUtil.wrap(errorDetails))
91 .put(JSONUtil.wrap(errorStacktrace)));
92 }

◆ encodeMethodCall()

ByteBuffer io.flutter.plugin.common.JSONMethodCodec.encodeMethodCall ( @NonNull MethodCall  methodCall)
inline

Encodes a message call into binary.

Parameters
methodCalla MethodCall.
Returns
a ByteBuffer containing the encoding between position 0 and the current position.

Implements io.flutter.plugin.common.MethodCodec.

Definition at line 32 of file JSONMethodCodec.java.

32 {
33 try {
34 final JSONObject map = new JSONObject();
35 map.put("method", methodCall.method);
36 map.put("args", JSONUtil.wrap(methodCall.arguments));
37 return JSONMessageCodec.INSTANCE.encodeMessage(map);
38 } catch (JSONException e) {
39 throw new IllegalArgumentException("Invalid JSON", e);
40 }
41 }

◆ encodeSuccessEnvelope()

ByteBuffer io.flutter.plugin.common.JSONMethodCodec.encodeSuccessEnvelope ( @Nullable Object  result)
inline

Encodes a successful result into a binary envelope message.

Parameters
resultThe result value, possibly null.
Returns
a ByteBuffer containing the encoding between position 0 and the current position.

Implements io.flutter.plugin.common.MethodCodec.

Definition at line 64 of file JSONMethodCodec.java.

64 {
65 return JSONMessageCodec.INSTANCE.encodeMessage(new JSONArray().put(JSONUtil.wrap(result)));
66 }
GAsyncResult * result

◆ unwrapNull()

Object io.flutter.plugin.common.JSONMethodCodec.unwrapNull ( Object  value)
inlinepackage

Definition at line 119 of file JSONMethodCodec.java.

119 {
120 return (value == JSONObject.NULL) ? null : value;
121 }
uint8_t value

Member Data Documentation

◆ INSTANCE

final JSONMethodCodec io.flutter.plugin.common.JSONMethodCodec.INSTANCE = new JSONMethodCodec()
static

Definition at line 26 of file JSONMethodCodec.java.


The documentation for this class was generated from the following file: