-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGRLJSON.m
More file actions
197 lines (150 loc) · 5 KB
/
GRLJSON.m
File metadata and controls
197 lines (150 loc) · 5 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// GRLJSON.m
// GRLJSON
//
// Created by Måns Severin on 2010-03-09.
// Copyright 2010 Graphiclife. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#import "GRLJSON.h"
#import "GRLJSONCoder.h"
#import "GRLJSONCoder_Internal.h"
#import "GRLJSONCoding.h"
#import "GRLJSONValueTransformer.h"
#import "grl_json_parser.h"
@interface GRLJSON (Private)
+ (id)createJSONObjectFromValue:(struct grl_json_value *)value preserveNull:(BOOL)preserveNull;
@end
@implementation GRLJSON
+ (void)init
{
[GRLJSONValueTransformer self];
}
+ (NSData *)serializeArray:(NSArray *)array
{
return [self serializeArray:array tidy:NO];
}
+ (NSData *)serializeDictionary:(NSDictionary *)dictionary
{
return [self serializeDictionary:dictionary tidy:NO];
}
+ (NSData *)serializeObject:(id <GRLJSONCoding>)object
{
return [self serializeObject:object tidy:NO];
}
+ (NSData *)serializeArray:(NSArray *)array tidy:(BOOL)tidy
{
GRLJSONCoder *encoder = [[GRLJSONCoder alloc] init];
NSData *data;
[encoder encodeRootObject:kGRLJSONObjectTypeSingle];
[encoder encodeArray:array];
data = [encoder serializeByTidying:tidy];
[encoder release];
return data;
}
+ (NSData *)serializeDictionary:(NSDictionary *)dictionary tidy:(BOOL)tidy
{
GRLJSONCoder *encoder = [[GRLJSONCoder alloc] init];
NSData *data;
[encoder encodeRootObject:kGRLJSONObjectTypeSingle];
[encoder encodeDictionary:dictionary];
data = [encoder serializeByTidying:tidy];
[encoder release];
return data;
}
+ (NSData *)serializeObject:(id <GRLJSONCoding>)object tidy:(BOOL)tidy
{
GRLJSONCoder *encoder = [[GRLJSONCoder alloc] init];
NSData *data;
[encoder encodeRootObject:kGRLJSONObjectTypeSingle];
[encoder encodeObject:object];
data = [encoder serializeByTidying:tidy];
[encoder release];
return data;
}
+ (id)deserializeData:(NSData *)data error:(NSError **)error
{
return [self deserializeData:data preserveNulls:NO error:error];
}
+ (id)deserializeData:(NSData *)data preserveNulls:(BOOL)preserveNulls error:(NSError **)error
{
id result = nil;
struct grl_json_parse_context context;
grl_json_init( &context );
grl_json_load( &context, [data bytes], [data length] );
if ( grl_json_parse( &context ) == 0 )
{
result = [self createJSONObjectFromValue:context.result preserveNull:preserveNulls];
}
else
{
}
grl_json_close( &context );
grl_json_destroy( &context );
return result;
}
+ (id)createJSONObjectFromValue:(struct grl_json_value *)value preserveNull:(BOOL)preserveNull
{
NSMutableArray *array;
NSMutableDictionary *dictionary;
struct grl_json_value_list *vlist;
struct grl_json_pair_list *plist;
id json;
switch ( value->type )
{
case grl_json_value_type_null:
return ( preserveNull ? [NSNull null] : nil );
case grl_json_value_type_integer:
return [NSNumber numberWithLongLong:value->value.integer_value];
case grl_json_value_type_float:
return [NSNumber numberWithDouble:value->value.float_value];
case grl_json_value_type_boolean:
return [NSNumber numberWithBool:value->value.boolean_value];
case grl_json_value_type_string:
return [NSString stringWithCharacters:value->value.string_value->characters length:value->value.string_value->length];
case grl_json_value_type_array:
array = [NSMutableArray array];
for ( vlist = value->value.array_value ; vlist ; vlist = vlist->next )
{
json = [self createJSONObjectFromValue:vlist->value preserveNull:preserveNull];
if ( json )
{
[array addObject:json];
}
}
return array;
case grl_json_value_type_object:
dictionary = [NSMutableDictionary dictionary];
for ( plist = value->value.object_value ; plist ; plist = plist->next )
{
json = [self createJSONObjectFromValue:plist->pair->value preserveNull:preserveNull];
if ( json )
{
[dictionary setObject:json forKey:[NSString stringWithCharacters:plist->pair->key->characters length:plist->pair->key->length]];
}
}
return dictionary;
}
return nil;
}
@end