Skip to content

Commit 2673637

Browse files
committed
* auto-ignore block properties
* auto-ignore readonly properties via property attributes (speed gain) * unit tests for auto-ignored properties
1 parent 12a67c4 commit 2673637

5 files changed

Lines changed: 90 additions & 20 deletions

File tree

JSONModel/JSONModel/JSONModel.m

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,14 @@ -(void)__inspectProperties
553553
//get property attributes
554554
const char *attrs = property_getAttributes(property);
555555
NSString* propertyAttributes = @(attrs);
556+
NSArray* attributeItems = [propertyAttributes componentsSeparatedByString:@","];
556557

558+
//ignore read-only properties
559+
if ([attributeItems containsObject:@"R"]) {
560+
continue; //to next property
561+
}
562+
563+
//check for 64b BOOLs
557564
if ([propertyAttributes hasPrefix:@"Tc,"]) {
558565
//mask BOOLs as structs so they can have custom convertors
559566
p.structName = @"BOOL";
@@ -597,8 +604,6 @@ -(void)__inspectProperties
597604
p.convertsOnDemand = YES;
598605
} else if([protocolName isEqualToString:@"Ignore"]) {
599606
p = nil;
600-
} else if ([self propertyIsReadOnly:p.name]) {
601-
p = nil;
602607
} else {
603608
p.protocol = protocolName;
604609
}
@@ -645,7 +650,8 @@ -(void)__inspectProperties
645650
p = nil;
646651
}
647652

648-
if([self propertyIsReadOnly:nsPropertyName]) {
653+
//few cases where JSONModel will ignore properties automatically
654+
if ([propertyType isEqualToString:@"Block"]) {
649655
p = nil;
650656
}
651657

@@ -1224,18 +1230,6 @@ +(BOOL)propertyIsIgnored:(NSString *)propertyName
12241230
return NO;
12251231
}
12261232

