forked from pkyeck/socket.IO-objc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketIOJSONSerialization.m
114 lines (86 loc) · 2.92 KB
/
SocketIOJSONSerialization.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// SocketIO.m
// v0.22 ARC
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
// by Fred Potter <[email protected]>
//
// using
// https://github.com/square/SocketRocket
// https://github.com/stig/json-framework/
//
// reusing some parts of
// /socket.io/socket.io.js
//
// Created by Philipp Kyeck http://beta-interactive.de
//
// Updated by
// samlown https://github.com/samlown
// kayleg https://github.com/kayleg
//
#import "SocketIOJSONSerialization.h"
extern NSString * const SocketIOException;
// covers the methods in SBJson and JSONKit
@interface NSObject (SocketIOJSONSerialization)
// used by both JSONKit and SBJson
- (id) objectWithData:(NSData *)data;
// Use by JSONKit serialization
- (NSString *) JSONString;
- (id) decoder;
// Used by SBJsonWriter
- (NSString *) stringWithObject:(id)object;
@end
@implementation SocketIOJSONSerialization
+ (id) objectFromJSONData:(NSData *)data error:(NSError **)error {
Class serializer;
// try SBJson first
serializer = NSClassFromString(@"SBJsonParser");
if (serializer) {
id parser;
id object;
parser = [[serializer alloc] init];
object = [parser objectWithData:data];
return object;
}
// try Foundation's JSON coder, available in OS X 10.7/iOS 5.0
serializer = NSClassFromString(@"NSJSONSerialization");
if (serializer) {
return [serializer JSONObjectWithData:data options:0 error:error];
}
// lastly, try JSONKit
serializer = NSClassFromString(@"JSONDecoder");
if (serializer) {
return [[serializer decoder] objectWithData:data];
}
// unable to find a suitable JSON deseralizer
[NSException raise:SocketIOException format:@"socket.IO-objc requires SBJson, JSONKit or an OS that has NSJSONSerialization."];
return nil;
}
+ (NSString *) JSONStringFromObject:(id)object error:(NSError **)error {
Class serializer;
NSString *jsonString;
jsonString = nil;
serializer = NSClassFromString(@"SBJsonWriter");
if (serializer) {
id writer;
writer = [[serializer alloc] init];
jsonString = [writer stringWithObject:object];
return jsonString;
}
serializer = NSClassFromString(@"NSJSONSerialization");
if (serializer) {
NSData *data;
data = [serializer dataWithJSONObject:object options:0 error:error];
jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return jsonString;
}
// lastly, try JSONKit
if ([object respondsToSelector:@selector(JSONString)]) {
return [object JSONString];
}
// unable to find a suitable JSON seralizer
[NSException raise:SocketIOException format:@"socket.IO-objc requires SBJson, JSONKit or an OS that has NSJSONSerialization."];
return nil;
}
@end