This repository has been archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassMapper.m
52 lines (46 loc) · 1.68 KB
/
ClassMapper.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// ClassMapper.m
// ClassMapper
//
// Created by Patrick Shields on 12/4/11.
// Copyright (c) 2011 Patrick Shields. All rights reserved.
//
#import "ClassMapper.h"
#import "MapperConfig.h"
/* Reflection */
#import <Foundation/NSObjCRuntime.h>
#import <objc/runtime.h>
@implementation ClassMapper
#pragma mark deserialize
+ (id)deserialize:(id)serialized toInstance:(id)instance {
/* We can't turn null into anything else, so just return it */
if (serialized == [NSNull null]) {
return serialized;
}
return [instance _cm_update_with:serialized withClass:nil];
}
+ (id)deserialize:(id)serialized toClass:(Class)classType {
/* We can't turn null into anything else, so just return it */
if (serialized == [NSNull null]) {
return serialized;
}
/* Generally we want to treat class information at the collection level
if the collection is not pair oriented. Dicitionaries sort of lie
about the fast enumeration (you just get keys), so we explicitly
throw them out. But otherwise, this makes it a lot easier to do
something like use a set.
*/
if (![serialized isKindOfClass:[NSDictionary class]] &&
[serialized conformsToProtocol:@protocol(NSFastEnumeration)]) {
return [[serialized class] _cm_inst_from:serialized withClass:classType];
} else if ([classType respondsToSelector:@selector(_cm_inst_from:withClass:)]) {
return [classType _cm_inst_from:serialized withClass:classType];
} else {
return [[classType new] _cm_update_with:serialized withClass:classType];
}
}
#pragma mark serialize
+ (id)serialize:(id<Serializable>)object {
return [object _cm_serialize];
}
@end