changed git call from https to git readonly
[atutor.git] / mods / atsocial_iphone_app / TouchJSON / JSON / CJSONSerializer.m
1 //
2 //  CJSONSerializer.m
3 //  TouchJSON
4 //
5 //  Created by Jonathan Wight on 12/07/2005.
6 //  Copyright 2005 Toxic Software. All rights reserved.
7 //
8
9 #import "CJSONSerializer.h"
10
11 @implementation CJSONSerializer
12
13 + (id)serializer
14 {
15 return([[[self alloc] init] autorelease]);
16 }
17
18 - (NSString *)serializeObject:(id)inObject;
19 {
20 NSString *theResult = @"";
21
22 if ([inObject isKindOfClass:[NSNull class]])
23         {
24         theResult = [self serializeNull:inObject];
25         }
26 else if ([inObject isKindOfClass:[NSNumber class]])
27         {
28         theResult = [self serializeNumber:inObject];
29         }
30 else if ([inObject isKindOfClass:[NSString class]])
31         {
32         theResult = [self serializeString:inObject];
33         }
34 else if ([inObject isKindOfClass:[NSArray class]])
35         {
36         theResult = [self serializeArray:inObject];
37         }
38 else if ([inObject isKindOfClass:[NSDictionary class]])
39         {
40         theResult = [self serializeDictionary:inObject];
41         }
42 else if ([inObject isKindOfClass:[NSData class]])
43         {
44         NSString *theString = [[[NSString alloc] initWithData:inObject encoding:NSUTF8StringEncoding] autorelease];
45         theResult = [self serializeString:theString];
46         }
47 else
48         {
49         [NSException raise:NSGenericException format:@"Cannot serialize data of type '%@'", NSStringFromClass([inObject class])];
50         }
51 if (theResult == NULL)
52         [NSException raise:NSGenericException format:@"Could not serialize object '%@'", inObject];
53 return(theResult);
54 }
55
56 - (NSString *)serializeNull:(NSNull *)inNull
57 {
58 #pragma unused (inNull)
59 return(@"null");
60 }
61
62 - (NSString *)serializeNumber:(NSNumber *)inNumber
63 {
64 NSString *theResult = NULL;
65 switch (CFNumberGetType((CFNumberRef)inNumber))
66         {
67         case kCFNumberCharType:
68                 {
69                 int theValue = [inNumber intValue];
70                 if (theValue == 0)
71                         theResult = @"false";
72                 else if (theValue == 1)
73                         theResult = @"true";
74                 else
75                         theResult = [inNumber stringValue];
76                 }
77                 break;
78         case kCFNumberSInt8Type:
79         case kCFNumberSInt16Type:
80         case kCFNumberSInt32Type:
81         case kCFNumberSInt64Type:
82         case kCFNumberFloat32Type:
83         case kCFNumberFloat64Type:
84         case kCFNumberShortType:
85         case kCFNumberIntType:
86         case kCFNumberLongType:
87         case kCFNumberLongLongType:
88         case kCFNumberFloatType:
89         case kCFNumberDoubleType:
90         case kCFNumberCFIndexType:
91         default:
92                 theResult = [inNumber stringValue];
93                 break;
94         }
95 return(theResult);
96 }
97
98 - (NSString *)serializeString:(NSString *)inString
99 {
100 NSMutableString *theMutableCopy = [[inString mutableCopy] autorelease];
101 [theMutableCopy replaceOccurrencesOfString:@"\\" withString:@"\\\\" options:0 range:NSMakeRange(0, [theMutableCopy length])];
102 [theMutableCopy replaceOccurrencesOfString:@"\"" withString:@"\\\"" options:0 range:NSMakeRange(0, [theMutableCopy length])];
103 [theMutableCopy replaceOccurrencesOfString:@"/" withString:@"\\/" options:0 range:NSMakeRange(0, [theMutableCopy length])];
104 [theMutableCopy replaceOccurrencesOfString:@"\b" withString:@"\\b" options:0 range:NSMakeRange(0, [theMutableCopy length])];
105 [theMutableCopy replaceOccurrencesOfString:@"\f" withString:@"\\f" options:0 range:NSMakeRange(0, [theMutableCopy length])];
106 [theMutableCopy replaceOccurrencesOfString:@"\n" withString:@"\\n" options:0 range:NSMakeRange(0, [theMutableCopy length])];
107 [theMutableCopy replaceOccurrencesOfString:@"\n" withString:@"\\n" options:0 range:NSMakeRange(0, [theMutableCopy length])];
108 [theMutableCopy replaceOccurrencesOfString:@"\t" withString:@"\\t" options:0 range:NSMakeRange(0, [theMutableCopy length])];
109 /*
110                         case 'u':
111                                 {
112                                 theCharacter = 0;
113
114                                 int theShift;
115                                 for (theShift = 12; theShift >= 0; theShift -= 4)
116                                         {
117                                         int theDigit = HexToInt([self scanCharacter]);
118                                         if (theDigit == -1)
119                                                 {
120                                                 [self setScanLocation:theScanLocation];
121                                                 return(NO);
122                                                 }
123                                         theCharacter |= (theDigit << theShift);
124                                         }
125                                 }
126 */
127 return([NSString stringWithFormat:@"\"%@\"", theMutableCopy]);
128 }
129
130 - (NSString *)serializeArray:(NSArray *)inArray
131 {
132 NSMutableString *theString = [NSMutableString string];
133
134 NSEnumerator *theEnumerator = [inArray objectEnumerator];
135 id theValue = NULL;
136 while ((theValue = [theEnumerator nextObject]) != NULL)
137         {
138         [theString appendString:[self serializeObject:theValue]];
139         if (theValue != [inArray lastObject])
140                 [theString appendString:@","];
141         }
142 return([NSString stringWithFormat:@"[%@]", theString]);
143 }
144
145 - (NSString *)serializeDictionary:(NSDictionary *)inDictionary
146 {
147 NSMutableString *theString = [NSMutableString string];
148
149 NSArray *theKeys = [inDictionary allKeys];
150 NSEnumerator *theEnumerator = [theKeys objectEnumerator];
151 NSString *theKey = NULL;
152 while ((theKey = [theEnumerator nextObject]) != NULL)
153         {
154         id theValue = [inDictionary valueForKey:theKey];
155         
156         [theString appendFormat:@"%@:%@", [self serializeString:theKey], [self serializeObject:theValue]];
157         if (theKey != [theKeys lastObject])
158                 [theString appendString:@","];
159         }
160 return([NSString stringWithFormat:@"{%@}", theString]);
161 }
162
163 @end