Skip to content

Commit ceb87c4

Browse files
committed
Merge pull request jsonmodel#276 from pmanna/master
New accessor for JSONModel & static date formatter
2 parents 07d9ff6 + bd0b8c2 commit ceb87c4

File tree

3 files changed

+54
-28
lines changed

3 files changed

+54
-28
lines changed

JSONModel/JSONModel/JSONModel.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
186186
*/
187187
-(NSString*)toJSONString;
188188

189+
/**
190+
* Export the whole object to a JSON data text string
191+
* @return JSON text data describing the data model
192+
*/
193+
-(NSData*)toJSONData;
194+
189195
/**
190196
* Export the specified properties of the object to a dictionary
191197
* @param propertyNames the properties to export; if nil, all properties exported
@@ -200,6 +206,13 @@ lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
200206
*/
201207
-(NSString*)toJSONStringWithKeys:(NSArray*)propertyNames;
202208

209+
/**
210+
* Export the specified properties of the object to a JSON data text string
211+
* @param propertyNames the properties to export; if nil, all properties exported
212+
* @return JSON text data describing the data model
213+
*/
214+
-(NSData*)toJSONDataWithKeys:(NSArray*)propertyNames;
215+
203216
/** @name Batch methods */
204217

205218
/**

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
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ extern BOOL isNull(id value)
2626
return NO;
2727
}
2828

29-
static NSDateFormatter *_dateFormatter;
30-
3129
@implementation JSONValueTransformer
3230

3331
-(id)init
@@ -213,14 +211,14 @@ -(NSString*)JSONObjectFromNSURL:(NSURL*)url
213211
#pragma mark - string <-> date
214212
-(NSDateFormatter*)importDateFormatter
215213
{
216-
static dispatch_once_t once;
217-
static NSDateFormatter* dateFormatter;
218-
dispatch_once(&once, ^{
219-
dateFormatter = [[NSDateFormatter alloc] init];
220-
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
221-
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HHmmssZZZ"];
214+
static dispatch_once_t onceInput;
215+
static NSDateFormatter* inputDateFormatter;
216+
dispatch_once(&onceInput, ^{
217+
inputDateFormatter = [[NSDateFormatter alloc] init];
218+
[inputDateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
219+
[inputDateFormatter setDateFormat:@"yyyy-MM-dd'T'HHmmssZZZ"];
222220
});
223-
return dateFormatter;
221+
return inputDateFormatter;
224222
}
225223

226224
-(NSDate*)__NSDateFromNSString:(NSString*)string
@@ -231,10 +229,14 @@ -(NSDate*)__NSDateFromNSString:(NSString*)string
231229

232230
-(NSString*)__JSONObjectFromNSDate:(NSDate*)date
233231
{
234-
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
235-
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
236-
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
237-
return [dateFormatter stringFromDate:date];
232+
static dispatch_once_t onceOutput;
233+
static NSDateFormatter *outputDateFormatter;
234+
dispatch_once(&onceOutput, ^{
235+
outputDateFormatter = [[NSDateFormatter alloc] init];
236+
[outputDateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
237+
[outputDateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
238+
});
239+
return [outputDateFormatter stringFromDate:date];
238240
}
239241

240242
#pragma mark - number <-> date

0 commit comments

Comments
 (0)