I like JSONModel library and its concept. While just looking on the implementation met with some doubts. in the initialiser
-(instancetype)initWithData:(NSData *)data error:(NSError *__autoreleasing *)err
the parameter data is converted to string and passing to next initialiser
-(id)initWithString:(NSString *)string usingEncoding:(NSStringEncoding)encoding error:(JSONModelError**)err
in this method its converting back to NSData
for using with NSJSONSerialization
Why didn't do something like this..
-(instancetype)initWithData:(NSData *)data error:(NSError *__autoreleasing *)err
{
//not converting nsdata to an nsstring -- using it directly
if (!data.length) return nil;
//read the json
JSONModelError* initError = nil;
id obj = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&initError];
if (initError) {
if (err) *err = [JSONModelError errorBadJSON];
return nil;
}
//init with dictionary
id objModel = [self initWithDictionary:obj error:&initError];
if (initError && err) *err = initError;
return objModel;
}
-(id)initWithString:(NSString *)string usingEncoding:(NSStringEncoding)encoding error:(JSONModelError**)err
{
//check for nil input
if (!string) {
if (err) *err = [JSONModelError errorInputIsNil];
return nil;
}
//create an instance
JSONModelError* initError = nil;
id objModel = [self initWithData:[string dataUsingEncoding:encoding] error:&initError];
if (initError && err) *err = initError;
return objModel;
}
Does this make sense?, shall we avoid two conversions?
This has been resolved thanks to @Johnykutty :)
The relevant commit is here: https://github.com/icanzilb/JSONModel/commit/02619d9d3bad92bc0ea6b75a45cc69d16b5e0801
Now running any of the initializers will avoid converting data types back-and-forth:
-initWithString:error:
-initWithString:usingEncoding:error:
-initWithDictionary:error:
-initWithData:error: