Skip to content

Commit 0a11e8a

Browse files
committed
1 parent c43a6f0 commit 0a11e8a

11 files changed

Lines changed: 135 additions & 20 deletions

File tree

‎JSONModel/JSONModel/JSONModel.m‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ +(void)load
7070

7171
// This is quite strange, but I found the test isSubclassOfClass: (line ~291) to fail if using [JSONModel class].
7272
// somewhat related: https://stackoverflow.com/questions/6524165/nsclassfromstring-vs-classnamednsstring
73-
// //NSClassFromString(@"JSONModel"); seems to break the unit tests
74-
if (!JSONModelClass) {
75-
JSONModelClass = [JSONModel class];
76-
}
73+
// //; seems to break the unit tests
74+
JSONModelClass = [JSONModel class];
7775
}
7876
});
7977
}
@@ -316,7 +314,8 @@ -(id)initWithDictionary:(NSDictionary*)dict error:(NSError**)err
316314

317315

318316
// 1) check if property is itself a JSONModel
319-
if ([[property.type class] isSubclassOfClass:JSONModelClass]) {
317+
318+
if ([self __isJSONModelSubClass:property.type]) {
320319

321320
//initialize the property's model, store it
322321
JSONModelError* initErr = nil;
@@ -446,6 +445,17 @@ -(id)initWithDictionary:(NSDictionary*)dict error:(NSError**)err
446445
}
447446

448447
#pragma mark - property inspection methods
448+
449+
-(BOOL)__isJSONModelSubClass:(Class)class
450+
{
451+
// http://stackoverflow.com/questions/19883472/objc-nsobject-issubclassofclass-gives-incorrect-failure
452+
#ifdef UNIT_TESTING
453+
return [@"JSONModel" isEqualToString: NSStringFromClass([class superclass])];
454+
#else
455+
return [class isSubclassOfClass:JSONModelClass];
456+
#endif
457+
}
458+
449459
//returns a set of the required keys for the model
450460
-(NSMutableSet*)__requiredPropertyNames
451461
{
@@ -654,7 +664,7 @@ -(id)__transform:(id)value forProperty:(JSONModelClassProperty*)property error:(
654664
}
655665

656666
//if the protocol is actually a JSONModel class
657-
if ([[protocolClass class] isSubclassOfClass:JSONModelClass]) {
667+
if ([self __isJSONModelSubClass:protocolClass]) {
658668

659669
//check if it's a list of models
660670
if ([property.type isSubclassOfClass:[NSArray class]]) {
@@ -733,7 +743,7 @@ -(id)__reverseTransform:(id)value forProperty:(JSONModelClassProperty*)property
733743
if (!protocolClass) return value;
734744

735745
//if the protocol is actually a JSONModel class
736-
if ([[protocolClass class] isSubclassOfClass:JSONModelClass]) {
746+
if ([self __isJSONModelSubClass:protocolClass]) {
737747

738748
//check if should export list of dictionaries
739749
if (property.type == [NSArray class] || property.type == [NSMutableArray class]) {

‎JSONModel/JSONModel/JSONModelArray.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* of each of the objects stored in the array, it'll be converted to the target model class.
2828
* Thus saving time upon the very first model creation.
2929
*/
30-
@interface JSONModelArray : NSArray
30+
@interface JSONModelArray : NSMutableArray
3131

3232
/**
3333
* Don't make instances of JSONModelArray yourself, except you know what you are doing.

‎JSONModel/JSONModel/JSONModelArray.m‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ -(id)modelWithIndexValue:(id)indexValue
7171
return nil;
7272
}
7373

74+
-(id)mutableCopy
75+
{
76+
//it's already mutable
77+
return self;
78+
}
79+
7480
#pragma mark - description
7581
-(NSString*)description
7682
{

‎JSONModel/JSONModelLib.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@
3535
#import "JSONModelArray.h"
3636

3737
#ifdef COREDATA_EXTERN
38-
#import "JSONModel+CoreData.h"
38+
#ifndef UNIT_TESTING
39+
#import "JSONModel+CoreData.h"
40+
#endif
3941
#endif

‎JSONModel/JSONModelTransformations/JSONValueTransformer.m‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// The MIT License in plain English: http://www.touch-code-magazine.com/JSONModel/MITLicense
1616

1717
#import "JSONValueTransformer.h"
18+
#import "JSONModelArray.h"
1819

1920
#pragma mark - functions
2021
extern BOOL isNull(id value)
@@ -79,6 +80,11 @@ -(NSMutableString*)NSMutableStringFromNSString:(NSString*)string
7980
#pragma mark - NSMutableArray <-> NSArray
8081
-(NSMutableArray*)NSMutableArrayFromNSArray:(NSArray*)array
8182
{
83+
if ([array isKindOfClass:[JSONModelArray class]]) {
84+
//it's a jsonmodelarray already, just return it
85+
return (id)array;
86+
}
87+
8288
return [NSMutableArray arrayWithArray:array];
8389
}
8490

@@ -109,24 +115,26 @@ -(id)JSONObjectFromNSMutableSet:(NSMutableSet*)set
109115
return [set allObjects];
110116
}
111117

118+
//
119+
// 0 converts to NO, everything else converts to YES
120+
//
112121

113122
#pragma mark - BOOL <-> number/string
114123
-(NSNumber*)BOOLFromNSNumber:(NSNumber*)number
115124
{
116-
if (isNull(number)) return @0;
117-
return number;
125+
if (isNull(number)) return [NSNumber numberWithBool:NO];
126+
return [NSNumber numberWithBool: number.intValue==0?NO:YES];
118127
}
119128

120129
-(NSNumber*)BOOLFromNSString:(NSString*)string
121130
{
122131
int val = [string intValue];
123-
if (val!=0) val=1;
124-
return @(val);
132+
return [NSNumber numberWithBool: val==0?NO:YES];
125133
}
126134

127135
-(NSNumber*)JSONObjectFromBOOL:(NSNumber*)number
128136
{
129-
return number;
137+
return [NSNumber numberWithBool: number.intValue==0?NO:YES];
130138
}
131139

132140
#pragma mark - string/number <-> float

‎JSONModelDemoTests/UnitTests/ArrayTests.m‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ -(void)setUp
3535
-(void)testLoading
3636
{
3737
STAssertTrue([repos.repositories isMemberOfClass:[JSONModelArray class]], @".properties is not a JSONModelArray");
38-
STAssertTrue([repos.repositories[0] isMemberOfClass:[GitHubRepoModel class]], @".properties[0] is not a GitHubRepoModel");
38+
STAssertTrue([[[repos.repositories[0] class] description] isEqualToString:@"GitHubRepoModel"], @".properties[0] is not a GitHubRepoModel");
3939
}
4040

4141
/*

‎JSONModelDemoTests/UnitTests/BuiltInConversionsTests.m‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ -(void)testConversions
4444
STAssertTrue([b.dynamicUnorderedList isKindOfClass:[NSMutableSet class]], @"dynamicUnorderedList is not an NSMutableSet object");
4545
STAssertTrue([b.dynamicUnorderedList anyObject], @"dynamicUnorderedList don't have any objects");
4646

47-
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
48-
int nrOfObjects = [b.dynamicUnorderedList allObjects].count;
49-
#else
5047
NSUInteger nrOfObjects = [b.dynamicUnorderedList allObjects].count;
51-
#endif
5248

5349
[b.dynamicUnorderedList addObject:@"ADDED"];
5450
STAssertTrue(nrOfObjects + 1 == [b.dynamicUnorderedList allObjects].count, @"dynamicUnorderedList didn't add an object");

‎JSONModelDemoTests/UnitTests/PrimitiveTypesReadTests.m‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ -(void)testPrimitiveTypes
4949
STAssertTrue(p.boolYES==YES, @"boolYES read fail");
5050
}
5151

52+
-(void)testBoolExport
53+
{
54+
NSString* exportedJSON = [p toJSONString];
55+
STAssertTrue([exportedJSON rangeOfString:@"\"boolNO\":false"].location != NSNotFound, @"boolNO should export to 'false'");
56+
STAssertTrue([exportedJSON rangeOfString:@"\"boolYES\":true"].location != NSNotFound, @"boolYES should export to 'true'");
57+
}
58+
5259
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
5360
-(void)testEnumerationTypes
5461
{

‎JSONModelDemo_iOS.xcodeproj/project.pbxproj‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
697852FF17D93547006BFCD0 /* nestedDataWithTypeMismatchOnImagesObject.json in Resources */ = {isa = PBXBuildFile; fileRef = 697852FE17D93546006BFCD0 /* nestedDataWithTypeMismatchOnImagesObject.json */; };
1414
9C0D0240166E6BBF001EA645 /* KivaViewControllerNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = 9C0D023E166E6BBF001EA645 /* KivaViewControllerNetworking.m */; };
1515
9C0D0241166E6BBF001EA645 /* KivaViewControllerNetworking.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9C0D023F166E6BBF001EA645 /* KivaViewControllerNetworking.xib */; };
16+
9C28A14118B2A4D2002AEC1E /* CoreData.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9C55AF0618903127004EBD8A /* CoreData.framework */; };
1617
9C55AF0718903127004EBD8A /* CoreData.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9C55AF0618903127004EBD8A /* CoreData.framework */; };
1718
9C55AF0C189031A8004EBD8A /* CoreDataViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9C55AF0A189031A8004EBD8A /* CoreDataViewController.m */; };
1819
9C55AF0D189031A8004EBD8A /* CoreDataViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9C55AF0B189031A8004EBD8A /* CoreDataViewController.xib */; };
@@ -356,6 +357,7 @@
356357
isa = PBXFrameworksBuildPhase;
357358
buildActionMask = 2147483647;
358359
files = (
360+
9C28A14118B2A4D2002AEC1E /* CoreData.framework in Frameworks */,
359361
9CA6B10016FCA5B400B3E78E /* SystemConfiguration.framework in Frameworks */,
360362
9CBBBF65166B9E3E008B4326 /* CoreGraphics.framework in Frameworks */,
361363
9CBBBF07166B6CF0008B4326 /* SenTestingKit.framework in Frameworks */,
@@ -1141,6 +1143,11 @@
11411143
);
11421144
GCC_PRECOMPILE_PREFIX_HEADER = YES;
11431145
GCC_PREFIX_HEADER = "JSONModelDemo_iOS/JSONModelDemo_iOS-Prefix.pch";
1146+
GCC_PREPROCESSOR_DEFINITIONS = (
1147+
"DEBUG=1",
1148+
"$(inherited)",
1149+
"UNIT_TESTING=1",
1150+
);
11441151
INFOPLIST_FILE = "JSONModelDemoTests/JSONModelDemo_iOSTests-Info.plist";
11451152
PRODUCT_NAME = JSONModelDemo_iOSTests;
11461153
TEST_HOST = "$(BUNDLE_LOADER)";

‎JSONModelDemo_iOS/CoreDataViewController.m‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)
174174

175175
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
176176
{
177-
return [NSString stringWithFormat:@"%i entities", self.entities.count];
177+
return [NSString stringWithFormat:@"%lu entities", (unsigned long)self.entities.count];
178178
}
179179

180180
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

0 commit comments

Comments
 (0)