Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

method to print JSON messages as JSON instead of NSDictionary. #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions AFNetworkActivityLogger/AFNetworkActivityLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
22 changes: 22 additions & 0 deletions AFNetworkActivityLogger/AFNetworkActivityLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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