Permalink
Browse files

minor update.

  • Loading branch information...
1 parent b93bb6d commit 9f36c74c798d72254ec5cbb0611a5579d8841454 @kishikawakatsumi committed Jan 18, 2009
View
0 Classes/JSON/JSON.h 100755 → 100644
No changes.
View
17 Classes/JSON/NSObject+SBJSON.h 100755 → 100644
@@ -56,22 +56,5 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
- (NSString *)JSONRepresentation;
-/**
- @brief Returns a string containing the receiver encoded in JSON.
-
- Pass in a dictionary with an object that returns a true value for the
- @p HumanReadable key, and the returned JSON will be human readable.
-
- @deprecated This method is deprecated. Use an instance of the SBJSON
- class and set the @p humanReadable attribute instead.
-
- This method is added as a category on NSObject but is only actually
- supported for the following objects:
- @li NSDictionary
- @li NSArray
-
- */
-- (NSString *)JSONRepresentationWithOptions:(NSDictionary *)opts;
-
@end
View
28 Classes/JSON/NSObject+SBJSON.m 100755 → 100644
@@ -33,45 +33,21 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
@implementation NSObject (NSObject_SBJSON)
- (NSString *)JSONFragment {
- SBJSON *generator = [SBJSON new];
+ SBJSON *generator = [[SBJSON new] autorelease];
NSError *error;
NSString *json = [generator stringWithFragment:self error:&error];
- [generator release];
if (!json)
NSLog(@"%@", error);
return json;
}
- (NSString *)JSONRepresentation {
- SBJSON *generator = [SBJSON new];
+ SBJSON *generator = [[SBJSON new] autorelease];
NSError *error;
NSString *json = [generator stringWithObject:self error:&error];
- [generator release];
-
- if (!json)
- NSLog(@"%@", error);
- return json;
-}
-
-- (NSString *)JSONRepresentationWithOptions:(NSDictionary *)x {
- SBJSON *generator = [SBJSON new];
-
- id o;
- if (o = [x objectForKey:@"HumanReadable"])
- [generator setHumanReadable:[o boolValue]];
-
- if (o = [x objectForKey:@"MultiLine"])
- [generator setHumanReadable:[o boolValue]];
-
- if (o = [x objectForKey:@"Pretty"])
- [generator setHumanReadable:[o boolValue]];
-
- NSError *error;
- NSString *json = [generator stringWithObject:self error:&error];
- [generator release];
if (!json)
NSLog(@"%@", error);
View
10 Classes/JSON/NSString+SBJSON.h 100755 → 100644
@@ -38,14 +38,4 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/// Returns the dictionary or array represented in the receiver, or nil on error.
- (id)JSONValue;
-/**
- @brief Returns the dictionary or array represented in the receiver, or nil on error.
- @param opts a dictionary of options
-
- Only one option is supported: MaxDepth. Set it to the maximum recursion level you want to support. 0 means no maximum level; the default is 512.
-
- @deprecated This method is deprecated. Use an instance of the SBJSON class and set the @p maxDepth attribute instead.
- */
-- (id)JSONValueWithOptions:(NSDictionary *)opts;
-
@end
View
35 Classes/JSON/NSString+SBJSON.m 100755 → 100644
@@ -33,51 +33,28 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
@implementation NSString (NSString_SBJSON)
-- (id)JSONFragmentValueWithOptions:(NSDictionary *)opts
+- (id)JSONFragmentValue
{
- SBJSON *json = [SBJSON new];
-
- if (opts) {
- id opt = [opts objectForKey:@"MaxDepth"];
- if (opt)
- [json setMaxDepth:[opt intValue]];
- }
+ SBJSON *json = [[SBJSON new] autorelease];
NSError *error;
id o = [json fragmentWithString:self error:&error];
+
if (!o)
NSLog(@"%@", error);
- [json release];
return o;
}
-
-- (id)JSONValueWithOptions:(NSDictionary *)opts
+- (id)JSONValue
{
- SBJSON *json = [SBJSON new];
-
- if (opts) {
- id opt = [opts objectForKey:@"MaxDepth"];
- if (opt)
- [json setMaxDepth:[opt intValue]];
- }
+ SBJSON *json = [[SBJSON new] autorelease];
NSError *error;
id o = [json objectWithString:self error:&error];
+
if (!o)
NSLog(@"%@", error);
- [json release];
return o;
}
-- (id)JSONFragmentValue
-{
- return [self JSONFragmentValueWithOptions:nil];
-}
-
-- (id)JSONValue
-{
- return [self JSONValueWithOptions:nil];
-}
-
@end
View
45 Classes/JSON/SBJSON.h 100755 → 100644
@@ -42,6 +42,8 @@ enum {
EESCAPE,
ETRAILCOMMA,
ETRAILGARBAGE,
+ EEOF,
+ EINPUT
};
/**
@@ -70,40 +72,45 @@ way you would expect. JSON numbers turn into NSDecimalNumber instances,
as we can thus avoid any loss of precision.
Strictly speaking correctly formed JSON text must have <strong>exactly
-one top-level container</strong>. (Either an Array or an Object.) Bare
-nulls, numbers, booleans and strings are not valid JSON on their own.
+one top-level container</strong>. (Either an Array or an Object.) Scalars,
+i.e. nulls, numbers, booleans and strings, are not valid JSON on their own.
It can be quite convenient to pretend that such fragments are valid
-JSON however. This class lets you do so.
+JSON however, and this class lets you do so.
This class does its best to be as strict as possible, both in what it
accepts and what it generates. (Other than the above mentioned support
for JSON fragments.) For example, it does not support trailing commas
in arrays or objects. Nor does it support embedded comments, or
anything else not in the JSON specification.
-
+
*/
@interface SBJSON : NSObject {
- // Attributes
BOOL humanReadable;
- unsigned maxDepth;
+ BOOL sortKeys;
+ NSUInteger maxDepth;
@private
// Used temporarily during scanning/generation
- unsigned depth;
+ NSUInteger depth;
const char *c;
}
/// Whether we are generating human-readable (multiline) JSON
-- (BOOL)humanReadable;
+/**
+ Set whether or not to generate human-readable JSON. The default is NO, which produces
+ JSON without any whitespace. (Except inside strings.) If set to YES, generates human-readable
+ JSON with linebreaks after each array value and dictionary key/value pair, indented two
+ spaces per nesting level.
+ */
+@property BOOL humanReadable;
-/// Set whether to generate human-readable JSON or not
-- (void)setHumanReadable:(BOOL)y;
+/// Whether or not to sort the dictionary keys in the output
+/** The default is to not sort the keys. */
+@property BOOL sortKeys;
/// The maximum depth the parser will go to
-- (unsigned)maxDepth;
-
-/// Set the maximum depth the parser will go to
-- (void)setMaxDepth:(unsigned)y;
+/** Defaults to 512. */
+@property NSUInteger maxDepth;
/// Return JSON representation of an array or dictionary
- (NSString*)stringWithObject:(id)value error:(NSError**)error;
@@ -117,4 +124,14 @@ anything else not in the JSON specification.
/// Return the fragment represented by the given string
- (id)fragmentWithString:(NSString*)jsonrep error:(NSError**)error;
+/// Return JSON representation (or fragment) for the given object
+- (NSString*)stringWithObject:(id)value
+ allowScalar:(BOOL)x
+ error:(NSError**)error;
+
+/// Parse the string and return the represented object (or scalar)
+- (id)objectWithString:(id)value
+ allowScalar:(BOOL)x
+ error:(NSError**)error;
+
@end
Oops, something went wrong.

0 comments on commit 9f36c74

Please sign in to comment.