Skip to content

Commit 8d0cb58

Browse files
committed
This commit includes the "Serialize a single NSString" feature/pull request from issue johnezang#4 and issue johnezang#11. Also replaces jk_encode() function with the JKSerializer class. This is a forward looking change as placing the state in to an autoreleased object that does serializing means that we can always make sure that any resources we might have allocated will get released, no matter what. This could be a problem, for example, if something throws an exception while we are serializing.
1 parent 2cf72ad commit 8d0cb58

3 files changed

Lines changed: 179 additions & 84 deletions

File tree

JSONKit.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ typedef struct JKParseState JKParseState; // Opaque internal, private type.
153153

154154
@end
155155

156-
@interface NSString (JSONKit)
156+
////////////
157+
#pragma mark Deserializing methods
158+
////////////
159+
160+
@interface NSString (JSONKitDeserializing)
157161
- (id)objectFromJSONString;
158162
- (id)objectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
159163
- (id)objectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;
@@ -162,7 +166,7 @@ typedef struct JKParseState JKParseState; // Opaque internal, private type.
162166
- (id)mutableObjectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;
163167
@end
164168

165-
@interface NSData (JSONKit)
169+
@interface NSData (JSONKitDeserializing)
166170
// The NSData MUST be UTF8 encoded JSON.
167171
- (id)objectFromJSONData;
168172
- (id)objectFromJSONDataWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
@@ -172,14 +176,29 @@ typedef struct JKParseState JKParseState; // Opaque internal, private type.
172176
- (id)mutableObjectFromJSONDataWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;
173177
@end
174178

175-
@interface NSArray (JSONKit)
179+
////////////
180+
#pragma mark Serializing methods
181+
////////////
182+
183+
@interface NSString (JSONKitSerializing)
184+
// Convenience methods for those that need to serialize the receiving NSString (i.e., instead of having to serialize a NSArray with a single NSString, you can "serialize to JSON" just the NSString).
185+
// Normally, a string that is serialized to JSON has quotation marks surrounding it, which you may or may not want when serializing a single string, and can be controlled with includeQuotes:
186+
// includeQuotes:YES `a "test"...` -> `"a \"test\"..."`
187+
// includeQuotes:NO `a "test"...` -> `a \"test\"...`
188+
- (NSData *)JSONData; // Invokes JSONDataWithOptions:JKSerializeOptionNone includeQuotes:YES
189+
- (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions includeQuotes:(BOOL)includeQuotes error:(NSError **)error;
190+
- (NSString *)JSONString; // Invokes JSONStringWithOptions:JKSerializeOptionNone includeQuotes:YES
191+
- (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions includeQuotes:(BOOL)includeQuotes error:(NSError **)error;
192+
@end
193+
194+
@interface NSArray (JSONKitSerializing)
176195
- (NSData *)JSONData;
177196
- (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions error:(NSError **)error;
178197
- (NSString *)JSONString;
179198
- (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions error:(NSError **)error;
180199
@end
181200

182-
@interface NSDictionary (JSONKit)
201+
@interface NSDictionary (JSONKitSerializing)
183202
- (NSData *)JSONData;
184203
- (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions error:(NSError **)error;
185204
- (NSString *)JSONString;

0 commit comments

Comments
 (0)