This repository has been archived by the owner on Apr 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathNSString+BSJSONAdditions.m
73 lines (66 loc) · 1.69 KB
/
NSString+BSJSONAdditions.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// NSString+BSJSONAdditions.m
// BSJSONAdditions
//
// Created by Blake Seely (Air) on 3/24/09.
// Copyright 2009 Apple Inc.. All rights reserved.
//
#import "NSScanner+BSJSONAdditions.h"
#import "NSString+BSJSONAdditions.h"
@implementation NSString (BSJSONAdditions)
+ (NSString *)jsonIndentStringForLevel:(NSInteger)level
{
if (level != jsonDoNotIndent) {
return [@"\n" stringByPaddingToLength:(level + 1) withString:jsonIndentString startingAtIndex:0];
} else {
return [NSString stringWithString: @""];
}
}
- (NSString *)jsonStringValue
{
NSMutableString *jsonString = [[NSMutableString alloc] init];
[jsonString appendString:jsonStringDelimiterString];
// Build the result one character at a time, inserting escaped characters as necessary
NSInteger i;
unichar nextChar;
for (i = 0; i < [self length]; i++) {
nextChar = [self characterAtIndex:i];
switch (nextChar) {
case '\"':
[jsonString appendString:@"\\\""];
break;
case '\\':
[jsonString appendString:@"\\\\"];
break;
case '/':
[jsonString appendString:@"\\/"];
break;
case '\b':
[jsonString appendString:@"\\b"];
break;
case '\f':
[jsonString appendString:@"\\f"];
break;
case '\n':
[jsonString appendString:@"\\n"];
break;
case '\r':
[jsonString appendString:@"\\r"];
break;
case '\t':
[jsonString appendString:@"\\t"];
break;
/* TODO: Find and encode unicode characters here?
case '\u':
[jsonString appendString:@"\\n"];
break;
*/
default:
[jsonString appendFormat:@"%c", nextChar];
break;
}
}
[jsonString appendString:jsonStringDelimiterString];
return [jsonString autorelease];
}
@end