- Added toJSONData accessor to JSONModel · sofili/JSONModel@bd0b8c2 · GitHub
Skip to content

Commit bd0b8c2

Browse files
author
Paolo Manna
committed
- Added toJSONData accessor to JSONModel
- Made date formatter static to avoid recreating it each time
1 parent e7386f0 commit bd0b8c2

3 files changed

Lines changed: 54 additions & 28 deletions

File tree

JSONModel/JSONModel/JSONModel.h

Lines changed: 13 additions & 0 deletions

JSONModel/JSONModel/JSONModel.m

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,11 @@ -(NSString*)toJSONString
928928
return [self toJSONStringWithKeys:nil];
929929
}
930930

931+
-(NSData*)toJSONData
932+
{
933+
return [self toJSONDataWithKeys:nil];
934+
}
935+
931936
//exports the model as a dictionary of JSON compliant objects
932937
-(NSDictionary*)toDictionaryWithKeys:(NSArray*)propertyNames
933938
{
@@ -1049,23 +1054,29 @@ -(NSDictionary*)toDictionaryWithKeys:(NSArray*)propertyNames
10491054
}
10501055

10511056
//exports model to a dictionary and then to a JSON string
1057+
-(NSData*)toJSONDataWithKeys:(NSArray*)propertyNames
1058+
{
1059+
NSData* jsonData = nil;
1060+
NSError* jsonError = nil;
1061+
1062+
@try {
1063+
NSDictionary* dict = [self toDictionaryWithKeys:propertyNames];
1064+
jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&jsonError];
1065+
}
1066+
@catch (NSException *exception) {
1067+
//this should not happen in properly design JSONModel
1068+
//usually means there was no reverse transformer for a custom property
1069+
JMLog(@"EXCEPTION: %@", exception.description);
1070+
return nil;
1071+
}
1072+
1073+
return jsonData;
1074+
}
1075+
10521076
-(NSString*)toJSONStringWithKeys:(NSArray*)propertyNames
10531077
{
1054-
NSData* jsonData = nil;
1055-
NSError* jsonError = nil;
1056-
1057-
@try {
1058-
NSDictionary* dict = [self toDictionaryWithKeys:propertyNames];
1059-
jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&jsonError];
1060-
}
1061-
@catch (NSException *exception) {
1062-
//this should not happen in properly design JSONModel
1063-
//usually means there was no reverse transformer for a custom property
1064-
JMLog(@"EXCEPTION: %@", exception.description);
1065-
return nil;
1066-
}
1067-
1068-
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
1078+
return [[NSString alloc] initWithData: [self toJSONDataWithKeys: propertyNames]
1079+
encoding: NSUTF8StringEncoding];
10691080
}
10701081

10711082
#pragma mark - import/export of lists

JSONModel/JSONModelTransformations/JSONValueTransformer.m

Lines changed: 15 additions & 13 deletions

0 commit comments

Comments
 (0)