1227-
-(BOOL)propertyIsReadOnly: (NSString*)key
1228-
{
1229-
NSString *setterString = [NSString stringWithFormat:@"set%@%@:",
1230-
[[key substringToIndex:1] capitalizedString],
1231-
[key substringFromIndex:1]];
1232-
1233-
if ([self respondsToSelector:NSSelectorFromString(setterString)]) {
1234-
return NO;
1235-
}
1236-
return YES;
1237-
}
1238-
12391233
#pragma mark - working with incomplete models
12401234
-(void)mergeFromDictionary:(NSDictionary*)dict useKeyMapping:(BOOL)useKeyMapping
12411235
{
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//
2+
// SpecialPropertiesTests.m
3+
// JSONModelDemo_iOS
4+
//
5+
// Created by Marin Todorov on 4/18/14.
6+
// Copyright (c) 2014 Underplot ltd. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "JSONModel.h"
11+
12+
#pragma mark - model with block property
13+
@interface BModel: JSONModel
14+
@property (assign, nonatomic) int id;
15+
@property (nonatomic, copy) void(^userLocationCompleted)();
16+
@end
17+
18+
@implementation BModel
19+
@end
20+
21+
#pragma mark - model with read-only properties
22+
@interface RModel: JSONModel
23+
@property (assign, nonatomic) int id;
24+
@property (assign, nonatomic, readonly) int rId;
25+
@property (strong, nonatomic, readonly) NSNumber* nId;
26+
@end
27+
28+
@implementation RModel
29+
@end
30+
31+
#pragma mark - test suite
32+
33+
@interface SpecialPropertiesTests : XCTestCase
34+
35+
@end
36+
37+
@implementation SpecialPropertiesTests
38+
39+
- (void)setUp
40+
{
41+
[super setUp];
42+
// Put setup code here. This method is called before the invocation of each test method in the class.
43+
}
44+
45+
- (void)tearDown
46+
{
47+
// Put teardown code here. This method is called after the invocation of each test method in the class.
48+
[super tearDown];
49+
}
50+
51+
//test autoignoring block properties
52+
- (void)testBlocks
53+
{
54+
NSString* json = @"{\"id\":1}";
55+
BModel* bm = [[BModel alloc] initWithString:json error:nil];
56+
XCTAssertNotNil(bm, @"model failed to crate");
57+
}
58+
59+
//test autoignoring read-only properties
60+
- (void)testReadOnly
61+
{
62+
NSString* json = @"{\"id\":1}";
63+
RModel* rm = [[RModel alloc] initWithString:json error:nil];
64+
XCTAssertNotNil(rm, @"model failed to crate");
65+
}
66+
67+
@end
68+

JSONModelDemo_OSX.xcodeproj/project.pbxproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
9CC2FCB9168CE7340059FE67 /* KivaFeed.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CC2FCB4168CE7340059FE67 /* KivaFeed.m */; };
103103
9CC2FCBA168CE7340059FE67 /* LoanModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CC2FCB6168CE7340059FE67 /* LoanModel.m */; };
104104
9CC2FCBB168CE7340059FE67 /* LocationModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CC2FCB8168CE7340059FE67 /* LocationModel.m */; };
105+
9CCAFD941901B7F500314886 /* SpecialPropertiesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CCAFD931901B7F500314886 /* SpecialPropertiesTests.m */; };
105106
9CD22C8618FF32F7003E66AD /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9CBD6D5118FF2FDD00DE66EC /* XCTest.framework */; };
106107
9CD425861702224F00A42AA1 /* HTTPClientSuite.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CD425851702224F00A42AA1 /* HTTPClientSuite.m */; };
107108
9CD4258B170222AF00A42AA1 /* MockNSURLConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CD42588170222AF00A42AA1 /* MockNSURLConnection.m */; };
@@ -254,6 +255,7 @@
254255
9CC2FCB6168CE7340059FE67 /* LoanModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LoanModel.m; sourceTree = "<group>"; };
255256
9CC2FCB7168CE7340059FE67 /* LocationModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LocationModel.h; sourceTree = "<group>"; };
256257
9CC2FCB8168CE7340059FE67 /* LocationModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocationModel.m; sourceTree = "<group>"; };
258+
9CCAFD931901B7F500314886 /* SpecialPropertiesTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpecialPropertiesTests.m; sourceTree = "<group>"; };
257259
9CD425841702224F00A42AA1 /* HTTPClientSuite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTTPClientSuite.h; sourceTree = "<group>"; };
258260
9CD425851702224F00A42AA1 /* HTTPClientSuite.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HTTPClientSuite.m; sourceTree = "<group>"; };
259261
9CD42587170222AF00A42AA1 /* MockNSURLConnection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MockNSURLConnection.h; path = JSONModelDemoTests/MockNSURLConnection.h; sourceTree = SOURCE_ROOT; };
@@ -266,7 +268,7 @@
266268
D5F918E9172ADA3F00AC2C8E /* SpecialPropertyModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SpecialPropertyModel.h; sourceTree = "<group>"; };
267269
D5F918EA172ADA3F00AC2C8E /* SpecialPropertyModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpecialPropertyModel.m; sourceTree = "<group>"; };
268270
D5F918EC172ADAF800AC2C8E /* SpecialPropertyNameTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SpecialPropertyNameTests.h; sourceTree = "<group>"; };
269-
D5F918ED172ADAF800AC2C8E /* SpecialPropertyNameTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpecialPropertyNameTests.m; sourceTree = "<group>"; };
271+
D5F918ED172ADAF800AC2C8E /* SpecialPropertyNameTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpecialPropertyNameTests.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
270272
/* End PBXFileReference section */
271273

272274
/* Begin PBXFrameworksBuildPhase section */
@@ -354,6 +356,7 @@
354356
9C735D66170B717F00FF96F5 /* JSONAPITests.m */,
355357
9C735D71170C048C00FF96F5 /* InitFromWebTests.h */,
356358
9C735D72170C048C00FF96F5 /* InitFromWebTests.m */,
359+
9CCAFD931901B7F500314886 /* SpecialPropertiesTests.m */,
357360
);
358361
path = UnitTests;
359362
sourceTree = "<group>";
@@ -780,6 +783,7 @@
780783
9C66E003168CF09A0015CCDF /* JSONHTTPClient.m in Sources */,
781784
9C66E005168CF09A0015CCDF /* JSONModel+networking.m in Sources */,
782785
9C66E007168CF09A0015CCDF /* JSONKeyMapper.m in Sources */,
786+
9CCAFD941901B7F500314886 /* SpecialPropertiesTests.m in Sources */,
783787
9C66E009168CF09A0015CCDF /* JSONValueTransformer.m in Sources */,
784788
D5F918EB172ADA3F00AC2C8E /* SpecialPropertyModel.m in Sources */,
785789
D5F918EE172ADAF900AC2C8E /* SpecialPropertyNameTests.m in Sources */,

