forked from blakeseely/bsjsonadditions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BSJSONEncoder.m
43 lines (38 loc) · 1.65 KB
/
BSJSONEncoder.m
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
//
// BSJSONEncoder.m
// BSJSONAdditions
//
#import "BSJSONEncoder.h"
#import "NSArray+BSJSONAdditions.h"
#import "NSDictionary+BSJSONAdditions.h"
#import "NSScanner+BSJSONAdditions.h"
#import "NSString+BSJSONAdditions.h"
@implementation BSJSONEncoder
+ (NSString *)jsonStringForValue:(id)value withIndentLevel:(NSInteger)level
{
NSString *jsonString;
if ([value respondsToSelector:@selector(characterAtIndex:)]) { // String
jsonString = [(NSString *)value jsonStringValue];
} else if ([value respondsToSelector:@selector(keyEnumerator)]) { // Dictionary
jsonString = [(NSDictionary *)value jsonStringValueWithIndentLevel:(level + 1)];
} else if ([value respondsToSelector:@selector(objectAtIndex:)]) { // Array
jsonString = [(NSArray *)value jsonStringValueWithIndentLevel:level];
} else if (value == [NSNull null]) { // null
jsonString = jsonNullString;
} else if ([value respondsToSelector:@selector(objCType)]) { // NSNumber - representing true, false, and any form of numeric
NSNumber *number = (NSNumber *)value;
if (((*[number objCType]) == 'c') && ([number boolValue] == YES)) { // true
jsonString = jsonTrueString;
} else if (((*[number objCType]) == 'c') && ([number boolValue] == NO)) { // false
jsonString = jsonFalseString;
} else { // attempt to handle as a decimal number - int, fractional, exponential
// TODO: values converted from exponential json to dict and back to json do not format as exponential again
jsonString = [[NSDecimalNumber decimalNumberWithDecimal:[number decimalValue]] stringValue];
}
} else {
// TODO: error condition - it's not any of the types that I know about.
return nil;
}
return jsonString;
}
@end