Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Instance Methods | Class Methods | List of all members
FlutterJSONMessageCodec Class Reference

#include <FlutterCodecs.h>

Inheritance diagram for FlutterJSONMessageCodec:
<FlutterMessageCodec>

Instance Methods

(NSData *) - encode: [implementation]
 
(id- decode: [implementation]
 
- Instance Methods inherited from <FlutterMessageCodec>
(NSData *_Nullable) - encode:
 
(id _Nullable) - decode:
 

Class Methods

(instancetype) + sharedInstance [implementation]
 

Detailed Description

A FlutterMessageCodec using UTF-8 encoded JSON messages.

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

Supports values accepted by NSJSONSerialization plus top-level nil, NSNumber, and NSString.

On the Dart side, JSON messages are handled by the JSON facilities of the dart:convert package.

Definition at line 81 of file FlutterCodecs.h.

Method Documentation

◆ decode:

- (id) decode: (NSData*)  message
implementation

Definition at line 9 of file FlutterCodecs.mm.

86 :(NSData*)message {
87 if ([message length] == 0) {
88 return nil;
89 }
90 BOOL isSimpleValue = NO;
91 id decoded = nil;
92 NSError* error;
93 if (0 < message.length) {
94 UInt8 first;
95 [message getBytes:&first length:1];
96 isSimpleValue = first != '{' && first != '[';
97 if (isSimpleValue) {
98 // NSJSONSerialization does not support top-level simple values.
99 // We expand encoding to singleton array, then decode that and extract
100 // the single entry.
101 UInt8 begin = '[';
102 UInt8 end = ']';
103 NSMutableData* expandedMessage = [NSMutableData dataWithLength:message.length + 2];
104 [expandedMessage replaceBytesInRange:NSMakeRange(0, 1) withBytes:&begin];
105 [expandedMessage replaceBytesInRange:NSMakeRange(1, message.length) withBytes:message.bytes];
106 [expandedMessage replaceBytesInRange:NSMakeRange(message.length + 1, 1) withBytes:&end];
107 message = expandedMessage;
108 }
109 decoded = [NSJSONSerialization JSONObjectWithData:message options:0 error:&error];
110 }
111 NSAssert(decoded, @"Invalid JSON message, decoding failed: %@", error);
112 return isSimpleValue ? ((NSArray*)decoded)[0] : decoded;
113}
static const char * begin(const StringSlice &s)
Definition editor.cpp:252
glong glong end
const uint8_t uint32_t uint32_t GError ** error
size_t length
Win32Message message
int BOOL

◆ encode:

- (NSData *) encode: (id message
implementation

Definition at line 9 of file FlutterCodecs.mm.

66 :(id)message {
67 if (message == nil) {
68 return nil;
69 }
70 NSData* encoding;
71 NSError* error;
72 if ([message isKindOfClass:[NSArray class]] || [message isKindOfClass:[NSDictionary class]]) {
73 encoding = [NSJSONSerialization dataWithJSONObject:message options:0 error:&error];
74 } else {
75 // NSJSONSerialization does not support top-level simple values.
76 // We encode as singleton array, then extract the relevant bytes.
77 encoding = [NSJSONSerialization dataWithJSONObject:@[ message ] options:0 error:&error];
78 if (encoding) {
79 encoding = [encoding subdataWithRange:NSMakeRange(1, encoding.length - 2)];
80 }
81 }
82 NSAssert(encoding, @"Invalid JSON message, encoding failed: %@", error);
83 return encoding;
84}
const uintptr_t id

◆ sharedInstance

+ (instancetype) sharedInstance
implementation

Returns a shared instance of this FlutterMessageCodec.

Reimplemented from <FlutterMessageCodec>.

Definition at line 9 of file FlutterCodecs.mm.

58 {
59 static id _sharedInstance = nil;
60 if (!_sharedInstance) {
61 _sharedInstance = [[FlutterJSONMessageCodec alloc] init];
62 }
63 return _sharedInstance;
64}

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