Flutter Engine
The Flutter Engine
shell
platform
android
io
flutter
plugin
common
JSONMessageCodec.java
Go to the documentation of this file.
1
// Copyright 2013 The Flutter Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file.
4
5
package
io.flutter.plugin.common;
6
7
import
androidx.annotation.Nullable;
8
import
java.nio.ByteBuffer;
9
import
org.json.JSONException;
10
import
org.json.JSONObject;
11
import
org.json.JSONTokener;
12
13
/**
14
* A {@link MessageCodec} using UTF-8 encoded JSON messages.
15
*
16
* <p>This codec is guaranteed to be compatible with the corresponding <a
17
* href="https://api.flutter.dev/flutter/services/JSONMessageCodec-class.html">JSONMessageCodec</a>
18
* on the Dart side. These parts of the Flutter SDK are evolved synchronously.
19
*
20
* <p>Supports the same Java values as {@link JSONObject#wrap(Object)}.
21
*
22
* <p>On the Dart side, JSON messages are handled by the JSON facilities of the <a
23
* href="https://api.dartlang.org/stable/dart-convert/JSON-constant.html">dart:convert</a> package.
24
*/
25
public
final
class
JSONMessageCodec
implements
MessageCodec
<Object> {
26
// This codec must match the Dart codec of the same name in package flutter/services.
27
public
static
final
JSONMessageCodec
INSTANCE
=
new
JSONMessageCodec
();
28
29
private
JSONMessageCodec
() {}
30
31
@Override
32
@Nullable
33
public
ByteBuffer
encodeMessage
(@Nullable Object
message
) {
34
if
(
message
==
null
) {
35
return
null
;
36
}
37
final
Object
wrapped
=
JSONUtil
.
wrap
(
message
);
38
if
(
wrapped
instanceof String) {
39
return
StringCodec
.
INSTANCE
.
encodeMessage
(JSONObject.quote((String)
wrapped
));
40
}
else
{
41
return
StringCodec
.
INSTANCE
.
encodeMessage
(
wrapped
.toString());
42
}
43
}
44
45
@Override
46
@Nullable
47
public
Object
decodeMessage
(@Nullable ByteBuffer
message
) {
48
if
(
message
==
null
) {
49
return
null
;
50
}
51
try
{
52
final
String json =
StringCodec
.
INSTANCE
.
decodeMessage
(
message
);
53
final
JSONTokener tokener =
new
JSONTokener(json);
54
final
Object
value
= tokener.nextValue();
55
if
(tokener.more()) {
56
throw
new
IllegalArgumentException(
"Invalid JSON"
);
57
}
58
return
value
;
59
}
catch
(JSONException
e
) {
60
throw
new
IllegalArgumentException(
"Invalid JSON"
,
e
);
61
}
62
}
63
}
wrapped
static sk_sp< GrTextureProxy > wrapped(skiatest::Reporter *reporter, GrRecordingContext *rContext, GrProxyProvider *proxyProvider, SkBackingFit fit)
Definition:
TextureProxyTest.cpp:112
io.flutter.plugin.common.JSONMessageCodec
Definition:
JSONMessageCodec.java:25
io.flutter.plugin.common.JSONMessageCodec.decodeMessage
Object decodeMessage(@Nullable ByteBuffer message)
Definition:
JSONMessageCodec.java:47
io.flutter.plugin.common.JSONMessageCodec.INSTANCE
static final JSONMessageCodec INSTANCE
Definition:
JSONMessageCodec.java:27
io.flutter.plugin.common.JSONMessageCodec.encodeMessage
ByteBuffer encodeMessage(@Nullable Object message)
Definition:
JSONMessageCodec.java:33
io.flutter.plugin.common.JSONUtil
Definition:
JSONUtil.java:14
io.flutter.plugin.common.JSONUtil.wrap
static Object wrap(@Nullable Object o)
Definition:
JSONUtil.java:63
io.flutter.plugin.common.StringCodec
Definition:
StringCodec.java:18
io.flutter.plugin.common.StringCodec.INSTANCE
static final StringCodec INSTANCE
Definition:
StringCodec.java:20
io.flutter.plugin.common.StringCodec.encodeMessage
ByteBuffer encodeMessage(@Nullable String message)
Definition:
StringCodec.java:26
io.flutter.plugin.common.StringCodec.decodeMessage
String decodeMessage(@Nullable ByteBuffer message)
Definition:
StringCodec.java:39
value
uint8_t value
Definition:
fl_standard_message_codec.cc:36
io.flutter.plugin.common.MessageCodec
Definition:
MessageCodec.java:15
message
Win32Message message
Definition:
keyboard_unittests.cc:139
protoc_wrapper.e
e
Definition:
protoc_wrapper.py:226
Generated on Sun Jun 23 2024 21:55:09 for Flutter Engine by
1.9.4