5#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h"
13 static id _sharedInstance = nil;
14 if (!_sharedInstance) {
17 return _sharedInstance;
20- (NSData*)
encode:(
id)message {
25- (NSData*)
decode:(NSData*)message {
32 static id _sharedInstance = nil;
33 if (!_sharedInstance) {
36 return _sharedInstance;
39- (NSData*)
encode:(
id)message {
43 NSAssert([
message isKindOfClass:[NSString
class]],
@"");
44 NSString* stringMessage =
message;
45 const char*
utf8 = stringMessage.UTF8String;
46 return [NSData dataWithBytes:utf8 length:strlen(utf8)];
49- (NSString*)
decode:(NSData*)message {
53 return [[NSString alloc] initWithData:message encoding:NSUTF8StringEncoding];
59 static id _sharedInstance = nil;
60 if (!_sharedInstance) {
63 return _sharedInstance;
66- (NSData*)
encode:(
id)message {
72 if ([
message isKindOfClass:[NSArray
class]] || [
message isKindOfClass:[NSDictionary
class]]) {
73 encoding = [NSJSONSerialization dataWithJSONObject:message options:0 error:&error];
77 encoding = [NSJSONSerialization dataWithJSONObject:@[ message ] options:0 error:&error];
79 encoding = [encoding subdataWithRange:NSMakeRange(1, encoding.length - 2)];
82 NSAssert(encoding,
@"Invalid JSON message, encoding failed: %@",
error);
90 BOOL isSimpleValue = NO;
95 [message getBytes:&first length:1];
96 isSimpleValue = first !=
'{' && first !=
'[';
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];
109 decoded = [NSJSONSerialization JSONObjectWithData:message options:0 error:&error];
111 NSAssert(decoded,
@"Invalid JSON message, decoding failed: %@",
error);
112 return isSimpleValue ? ((NSArray*)decoded)[0] : decoded;
118 static id _sharedInstance = nil;
119 if (!_sharedInstance) {
122 return _sharedInstance;
127 @"method" : call.method,
128 @"args" : [
self wrapNil:call.arguments],
132- (NSData*)encodeSuccessEnvelope:(
id)result {
139 [
self wrapNil:error.message],
140 [
self wrapNil:error.details],
146 id method = dictionary[@"method"];
147 id arguments = [
self unwrapNil:dictionary[@"args"]];
148 NSAssert([method isKindOfClass:[NSString
class]],
@"Invalid JSON method call");
152- (
id)decodeEnvelope:(NSData*)envelope {
154 if (array.count == 1) {
155 return [
self unwrapNil:array[0]];
157 NSAssert(array.count == 3,
@"Invalid JSON envelope");
159 id message = [
self unwrapNil:array[1]];
160 id details = [
self unwrapNil:array[2]];
161 NSAssert([
code isKindOfClass:[NSString
class]],
@"Invalid JSON envelope");
162 NSAssert(
message == nil || [
message isKindOfClass:[NSString
class]],
@"Invalid JSON envelope");
166- (
id)wrapNil:(
id)value {
169- (
id)unwrapNil:(
id)value {
static void encode(uint8_t output[16], const uint32_t input[4])
static const char * begin(const StringSlice &s)
const uint8_t uint32_t uint32_t GError ** error
instancetype sharedInstance()
instancetype errorWithCode:message:details:(NSString *code,[message] NSString *_Nullable message,[details] id _Nullable details)
instancetype sharedInstance()
instancetype sharedInstance()
instancetype methodCallWithMethodName:arguments:(NSString *method,[arguments] id _Nullable arguments)
instancetype sharedInstance()
static DecodeResult decode(std::string path)