Browse files

Added -[RESTBody prettyStringWithJSONObject:]

Change-Id: I595a55e9b467fb5367cfe300fa4f8c3b50d0fdc9
  • Loading branch information...
1 parent 90e5c49 commit 640539b2d6a2e542dce2af9eaaee39ae5d030dee @snej snej committed Jan 26, 2012
Showing with 17 additions and 0 deletions.
  1. +3 −0 REST/RESTBody.h
  2. +14 −0 REST/RESTBody.m
View
3 REST/RESTBody.h
@@ -86,6 +86,9 @@
/** Converts an object to a JSON string.
JSON 'fragments' (NSString / NSNumber) are allowed. Returns nil on nil input. */
+ (NSString*) stringWithJSONObject: (id)obj;
+/** Converts an object to a pretty-printed JSON string.
+ JSON 'fragments' (NSString / NSNumber) are allowed. Returns nil on nil input. */
++ (NSString*) prettyStringWithJSONObject: (id)obj;
/** Parses JSON data into a Foundation object tree.
If parsing fails, returns nil. */
+ (id) JSONObjectWithData: (NSData*)data;
View
14 REST/RESTBody.m
@@ -279,6 +279,20 @@ + (NSString*) stringWithJSONObject: (id)obj {
return [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
}
++ (NSString*) prettyStringWithJSONObject: (id)obj {
+#if USE_JSONKIT
+ if (!sJSONSerialization)
+ return [obj JSONStringWithOptions: JKSerializeOptionPretty error: nil];
+#endif
+ NSData* data = [sJSONSerialization dataWithJSONObject: obj
+ options: NSJSONReadingAllowFragments
+ | NSJSONWritingPrettyPrinted
+ error: NULL];
+ if (!data)
+ return nil;
+ return [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
+}
+
+ (id) JSONObjectWithData: (NSData*)data {
#if USE_JSONKIT

0 comments on commit 640539b

Please sign in to comment.