Skip to content

Commit 2419091

Browse files
author
Zachary Schneirov
committed
use BJSON for simplenote parsing
1 parent 18a8c38 commit 2419091

11 files changed

+753
-0
lines changed

Acknowledgments.txt

+24
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@ under the License.
112112

113113
--
114114

115+
BSJSONAdditions
116+
117+
Created by Blake Seely on 2009/03/24.
118+
Copyright 2006 Blake Seely - http://www.blakeseely.com All rights reserved.
119+
Permission to use this code:
120+
121+
Feel free to use this code in your software, either as-is or
122+
in a modified form. Either way, please include a credit in
123+
your software's "About" box or similar, mentioning at least
124+
my name (Blake Seely).
125+
126+
Permission to redistribute this code:
127+
128+
You can redistribute this code, as long as you keep these
129+
comments. You can also redistribute modified versions of the
130+
code, as long as you add comments to say that you've made
131+
modifications (keeping these original comments too).
132+
133+
If you do use or redistribute this code, an email would be
134+
appreciated, just to let me know that people are finding my
135+
code useful. You can reach me at blakeseely@mac.com
136+
137+
--
138+
115139
OpenSSL License
116140
---------------
117141

JSON/BSJSONEncoder.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// BSJSONEncoder.h
3+
// BSJSONAdditions
4+
//
5+
6+
#import <Foundation/Foundation.h>
7+
8+
@interface BSJSONEncoder : NSObject
9+
+ (NSString *)jsonStringForValue:(id)value withIndentLevel:(NSInteger)level;
10+
@end

JSON/BSJSONEncoder.m

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// BSJSONEncoder.m
3+
// BSJSONAdditions
4+
//
5+
6+
#import "BSJSONEncoder.h"
7+
#import "NSArray+BSJSONAdditions.h"
8+
#import "NSDictionary+BSJSONAdditions.h"
9+
#import "NSScanner+BSJSONAdditions.h"
10+
#import "NSString+BSJSONAdditions.h"
11+
12+
@implementation BSJSONEncoder
13+
14+
+ (NSString *)jsonStringForValue:(id)value withIndentLevel:(NSInteger)level
15+
{
16+
NSString *jsonString;
17+
if ([value respondsToSelector:@selector(characterAtIndex:)]) { // String
18+
jsonString = [(NSString *)value jsonStringValue];
19+
} else if ([value respondsToSelector:@selector(keyEnumerator)]) { // Dictionary
20+
jsonString = [(NSDictionary *)value jsonStringValueWithIndentLevel:(level + 1)];
21+
} else if ([value respondsToSelector:@selector(objectAtIndex:)]) { // Array
22+
jsonString = [(NSArray *)value jsonStringValueWithIndentLevel:level];
23+
} else if (value == [NSNull null]) { // null
24+
jsonString = jsonNullString;
25+
} else if ([value respondsToSelector:@selector(objCType)]) { // NSNumber - representing true, false, and any form of numeric
26+
NSNumber *number = (NSNumber *)value;
27+
if (((*[number objCType]) == 'c') && ([number boolValue] == YES)) { // true
28+
jsonString = jsonTrueString;
29+
} else if (((*[number objCType]) == 'c') && ([number boolValue] == NO)) { // false
30+
jsonString = jsonFalseString;
31+
} else { // attempt to handle as a decimal number - int, fractional, exponential
32+
// TODO: values converted from exponential json to dict and back to json do not format as exponential again
33+
jsonString = [[NSDecimalNumber decimalNumberWithDecimal:[number decimalValue]] stringValue];
34+
}
35+
} else {
36+
// TODO: error condition - it's not any of the types that I know about.
37+
return nil;
38+
}
39+
40+
return jsonString;
41+
}
42+
43+
@end

JSON/NSArray+BSJSONAdditions.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// BSJSONAdditions
3+
//
4+
// Created by Blake Seely on 2009/03/24.
5+
// Copyright 2006 Blake Seely - http://www.blakeseely.com All rights reserved.
6+
// Permission to use this code:
7+
//
8+
// Feel free to use this code in your software, either as-is or
9+
// in a modified form. Either way, please include a credit in
10+
// your software's "About" box or similar, mentioning at least
11+
// my name (Blake Seely).
12+
//
13+
// Permission to redistribute this code:
14+
//
15+
// You can redistribute this code, as long as you keep these
16+
// comments. You can also redistribute modified versions of the
17+
// code, as long as you add comments to say that you've made
18+
// modifications (keeping these original comments too).
19+
//
20+
// If you do use or redistribute this code, an email would be
21+
// appreciated, just to let me know that people are finding my
22+
// code useful. You can reach me at blakeseely@mac.com
23+
24+
#import <Foundation/Foundation.h>
25+
26+
#import "NSDictionary+BSJSONAdditions.h"
27+
28+
@interface NSArray (BSJSONAdditions)
29+
30+
+ (NSArray *)arrayWithJSONString:(NSString *)jsonString;
31+
- (NSString *)jsonStringValue;
32+
- (NSString *)jsonStringValueWithIndentLevel:(NSInteger)level;
33+
34+
@end

