Skip to content

Commit 154b73a

Browse files
committed
added convinience method for mapping, refactored the code
1 parent bcc8077 commit 154b73a

File tree

6 files changed

+208
-116
lines changed

6 files changed

+208
-116
lines changed

README.md

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ SHModelObject
33

44
`SHModelObject` is a utility model Base Class that uses objective-c runtime to assign the values to instance variables and properties of the model class from an `NSDictionary`, Which is a basic usecase when using webservices that return JSON response.
55

6-
[![Build Status](https://travis-ci.org/grevolution/SHModelObject.svg?branch=master)](https://travis-ci.org/grevolution/SHModelObject)
6+
[![Build Status](https://img.shields.io/travis/grevolution/SHModelObject.svg?branch=master)](https://travis-ci.org/grevolution/SHModelObject) [![Pod Version](https://img.shields.io/cocoapods/v/SHModelObject.svg)](https://img.shields.io/cocoapods/v/SHModelObject.svg) [![License](https://img.shields.io/cocoapods/l/SHModelObject.svg)](https://img.shields.io/cocoapods/l/SHModelObject.svg)
77

88
Let say you have a WebService that serves you back the following Response:
99

@@ -79,7 +79,110 @@ you can override `- (void)serializeValue:(id)value withKey:(id)key` method if yo
7979

8080
##Parsing .NET JSON Dates to NSDate or NSTimeInterval
8181

82-
you can use `kDateConversionOption` to convert the .NET JSON Date Strings to either `NSDate` or `NSTimeInterval` or keep it as `NSString` and parse yourself.
82+
you can use `kDateConversionOption` to convert the .NET JSON Date Strings to either `NSDate` or `NSTimeInterval` or keep it as `NSString` and parse yourself and also you can define `kInputDateFormat` to specify your input date format (JSON format, .NET Simple or .NET with timezone)
83+
84+
85+
##Parsing instance variables which are also a subclass of `SHModelObject`
86+
87+
you dont have to do anything :). `SHModelObject` automatically handles it. checkout the sample code.
88+
89+
for example, following JSON
90+
91+
```
92+
93+
{
94+
"name" : "Shan Ul Haq",
95+
"person_id" : 123,
96+
"image" : {
97+
"image_id" : 234,
98+
"image_url" : http://image_url,
99+
"orientation" : "portrait"
100+
}
101+
}
102+
103+
```
104+
will be automatically parsed into following object:
105+
106+
```
107+
@interface Person : SHModelObject
108+
109+
@property(nonatomic, strong) NSString *name;
110+
@property(nonatomic) int personId;
111+
@property(nonatomic, strong) Image *image;
112+
113+
@end
114+
```
115+
where Image object is also a `SHModelObject`
116+
117+
```
118+
@interface Image : SHModelObject
119+
120+
@property(nonatomic) int imageId;
121+
@property(nonatomic, strong) NSString *imageUrl;
122+
@property(nonatomic, strong) NSString *orientation;
123+
124+
@end
125+
```
126+
127+
###Parsing arrays of objects which are a subclass of `SHModelObject`
128+
129+
similar to parsing `SHModelObject` instance variables, arrays can be handled too. you need to specify the mapping which will define that the JSON array consist of which object type.
130+
131+
for example, if you have following JSON
132+
133+
```
134+
{
135+
"aKey" : "aValue",
136+
"arrayOfModels" : [
137+
{
138+
"modelId" : 2,
139+
"modelName" : "My Model 2",
140+
"modelType" : "My Model Type 2"
141+
},{
142+
"modelId" : 3,
143+
"modelName" : "My Model 3",
144+
"modelType" : "My Model Type 3"
145+
},{
146+
"modelId" : 4,
147+
"modelName" : "My Model 4",
148+
"modelType" : "My Model Type 4"
149+
}
150+
]
151+
}
152+
}
153+
154+
```
155+
which translates to following objects.
156+
157+
```
158+
@interface MyObject : SHModelObject
159+
160+
@property(nonatomic, strong) NSString *aKey;
161+
@property(nonatomic, strong) NSArray *arrayOfModels;
162+
163+
@end
164+
165+
@interface AModel : SHModelObject
166+
167+
@property(nonatomic) int modelId;
168+
@property(nonatomic, strong) NSString *modelName;
169+
@property(nonatomic, strong) NSString *modelType;
170+
171+
@end
172+
173+
174+
```
175+
176+
you can convert json like this:
177+
178+
```
179+
// key is the variable name and value is the class name.
180+
NSDictionary *mappingDictionary = @{@"arrayOfModels" : "AModel"};
181+
182+
MyObject *myObject = [MyObject objectWithDictionary:dictionary mapping:mappingDictionary];
183+
184+
```
185+
83186

84187
##How to Use it.
85188

@@ -96,8 +199,9 @@ you can use `kDateConversionOption` to convert the .NET JSON Date Strings to eit
96199
##Tasks Pending
97200

98201
- [X] adding to cocoapods.
99-
- [ ] adding support for custom instance variable types that are also subclasses of `SHModelObject`
100-
- [ ] implementing `NSCoding` for archiving/unarchiving the model objects.
202+
- [X] adding support for custom instance variable types that are also subclasses of `SHModelObject`
203+
- [X] implementing `NSCoding` for archiving/unarchiving the model objects.
204+
- [X] adding support for handling arrays of custom `SHModelObject` objects.
101205
- [ ] implementing a deserializer for converting the object to NSDictionary.
102206

103207
##Contact Me

SHModelObject.xcodeproj/xcuserdata/shanulhaq.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,18 @@
33
type = "1"
44
version = "2.0">
55
<Breakpoints>
6-
<BreakpointProxy
7-
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
8-
<BreakpointContent
9-
shouldBeEnabled = "Yes"
10-
ignoreCount = "0"
11-
continueAfterRunningActions = "No"
12-
filePath = "SHModelObject/SHModelObject/SHModelObject.m"
13-
timestampString = "448012336.337347"
14-
startingColumnNumber = "9223372036854775807"
15-
endingColumnNumber = "9223372036854775807"
16-
startingLineNumber = "308"
17-
endingLineNumber = "308"
18-
landmarkName = "-serializeValue:withKey:"
19-
landmarkType = "5">
20-
</BreakpointContent>
21-
</BreakpointProxy>
226
<BreakpointProxy
237
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
248
<BreakpointContent
259
shouldBeEnabled = "Yes"
2610
ignoreCount = "0"
2711
continueAfterRunningActions = "No"
2812
filePath = "SHModelObject/SHAppDelegate.m"
29-
timestampString = "448012488.962849"
13+
timestampString = "448026024.247668"
3014
startingColumnNumber = "9223372036854775807"
3115
endingColumnNumber = "9223372036854775807"
32-
startingLineNumber = "80"
33-
endingLineNumber = "80"
16+
startingLineNumber = "79"
17+
endingLineNumber = "79"
3418
landmarkName = "-application:didFinishLaunchingWithOptions:"
3519
landmarkType = "5">
3620
</BreakpointContent>

SHModelObject/SHAppDelegate.m

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
3333
[self.window makeKeyAndVisible];
3434

3535

36-
NSDictionary *a = @{ @"string_value" : @"shan",
36+
NSDictionary *aDictionary = @{ @"string_value" : @"shan",
3737
@"another_string_value" : @"ul haq",
3838
@"INTVALUE" : @12,
3939
@"AnotherIntValue" : @23,
@@ -72,19 +72,18 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
7272
]
7373
};
7474

75-
NSDictionary *mapping = @{@"time1":@"",@"time2":@"",@"time3":@"", @"arrayOfAnotherModels" : @"SHAnotherModel"};
76-
SHTestModal *modal = [[SHTestModal alloc] initWithDictionary:a
77-
dateConversionOption:kDateConverstionFromNSStringToNSDateOption
78-
inputDateType:kInputDateFormatDotNetWithTimeZone mappings:mapping];
75+
NSDictionary *mapping = @{@"arrayOfAnotherModels" : @"SHAnotherModel"};
7976

77+
SHTestModal *model = [SHTestModal objectWithDictionary:aDictionary dateConversionOption:kDateConverstionFromNSStringToNSTimeIntervalOption inputDateType:kInputDateFormatDotNetWithTimeZone mappings:mapping];
78+
8079
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
8180
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
8281
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"appData"];
8382

84-
BOOL isArchived = [NSKeyedArchiver archiveRootObject:modal toFile:filePath];
83+
BOOL isArchived = [NSKeyedArchiver archiveRootObject:model toFile:filePath];
8584
SHTestModal *unarchived = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
8685

87-
NSLog([modal description], nil);
86+
NSLog([model description], nil);
8887
NSLog([unarchived description], nil);
8988

9089

SHModelObject/SHModelObject/SHModelObject.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ typedef NS_ENUM(int, kInputDateFormat) {
6868
*/
6969
- (instancetype)initWithDictionary:(NSDictionary *)dictionary dateConversionOption:(kDateConversionOption)option
7070
inputDateType:(kInputDateFormat)inputDateType
71-
mappings:(NSDictionary *)dateValueMapping;
71+
mappings:(NSDictionary *)mapping;
7272

7373
/**
7474
* static variant of class initializer with nil check for the dictionary passed
@@ -79,18 +79,31 @@ typedef NS_ENUM(int, kInputDateFormat) {
7979
*/
8080
+ (instancetype)objectWithDictionary:(NSDictionary *)dictionary;
8181

82+
/**
83+
* static variant of class initializer with nil check for the dictionary passed and mapping dictionary
84+
* this will use the default date conversion options (kDateConverstionFromNSStringToNSStringOption and
85+
* kInputDateFormatJSON)
86+
*
87+
* @param dictionary dictionary containing key/value pairs for the object
88+
* @param mapping dictionary to define the mappings for date conversion and array <-> object.
89+
*
90+
* @return object of type instancetype populated with the values from `dictionary`
91+
*/
92+
+ (instancetype)objectWithDictionary:(NSDictionary *)dictionary mappings:(NSDictionary *)mapping;
93+
8294
/**
8395
* static variant of class initializer with nil check for the dictionary passed
8496
*
8597
* @param dictionary dictionary containing key/value pairs for the object
8698
* @param option `kDateConverstionOption` to determine what to do when the value from dictionary is a DOT.NET Date type.
8799
* @param inputDateType `kInputDateFormat` to determine what what is the input format of the date.
100+
* @param mapping dictionary to define the mappings for date conversion and array <-> object.
88101
*
89102
* @return object of type instancetype populated with the values from `dictionary`
90103
*/
91104
+ (instancetype)objectWithDictionary:(NSDictionary *)dictionary dateConversionOption:(kDateConversionOption)option
92105
inputDateType:(kInputDateFormat)inputDateType
93-
mappings:(NSDictionary *)dateValueMapping;
106+
mappings:(NSDictionary *)mapping;
94107

95108
/**
96109
* update the instance with new dictionary values
@@ -107,12 +120,13 @@ typedef NS_ENUM(int, kInputDateFormat) {
107120
* @param dictionary dictionary containing key/value pairs for the object
108121
* @param option `kDateConverstionOption` to determine what to do when the value from dictionary is a DOT.NET Date type.
109122
* @param inputDateType `kInputDateFormat` to determine what what is the input format of the date.
123+
* @param mapping dictionary to define the mappings for date conversion and array <-> object.
110124
*
111125
* @return object of type instancetype populated with the values from `dictionary`
112126
*/
113127
- (instancetype)updateWithDictionary:(NSDictionary *)dictionary dateConversionOption:(kDateConversionOption)option
114128
inputDateType:(kInputDateFormat)inputDateType
115-
mappings:(NSDictionary *)dateValueMapping;
129+
mappings:(NSDictionary *)mapping;
116130
@end
117131

118132

0 commit comments

Comments
 (0)