Skip to content

Commit e5d69f0

Browse files
committed
Merge pull request #86 from viktorasl/feature/json-preprocessing
JSON preprocessing static method + docs
2 parents d3f38b1 + 588c043 commit e5d69f0

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ You should specify the inbound and outbound JSON mapping on your `RLMObject` sub
8585
};
8686
}
8787

88+
JSON preprocessing can be done by implementing `jsonPreprocessing:` static method:
89+
90+
```ObjC
91+
- (NSDictionary *)jsonPreprocessing:(NSDictionary *)dictionary {
92+
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:dictionary];
93+
dict[@"releaseCount"] = @(0);
94+
return dict.copy;
95+
}
96+
```
97+
8898
Leaving out either one of the above will result in a mapping that assumes camelCase for your properties which map to snake_case for the JSON equivalents.
8999

90100
As you can do with Mantle, you can specify `NSValueTransformers` for your properties:

Realm+JSON.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'Realm+JSON'
3-
s.version = '0.2.14'
3+
s.version = '0.2.16'
44
s.ios.deployment_target = '7.0'
55
s.osx.deployment_target = '10.9'
66
s.watchos.deployment_target = '2.0'

Realm+JSON/RLMObject+JSON.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,24 @@ - (void)removeFromRealm {
156156

157157
#pragma mark - Private
158158

159-
+ (id)mc_createObjectFromJSONDictionary:(NSDictionary *)dictionary {
159+
+ (id)mc_createObjectFromJSONDictionary:(NSDictionary *)origDict {
160160
NSMutableDictionary *result = [NSMutableDictionary dictionary];
161161
NSDictionary *mapping = [[self class] mc_inboundMapping];
162162

163+
NSDictionary *dictionary;
164+
SEL preprocessingSel = NSSelectorFromString(@"preprocessedJSON:");
165+
if ([self respondsToSelector:preprocessingSel]) {
166+
dictionary = [self performSelector:preprocessingSel withObject:origDict];
167+
} else {
168+
dictionary = origDict;
169+
}
170+
163171
for (NSString *dictionaryKeyPath in mapping) {
164172
NSString *objectKeyPath = mapping[dictionaryKeyPath];
165-
173+
166174
id value = [dictionary valueForKeyPath:dictionaryKeyPath];
167175

168-
if (value) {
176+
if (value) {
169177
Class propertyClass = [[self class] mc_classForPropertyKey:objectKeyPath];
170178

171179
NSValueTransformer *transformer = [[self class] mc_transformerForPropertyKey:objectKeyPath];

0 commit comments

Comments
 (0)