Skip to content

Commit

Permalink
* handle PHP bug for empty objects
Browse files Browse the repository at this point in the history
  • Loading branch information
icanzilb committed Apr 18, 2014
1 parent 2673637 commit ec19f80
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
14 changes: 14 additions & 0 deletions JSONModel/JSONModelTransformations/JSONValueTransformer.m
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,18 @@ -(NSString*)__JSONObjectFromNSDate:(NSDate*)date
return [dateFormatter stringFromDate:date];
}

#pragma mark - hidden transform for empty dictionaries
//https://github.com/icanzilb/JSONModel/issues/163
-(NSDictionary*)__NSDictionaryFromNSArray:(NSArray*)array
{
if (array.count==0) return @{};
return (id)array;
}

-(NSMutableDictionary*)__NSMutableDictionaryFromNSArray:(NSArray*)array
{
if (array.count==0) return [[self __NSDictionaryFromNSArray:array] mutableCopy];
return (id)array;
}

@end
19 changes: 19 additions & 0 deletions JSONModelDemoTests/UnitTests/SpecialPropertiesTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ @interface RModel: JSONModel
@implementation RModel
@end

#pragma mark - empty array/dictionary
@interface DModel: JSONModel
@property (strong, nonatomic) NSDictionary* dict;
@property (strong, nonatomic) NSMutableDictionary* mdict;
@end

@implementation DModel
@end

#pragma mark - test suite

@interface SpecialPropertiesTests : XCTestCase
Expand Down Expand Up @@ -64,5 +73,15 @@ - (void)testReadOnly
XCTAssertNotNil(rm, @"model failed to crate");
}

//test auto-converting array to dict
-(void)testEmtpyDictionary
{
NSString* json = @"{\"dict\":[],\"mdict\":[]}";
DModel* dm = [[DModel alloc] initWithString:json error:nil];
XCTAssertNotNil(dm, @"model failed to crate");
XCTAssertTrue([dm.dict isKindOfClass:[NSDictionary class]], @"property did not convert to dictionary");
XCTAssertTrue([dm.mdict isKindOfClass:[NSMutableDictionary class]], @"property did not convert to mutable dictionary");
}

@end

2 changes: 1 addition & 1 deletion JSONModelDemo_iOS/MBProgressHUD.m
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ - (void)layoutSubviews {
totalSize.width = MAX(totalSize.width, indicatorF.size.width);
totalSize.height += indicatorF.size.height;

CGSize labelSize = [label.text sizeWithFont:label.font];
CGSize labelSize = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];
labelSize.width = MIN(labelSize.width, maxWidth);
totalSize.width = MAX(totalSize.width, labelSize.width);
totalSize.height += labelSize.height;
Expand Down
3 changes: 2 additions & 1 deletion JSONModelDemo_iOS/MasterViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ @interface TopModel : JSONModel
@property (strong, nonatomic) JSONAnswer<Optional>* answer;
@property (assign, nonatomic, readonly) int rId;
@property (nonatomic, copy) void(^userLocationCompleted)();
@property (strong, nonatomic) NSDictionary* dict;
@end

@implementation TopModel
Expand All @@ -113,7 +114,7 @@ @implementation MasterViewController

-(void)viewDidAppear:(BOOL)animated
{
NSString* json = @"{\"id\":1, \"answer\": {\"name1\":\"marin\"}}";
NSString* json = @"{\"id\":1, \"answer\": {\"name1\":\"marin\"}, \"dict\":[]}";
TopModel* tm = [[TopModel alloc] initWithString:json error:nil];
NSLog(@"tm: %@", tm);
}
Expand Down

0 comments on commit ec19f80

Please sign in to comment.