JSON/NSArray+BSJSONAdditions.m

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// BSJSONAdditions
2+
//
3+
// Created by Blake Seely on 2009/03/24.
4+
// Copyright 2006 Blake Seely - http://www.blakeseely.com All rights reserved.
5+
// Permission to use this code:
6+
//
7+
// Feel free to use this code in your software, either as-is or
8+
// in a modified form. Either way, please include a credit in
9+
// your software's "About" box or similar, mentioning at least
10+
// my name (Blake Seely).
11+
//
12+
// Permission to redistribute this code:
13+
//
14+
// You can redistribute this code, as long as you keep these
15+
// comments. You can also redistribute modified versions of the
16+
// code, as long as you add comments to say that you've made
17+
// modifications (keeping these original comments too).
18+
//
19+
// If you do use or redistribute this code, an email would be
20+
// appreciated, just to let me know that people are finding my
21+
// code useful. You can reach me at blakeseely@mac.com
22+
23+
#import "NSArray+BSJSONAdditions.h"
24+
#import "NSScanner+BSJSONAdditions.h"
25+
#import "BSJSONEncoder.h"
26+
27+
@implementation NSArray (BSJSONAdditions)
28+
29+
+ (NSArray *)arrayWithJSONString:(NSString *)jsonString
30+
{
31+
NSScanner *scanner = [[NSScanner alloc] initWithString:jsonString];
32+
NSArray *array = nil;
33+
[scanner scanJSONArray:&array];
34+
[scanner release];
35+
36+
return array;
37+
}
38+
39+
- (NSString *)jsonStringValue
40+
{
41+
return [self jsonStringValueWithIndentLevel:0];
42+
}
43+
44+
- (NSString *)jsonStringValueWithIndentLevel:(NSInteger)level
45+
{
46+
NSMutableString *jsonString = [[NSMutableString alloc] init];
47+
[jsonString appendString:jsonArrayStartString];
48+
49+
if ([self count] > 0) {
50+
[jsonString appendString:[BSJSONEncoder jsonStringForValue:[self objectAtIndex:0] withIndentLevel:level]];
51+
}
52+
53+
NSUInteger i;
54+
NSString *encoded;
55+
for (i = 1; i < [self count]; i++) {
56+
encoded = [BSJSONEncoder jsonStringForValue:[self objectAtIndex:i] withIndentLevel:level];
57+
[jsonString appendFormat:@"%@ %@", jsonValueSeparatorString, encoded];
58+
}
59+
60+
[jsonString appendString:jsonArrayEndString];
61+
return [jsonString autorelease];
62+
}
63+
64+
@end

JSON/NSDictionary+BSJSONAdditions.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// BSJSONAdditions
3+
//
4+
// Created by Blake Seely on 2/1/06.
5+
// Copyright 2006 Blake Seely - http://www.blakeseely.com All rights reserved.
6+
// Permission to use this code:
7+
//
8+
// Feel free to use this code in your software, either as-is or
9+
// in a modified form. Either way, please include a credit in
10+
// your software's "About" box or similar, mentioning at least
11+
// my name (Blake Seely).
12+
//
13+
// Permission to redistribute this code:
14+
//
15+
// You can redistribute this code, as long as you keep these
16+
// comments. You can also redistribute modified versions of the
17+
// code, as long as you add comments to say that you've made
18+
// modifications (keeping these original comments too).
19+
//
20+
// If you do use or redistribute this code, an email would be
21+
// appreciated, just to let me know that people are finding my
22+
// code useful. You can reach me at blakeseely@mac.com
23+
24+
#import <Foundation/Foundation.h>
25+
26+
@interface NSDictionary (BSJSONAdditions)
27+
+ (NSDictionary *)dictionaryWithJSONString:(NSString *)jsonString;
28+
29+
- (NSString *)jsonStringValue;
30+
- (NSString *)jsonStringValueWithIndentLevel:(NSInteger)level;
31+
@end

