Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 138 lines (109 sloc) 5.071 kb
b8c3477 @rickfillion Import into GitHub
rickfillion authored
1 /*
2 Copyright (C) 2008 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 #import <Foundation/Foundation.h>
31
32 extern NSString * SBJSONErrorDomain;
33
34 enum {
35 EUNSUPPORTED = 1,
36 EPARSENUM,
37 EPARSE,
38 EFRAGMENT,
39 ECTRL,
40 EUNICODE,
41 EDEPTH,
42 EESCAPE,
43 ETRAILCOMMA,
44 ETRAILGARBAGE,
45 EEOF,
46 EINPUT
47 };
48
49 /**
50 @brief A strict JSON parser and generator
51
52 This is the parser and generator underlying the categories added to
53 NSString and various other objects.
54
55 Objective-C types are mapped to JSON types and back in the following way:
56
57 @li NSNull -> Null -> NSNull
58 @li NSString -> String -> NSMutableString
59 @li NSArray -> Array -> NSMutableArray
60 @li NSDictionary -> Object -> NSMutableDictionary
61 @li NSNumber (-initWithBool:) -> Boolean -> NSNumber -initWithBool:
62 @li NSNumber -> Number -> NSDecimalNumber
63
64 In JSON the keys of an object must be strings. NSDictionary keys need
65 not be, but attempting to convert an NSDictionary with non-string keys
66 into JSON will throw an exception.
67
68 NSNumber instances created with the +numberWithBool: method are
69 converted into the JSON boolean "true" and "false" values, and vice
70 versa. Any other NSNumber instances are converted to a JSON number the
71 way you would expect. JSON numbers turn into NSDecimalNumber instances,
72 as we can thus avoid any loss of precision.
73
74 Strictly speaking correctly formed JSON text must have <strong>exactly
75 one top-level container</strong>. (Either an Array or an Object.) Scalars,
76 i.e. nulls, numbers, booleans and strings, are not valid JSON on their own.
77 It can be quite convenient to pretend that such fragments are valid
78 JSON however, and this class lets you do so.
79
80 This class does its best to be as strict as possible, both in what it
81 accepts and what it generates. (Other than the above mentioned support
82 for JSON fragments.) For example, it does not support trailing commas
83 in arrays or objects. Nor does it support embedded comments, or
84 anything else not in the JSON specification.
85
86 */
87 @interface SBJSON : NSObject {
88 BOOL humanReadable;
89 BOOL sortKeys;
90 NSUInteger maxDepth;
91
92 @private
93 // Used temporarily during scanning/generation
94 NSUInteger depth;
95 const char *c;
96 }
97
98 /// Whether we are generating human-readable (multiline) JSON
99 /**
100 Set whether or not to generate human-readable JSON. The default is NO, which produces
101 JSON without any whitespace. (Except inside strings.) If set to YES, generates human-readable
102 JSON with linebreaks after each array value and dictionary key/value pair, indented two
103 spaces per nesting level.
104 */
105 @property BOOL humanReadable;
106
107 /// Whether or not to sort the dictionary keys in the output
108 /** The default is to not sort the keys. */
109 @property BOOL sortKeys;
110
111 /// The maximum depth the parser will go to
112 /** Defaults to 512. */
113 @property NSUInteger maxDepth;
114
115 /// Return JSON representation of an array or dictionary
116 - (NSString*)stringWithObject:(id)value error:(NSError**)error;
117
118 /// Return JSON representation of any legal JSON value
119 - (NSString*)stringWithFragment:(id)value error:(NSError**)error;
120
121 /// Return the object represented by the given string
122 - (id)objectWithString:(NSString*)jsonrep error:(NSError**)error;
123
124 /// Return the fragment represented by the given string
125 - (id)fragmentWithString:(NSString*)jsonrep error:(NSError**)error;
126
127 /// Return JSON representation (or fragment) for the given object
128 - (NSString*)stringWithObject:(id)value
129 allowScalar:(BOOL)x
130 error:(NSError**)error;
131
132 /// Parse the string and return the represented object (or scalar)
133 - (id)objectWithString:(id)value
134 allowScalar:(BOOL)x
135 error:(NSError**)error;
136
137 @end
Something went wrong with that request. Please try again.