JSONModelDemo_iOS.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
9CBBBFB5166BBB21008B4326 /* StorageViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CBBBFB3166BBB21008B4326 /* StorageViewController.m */; };
123123
9CBBBFB6166BBB21008B4326 /* StorageViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9CBBBFB4166BBB21008B4326 /* StorageViewController.xib */; };
124124
9CC2FD2B168CE7830059FE67 /* JSONModelDemo_iOSTests-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 9CC2FCD5168CE7830059FE67 /* JSONModelDemo_iOSTests-Info.plist */; };
125+
9CCAFD921901B44300314886 /* SpecialPropertiesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CCAFD911901B44300314886 /* SpecialPropertiesTests.m */; };
125126
9CD425751701FE0000A42AA1 /* HTTPClientSuite.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CD425731701FDE500A42AA1 /* HTTPClientSuite.m */; };
126127
9CD425781701FF2100A42AA1 /* MTTestSemaphor.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CD425771701FF2100A42AA1 /* MTTestSemaphor.m */; };
127128
9CD4257B1702002900A42AA1 /* MockNSURLConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CD4257A1702002900A42AA1 /* MockNSURLConnection.m */; };
@@ -296,6 +297,7 @@
296297
9CBD6D4F18FF2D7D00DE66EC /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
297298
9CC2FCD2168CE7830059FE67 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
298299
9CC2FCD5168CE7830059FE67 /* JSONModelDemo_iOSTests-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "JSONModelDemo_iOSTests-Info.plist"; sourceTree = "<group>"; };
300+
9CCAFD911901B44300314886 /* SpecialPropertiesTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpecialPropertiesTests.m; sourceTree = "<group>"; };
299301
9CD425721701FDE500A42AA1 /* HTTPClientSuite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTTPClientSuite.h; sourceTree = "<group>"; };
300302
9CD425731701FDE500A42AA1 /* HTTPClientSuite.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HTTPClientSuite.m; sourceTree = "<group>"; };
301303
9CD425761701FF2100A42AA1 /* MTTestSemaphor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTTestSemaphor.h; sourceTree = "<group>"; };
@@ -444,6 +446,7 @@
444446
9C735D63170B716300FF96F5 /* JSONAPITests.m */,
445447
9C735D6E170C007900FF96F5 /* InitFromWebTests.h */,
446448
9C735D6F170C007900FF96F5 /* InitFromWebTests.m */,
449+
9CCAFD911901B44300314886 /* SpecialPropertiesTests.m */,
447450
);
448451
path = UnitTests;
449452
sourceTree = "<group>";
@@ -973,6 +976,7 @@
973976
9C66E027168CF0AA0015CCDF /* JSONModelArray.m in Sources */,
974977
9C66E029168CF0AA0015CCDF /* JSONModelClassProperty.m in Sources */,
975978
9C66E02B168CF0AA0015CCDF /* JSONModelError.m in Sources */,
979+
9CCAFD921901B44300314886 /* SpecialPropertiesTests.m in Sources */,
976980
9C66E02D168CF0AA0015CCDF /* NSArray+JSONModel.m in Sources */,
977981
9C66E02F168CF0AA0015CCDF /* JSONAPI.m in Sources */,
978982
9C55AF1D1890494E004EBD8A /* GitHubRepoEntity.m in Sources */,

JSONModelDemo_iOS/MasterViewController.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,16 @@ @implementation JSONAnswer
9696
@end
9797

9898
@interface TopModel : JSONModel
99-
//@property (assign, nonatomic) int id;
100-
//@property (strong, nonatomic) JSONAnswer<Optional>* answer;
101-
99+
@property (assign, nonatomic) int id;
100+
@property (strong, nonatomic) JSONAnswer<Optional>* answer;
101+
@property (assign, nonatomic, readonly) int rId;
102102
@property (nonatomic, copy) void(^userLocationCompleted)();
103103
@end
104104

105105
@implementation TopModel
106106
+(BOOL)propertyIsIgnored:(NSString *)propertyName
107107
{
108-
return YES;
108+
return NO;
109109
}
110110
@end
111111

0 commit comments

Comments
 (0)