This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
TypeJSON.m
166 lines (128 loc) · 3.98 KB
/
TypeJSON.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#import "TypeJSON.h"
#import <CoreFoundation/CoreFoundation.h>
NSString *TypeJSONErrorDomain = @"TypeJSONErrorDomain";
typedef NS_ENUM(NSInteger, TypeJSONErrorCode) {
TypeJSONErrorCodeNotFound = 404,
TypeJSONErrorCodeNotAcceptable = 406,
};
@interface TypeJSON ()
@property (nonatomic, strong) id value;
@end
@implementation TypeJSON
+ (instancetype)fromData:(NSData *)data {
return [[self alloc] initWithData:data];
}
+ (instancetype)emptyObject {
return [[self alloc] initWithValue:@{}];
}
- (instancetype)initWithData:(NSData *)data {
self = [self init];
if (self) {
NSError *error;
self.value = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
if (error) {
self.value = error;
}
}
return self;
}
- (instancetype)initWithValue:(id)value {
self = [self init];
if (self) {
self.value = value;
}
return self;
}
- (instancetype)initWithError:(TypeJSONErrorCode)errorCode {
return [self initWithValue:[NSError errorWithDomain:TypeJSONErrorDomain
code:errorCode
userInfo:nil]];
}
#pragma mark Collection Accessors
- (TypeJSON *)objectForKeyedSubscript:(NSString *)key {
if (self.isError) {
return self;
} else if ([self.value respondsToSelector:@selector(objectForKeyedSubscript:)]) {
id newRoot = self.value[key];
if (newRoot) {
return [[TypeJSON alloc] initWithValue:newRoot];
} else {
return [[TypeJSON alloc] initWithError:TypeJSONErrorCodeNotFound];
}
} else {
return [[TypeJSON alloc] initWithError:TypeJSONErrorCodeNotAcceptable];
}
}
- (TypeJSON *)objectAtIndexedSubscript:(NSUInteger)index {
if (self.isError) {
return self;
} else if ([self.value respondsToSelector:@selector(objectAtIndexedSubscript:)] && [self.value respondsToSelector:@selector(count)]) {
if (index < [self.value count]) {
return [[TypeJSON alloc] initWithValue:self.value[index]];
} else {
return [[TypeJSON alloc] initWithError:TypeJSONErrorCodeNotFound];
}
} else {
return [[TypeJSON alloc] initWithError:TypeJSONErrorCodeNotAcceptable];
}
}
#pragma mark Value Accessors
- (NSString *)asString {
return self.isString ? self.value : nil;
}
- (NSDecimalNumber *)asNumber {
return self.isNumber ? [NSDecimalNumber decimalNumberWithDecimal:[self.value decimalValue]] : nil;
}
- (BOOL)asBool {
return self.isTrue;
}
- (NSNull *)asNull {
return self.isNull ? self.value : nil;
}
- (NSArray *)asArray {
return self.isArray ? self.value : nil;
}
- (NSDictionary *)asObject {
return self.isObject ? self.value : nil;
}
#pragma mark Type Checking
- (BOOL)isTrue {
return [self.value isEqual:@(YES)];
}
- (BOOL)isFalse {
return [self.value isEqual:@(NO)];
}
- (BOOL)isNull {
return [self.value isEqual:NSNull.null];
}
- (BOOL)isNumber {
return [self.value isKindOfClass:[NSNumber class]] && CFGetTypeID((__bridge CFTypeRef)self.value) != CFBooleanGetTypeID();
}
- (BOOL)isString {
return [self.value isKindOfClass:[NSString class]];
}
- (BOOL)isArray {
return [self.value isKindOfClass:[NSArray class]];
}
- (BOOL)isObject {
return [self.value isKindOfClass:[NSDictionary class]];
}
#pragma mark Error Handling
- (BOOL)isError {
return [self.value isKindOfClass:[NSError class]];
}
- (NSError *)asError {
return self.isError ? self.value : nil;
}
#pragma mark Serialization
- (NSData *)asJSON {
return [self asJSONWithOptions:0];
}
- (NSData *)asJSONWithOptions:(NSJSONWritingOptions)options {
return [NSJSONSerialization dataWithJSONObject:self.value
options:options
error:nil];
}
@end