diff --git a/AFNetworkActivityLogger/AFNetworkActivityLogger.h b/AFNetworkActivityLogger/AFNetworkActivityLogger.h index 2d786cd..d08c213 100644 --- a/AFNetworkActivityLogger/AFNetworkActivityLogger.h +++ b/AFNetworkActivityLogger/AFNetworkActivityLogger.h @@ -47,6 +47,11 @@ typedef NS_ENUM(NSUInteger, AFHTTPRequestLoggerLevel) { */ @property (nonatomic, assign) AFHTTPRequestLoggerLevel level; +/** + Enable JSON description on response values when having a JSON message. + */ +@property (assign) BOOL prettyJsonDescription; + /** Omit requests which match the specified predicate, if provided. `nil` by default. diff --git a/AFNetworkActivityLogger/AFNetworkActivityLogger.m b/AFNetworkActivityLogger/AFNetworkActivityLogger.m index af12034..478c02a 100644 --- a/AFNetworkActivityLogger/AFNetworkActivityLogger.m +++ b/AFNetworkActivityLogger/AFNetworkActivityLogger.m @@ -161,6 +161,10 @@ - (void)networkRequestDidFinish:(NSNotification *)notification { } NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:objc_getAssociatedObject(notification.object, AFNetworkRequestStartDate)]; + + if (self.prettyJsonDescription) { + responseObject = [self jsonDescriptionForResponseObject:responseObject]; + } if (error) { switch (self.level) { @@ -186,4 +190,22 @@ - (void)networkRequestDidFinish:(NSNotification *)notification { } } +- (NSString *)jsonDescriptionForResponseObject:(id)responseObject { + NSError * err; + NSData *jsonData; + + if ([responseObject isKindOfClass:[NSDictionary class]]) { + jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:&err]; + + if (err) { + return responseObject; + } else { + return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; + } + } else { + return responseObject; + } +} + + @end