JSON/NSDictionary+BSJSONAdditions.m

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// BSJSONAdditions
3+
//
4+
// Created by Blake Seely on 2/1/06.
5+
// Copyright 2006 Blake Seely - http://www.blakeseely.com All rights reserved.
6+
// Permission to use this code:
7+
//
8+
// Feel free to use this code in your software, either as-is or
9+
// in a modified form. Either way, please include a credit in
10+
// your software's "About" box or similar, mentioning at least
11+
// my name (Blake Seely).
12+
//
13+
// Permission to redistribute this code:
14+
//
15+
// You can redistribute this code, as long as you keep these
16+
// comments. You can also redistribute modified versions of the
17+
// code, as long as you add comments to say that you've made
18+
// modifications (keeping these original comments too).
19+
//
20+
// If you do use or redistribute this code, an email would be
21+
// appreciated, just to let me know that people are finding my
22+
// code useful. You can reach me at blakeseely@mac.com
23+
24+
#import "NSDictionary+BSJSONAdditions.h"
25+
#import "NSScanner+BSJSONAdditions.h"
26+
#import "NSString+BSJSONAdditions.h"
27+
#import "BSJSONEncoder.h"
28+
29+
@implementation NSDictionary (BSJSONAdditions)
30+
31+
+ (NSDictionary *)dictionaryWithJSONString:(NSString *)jsonString
32+
{
33+
NSScanner *scanner = [[NSScanner alloc] initWithString:jsonString];
34+
NSDictionary *dictionary = nil;
35+
[scanner scanJSONObject:&dictionary];
36+
[scanner release];
37+
return dictionary;
38+
}
39+
40+
- (NSString *)jsonStringValue
41+
{
42+
return [self jsonStringValueWithIndentLevel:0];
43+
}
44+
45+
- (NSString *)jsonStringValueWithIndentLevel:(NSInteger)level
46+
{
47+
NSMutableString *jsonString = [[NSMutableString alloc] initWithString:jsonObjectStartString];
48+
49+
BOOL first = YES;
50+
51+
NSEnumerator *enumerator = [self keyEnumerator];
52+
NSString *keyString = nil;
53+
while ((keyString = [enumerator nextObject])) {
54+
NSString *valueString = [BSJSONEncoder jsonStringForValue:[self objectForKey:keyString] withIndentLevel:level];
55+
if (!first) {
56+
[jsonString appendString:jsonValueSeparatorString];
57+
}
58+
if (level != jsonDoNotIndent) { // indent before each key
59+
[jsonString appendString:[NSString jsonIndentStringForLevel:level]];
60+
}
61+
[jsonString appendFormat:@" %@ %@ %@", [keyString jsonStringValue], jsonKeyValueSeparatorString, valueString];
62+
first = NO;
63+
}
64+
65+
[jsonString appendString:jsonObjectEndString];
66+
return [jsonString autorelease];
67+
}
68+
69+
@end

JSON/NSScanner+BSJSONAdditions.h

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// BSJSONAdditions
3+
//
4+
// Created by Blake Seely on 2/1/06.
5+
// Copyright 2006 Blake Seely - http://www.blakeseely.com All rights reserved.
6+
// Permission to use this code:
7+
//
8+
// Feel free to use this code in your software, either as-is or
9+
// in a modified form. Either way, please include a credit in
10+
// your software's "About" box or similar, mentioning at least
11+
// my name (Blake Seely).
12+
//
13+
// Permission to redistribute this code:
14+
//
15+
// You can redistribute this code, as long as you keep these
16+
// comments. You can also redistribute modified versions of the
17+
// code, as long as you add comments to say that you've made
18+
// modifications (keeping these original comments too).
19+
//
20+
// If you do use or redistribute this code, an email would be
21+
// appreciated, just to let me know that people are finding my
22+
// code useful. You can reach me at blakeseely@mac.com
23+
//
24+
25+
#import <Foundation/Foundation.h>
26+
27+
extern NSString *jsonObjectStartString;
28+
extern NSString *jsonObjectEndString;
29+
extern NSString *jsonArrayStartString;
30+
extern NSString *jsonArrayEndString;
31+
extern NSString *jsonKeyValueSeparatorString;
32+
extern NSString *jsonValueSeparatorString;
33+
extern NSString *jsonStringDelimiterString;
34+
extern NSString *jsonStringEscapedDoubleQuoteString;
35+
extern NSString *jsonStringEscapedSlashString;
36+
extern NSString *jsonTrueString;
37+
extern NSString *jsonFalseString;
38+
extern NSString *jsonNullString;
39+
extern NSString *jsonIndentString;
40+
extern const NSInteger jsonDoNotIndent;
41+
42+
43+
@interface NSScanner (PrivateBSJSONAdditions)
44+
45+
- (BOOL)scanJSONObject:(NSDictionary **)dictionary;
46+
- (BOOL)scanJSONArray:(NSArray **)array;
47+
- (BOOL)scanJSONString:(NSString **)string;
48+
- (BOOL)scanJSONValue:(id *)value;
49+
- (BOOL)scanJSONNumber:(NSNumber **)number;
50+
51+
- (BOOL)scanJSONWhiteSpace;
52+
- (BOOL)scanJSONKeyValueSeparator;
53+
- (BOOL)scanJSONValueSeparator;
54+
- (BOOL)scanJSONObjectStartString;
55+
- (BOOL)scanJSONObjectEndString;
56+
- (BOOL)scanJSONArrayStartString;
57+
- (BOOL)scanJSONArrayEndString;
58+
- (BOOL)scanJSONArrayEndString;
59+
- (BOOL)scanJSONStringDelimiterString;
60+
61+
- (BOOL)scanUnicodeCharacterIntoString:(NSMutableString *)string;
62+
63+
@end

0 commit comments

Comments
 (0)