root / json-framework-3.2.0 / Classes / SBJsonWriter.m @ 3ebe9884
History | View | Annotate | Download (3.5 kB)
1 |
/* |
---|---|
2 |
Copyright (C) 2009 Stig Brautaset. All rights reserved. |
3 |
|
4 |
Redistribution and use in source and binary forms, with or without |
5 |
modification, are permitted provided that the following conditions are met: |
6 |
|
7 |
* Redistributions of source code must retain the above copyright notice, this |
8 |
list of conditions and the following disclaimer. |
9 |
|
10 |
* Redistributions in binary form must reproduce the above copyright notice, |
11 |
this list of conditions and the following disclaimer in the documentation |
12 |
and/or other materials provided with the distribution. |
13 |
|
14 |
* Neither the name of the author nor the names of its contributors may be used |
15 |
to endorse or promote products derived from this software without specific |
16 |
prior written permission. |
17 |
|
18 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
22 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
24 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
25 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
26 |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 |
*/ |
29 |
|
30 |
#if !__has_feature(objc_arc) |
31 |
#error "This source file must be compiled with ARC enabled!" |
32 |
#endif |
33 |
|
34 |
#import "SBJsonWriter.h" |
35 |
#import "SBJsonStreamWriter.h" |
36 |
#import "SBJsonStreamWriterAccumulator.h" |
37 |
|
38 |
|
39 |
@interface SBJsonWriter () |
40 |
@property (copy) NSString *error; |
41 |
@end |
42 |
|
43 |
@implementation SBJsonWriter |
44 |
|
45 |
@synthesize sortKeys; |
46 |
@synthesize humanReadable; |
47 |
|
48 |
@synthesize error; |
49 |
@synthesize maxDepth; |
50 |
|
51 |
@synthesize sortKeysComparator; |
52 |
|
53 |
- (id)init { |
54 |
self = [super init]; |
55 |
if (self) { |
56 |
self.maxDepth = 32u; |
57 |
} |
58 |
return self; |
59 |
} |
60 |
|
61 |
|
62 |
- (NSString*)stringWithObject:(id)value { |
63 |
NSData *data = [self dataWithObject:value]; |
64 |
if (data) |
65 |
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; |
66 |
return nil; |
67 |
} |
68 |
|
69 |
- (NSString*)stringWithObject:(id)value error:(NSError**)error_ { |
70 |
NSString *tmp = [self stringWithObject:value]; |
71 |
if (tmp) |
72 |
return tmp; |
73 |
|
74 |
if (error_) { |
75 |
NSDictionary *ui = [NSDictionary dictionaryWithObjectsAndKeys:error, NSLocalizedDescriptionKey, nil]; |
76 |
*error_ = [NSError errorWithDomain:@"org.brautaset.SBJsonWriter.ErrorDomain" code:0 userInfo:ui]; |
77 |
} |
78 |
|
79 |
return nil; |
80 |
} |
81 |
|
82 |
- (NSData*)dataWithObject:(id)object { |
83 |
self.error = nil; |
84 |
|
85 |
SBJsonStreamWriterAccumulator *accumulator = [[SBJsonStreamWriterAccumulator alloc] init]; |
86 |
|
87 |
SBJsonStreamWriter *streamWriter = [[SBJsonStreamWriter alloc] init]; |
88 |
streamWriter.sortKeys = self.sortKeys; |
89 |
streamWriter.maxDepth = self.maxDepth; |
90 |
streamWriter.sortKeysComparator = self.sortKeysComparator; |
91 |
streamWriter.humanReadable = self.humanReadable; |
92 |
streamWriter.delegate = accumulator; |
93 |
|
94 |
BOOL ok = NO; |
95 |
if ([object isKindOfClass:[NSDictionary class]]) |
96 |
ok = [streamWriter writeObject:object]; |
97 |
|
98 |
else if ([object isKindOfClass:[NSArray class]]) |
99 |
ok = [streamWriter writeArray:object]; |
100 |
|
101 |
else if ([object respondsToSelector:@selector(proxyForJson)]) |
102 |
return [self dataWithObject:[object proxyForJson]]; |
103 |
else { |
104 |
self.error = @"Not valid type for JSON"; |
105 |
return nil; |
106 |
} |
107 |
|
108 |
if (ok) |
109 |
return accumulator.data; |
110 |
|
111 |
self.error = streamWriter.error; |
112 |
return nil; |
113 |
} |
114 |
|
115 |
|
116 |
@end |