13 static id _sharedInstance = nil;
14 if (!_sharedInstance) {
17 return _sharedInstance;
20 - (NSData*)encode:(
id)message {
21 NSAssert(!message || [message isKindOfClass:[NSData
class]],
@"");
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);
86 - (id)decode:(NSData*)message {
87 if ([message length] == 0) {
90 BOOL isSimpleValue = NO;
93 if (0 < message.length) {
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];
107 message = expandedMessage;
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;
117 + (instancetype)sharedInstance {
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 {
167 return value == nil ? [NSNull null] : value;
169 - (id)unwrapNil:(
id)value {
170 return value == [NSNull null] ? nil : value;