Skip to content

Commit

Permalink
* handle "description" property
Browse files Browse the repository at this point in the history
* unit test
  • Loading branch information
icanzilb committed Apr 18, 2014
1 parent ec19f80 commit 2cdc5e4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
6 changes: 5 additions & 1 deletion JSONModel/JSONModel/JSONModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@

#pragma mark - JSONModel implementation
@implementation JSONModel
{
NSString* _description;
}

#pragma mark - initialization methods

Expand Down Expand Up @@ -1193,7 +1196,8 @@ -(NSString*)description
NSMutableString* text = [NSMutableString stringWithFormat:@"<%@> \n", [self class]];

for (JSONModelClassProperty *p in [self __properties__]) {
id value = [self valueForKey:p.name];

id value = ([p.name isEqualToString:@"description"])?self->_description:[self valueForKey:p.name];
NSString* valueDescription = (value)?[value description]:@"<nil>";

if (p.isStandardJSONType && ![value respondsToSelector:@selector(count)] && [valueDescription length]>60 && !p.convertsOnDemand) {
Expand Down
2 changes: 1 addition & 1 deletion JSONModel/JSONModel/JSONModelClassProperty.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ @implementation JSONModelClassProperty
-(NSString*)description
{
//build the properties string for the current class property
NSMutableArray* properties = [NSMutableArray arrayWithCapacity:4];
NSMutableArray* properties = [NSMutableArray arrayWithCapacity:8];

if (self.isIndex) [properties addObject:@"Index"];
if (self.isOptional) [properties addObject:@"Optional"];
Expand Down
22 changes: 21 additions & 1 deletion JSONModelDemoTests/UnitTests/SpecialPropertyNameTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
#import "SpecialPropertyNameTests.h"
#import "SpecialPropertyModel.h"

@interface DescModel : JSONModel
@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* description;
@end

@implementation DescModel
@end

@implementation SpecialPropertyNameTests

- (void)testSpecialPropertyName
Expand All @@ -20,8 +28,20 @@ - (void)testSpecialPropertyName

NSError* err;
SpecialPropertyModel *p = [[SpecialPropertyModel alloc] initWithString: jsonContents error:&err];
JMLog(@"%@", p);

XCTAssertNotNil(p, @"Could not initialize model.");
XCTAssertNil(err, "%@", [err localizedDescription]);
}

-(void)testDescriptionProperty
{
NSString* json = @"{\"id\":10, \"description\":\"Marin\"}";
DescModel* dm = [[DescModel alloc] initWithString:json error:nil];

XCTAssertNotNil(dm, @"Could not initialize model.");
XCTAssertEqualObjects(dm.description, @"Marin", @"could not initialize description proeprty");
NSDictionary* dict = dm.toDictionary;
XCTAssertEqualObjects(dict[@"description"], @"Marin", @"could not export description proeprty");
}

@end
10 changes: 8 additions & 2 deletions JSONModelDemo_iOS/MasterViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,28 @@ @interface TopModel : JSONModel
@property (assign, nonatomic, readonly) int rId;
@property (nonatomic, copy) void(^userLocationCompleted)();
@property (strong, nonatomic) NSDictionary* dict;
@property (strong, nonatomic) NSString* description;
@end

@implementation TopModel
+(BOOL)propertyIsIgnored:(NSString *)propertyName
{
return NO;
}
-(NSString*)getText
{
return @"1123";
}
@end

@implementation MasterViewController

-(void)viewDidAppear:(BOOL)animated
{
NSString* json = @"{\"id\":1, \"answer\": {\"name1\":\"marin\"}, \"dict\":[]}";
NSString* json = @"{\"id\":1, \"answer\": {\"name1\":\"marin\"}, \"dict\":[], \"description\":\"Marin\"}";
TopModel* tm = [[TopModel alloc] initWithString:json error:nil];
NSLog(@"tm: %@", tm);
NSLog(@"tm: %@", tm.toDictionary);
NSLog(@"to string: %@", tm.toJSONString);
}

-(IBAction)actionLoadCall:(id)sender
Expand Down

0 comments on commit 2cdc5e4

Please sign in to comment.