Added JSON and text extensions to GCDWebServerDataRequest

This commit is contained in:
Pierre-Olivier Latour
2014-04-08 17:49:17 -07:00
parent e26c9b76ea
commit 881cc3b00c
3 changed files with 38 additions and 1 deletions
+32
View File
@@ -30,6 +30,9 @@
@interface GCDWebServerDataRequest () {
@private
NSMutableData* _data;
NSString* _text;
id _jsonObject;
}
@end
@@ -39,6 +42,8 @@
- (void)dealloc {
ARC_RELEASE(_data);
ARC_RELEASE(_text);
ARC_RELEASE(_jsonObject);
ARC_DEALLOC(super);
}
@@ -69,3 +74,30 @@
}
@end
@implementation GCDWebServerDataRequest (Extensions)
- (NSString*)text {
if (_text == nil) {
if ([self.contentType hasPrefix:@"text/"]) {
NSString* charset = GCDWebServerExtractHeaderParameter(self.contentType, @"charset");
_text = [[NSString alloc] initWithData:self.data encoding:GCDWebServerStringEncodingFromCharset(charset)];
} else {
DNOT_REACHED();
}
}
return _text;
}
- (id)jsonObject {
if (_jsonObject == nil) {
if ([self.contentType isEqualToString:@"application/json"] || [self.contentType isEqualToString:@"text/json"] || [self.contentType isEqualToString:@"text/javascript"]) {
_jsonObject = ARC_RETAIN([NSJSONSerialization JSONObjectWithData:_data options:0 error:NULL]);
} else {
DNOT_REACHED();
}
}
return _jsonObject;
}
@end