-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSManagedObject+JsonSerialization.m
156 lines (122 loc) · 4.61 KB
/
NSManagedObject+JsonSerialization.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// NSManagedObject+JsonSerialization.m
//
// Created by Serge Maslyakov on 30/04/15.
//
#import "NSManagedObject+JsonSerialization.h"
/**
* For ignoring serialization set this key in userInfo dictionary for a CoreData attribute.
*
*/
static const NSString *const kNotSerializeToJSON = @"notSerializeToJSON";
/**
* MagicalRecord mappedKey support.
* It is strongly recommended to use MagicalRecord.
*/
extern const NSString *const kMagicalRecordImportAttributeKeyMapKey;
@implementation NSManagedObject (JsonSerialization)
/**
* Grabbed from here https://gist.github.com/nuthatch/5607405
*
*/
- (NSDictionary *)toDictionaryWithTraversalHistory:(NSMutableSet *)traversalHistory
{
NSArray *attributes = self.entity.attributesByName.allKeys;
NSArray *relationships = self.entity.relationshipsByName.allKeys;
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:attributes.count + relationships.count + 1];
NSMutableSet *localTraversalHistory = nil;
if (!traversalHistory)
{
localTraversalHistory = [NSMutableSet setWithCapacity:attributes.count + relationships.count + 1];
}
else
{
localTraversalHistory = traversalHistory;
}
[localTraversalHistory addObject:self];
for (NSString *attr in attributes)
{
NSDictionary *userInfo = ((NSPropertyDescription *)self.entity.attributesByName[attr]).userInfo;
if (userInfo[kNotSerializeToJSON])
{
continue;
}
NSObject *value = [self valueForKey:attr];
NSString *attrKey = attr;
if (userInfo[kMagicalRecordImportAttributeKeyMapKey])
{
attrKey = userInfo[kMagicalRecordImportAttributeKeyMapKey];
}
if (value)
{
dict[attrKey] = value;
}
}
for (NSString *relationship in relationships)
{
NSDictionary *userInfo = ((NSRelationshipDescription *)self.entity.relationshipsByName[relationship]).userInfo;
if (userInfo[kNotSerializeToJSON])
{
continue;
}
NSObject *value = [self valueForKey:relationship];
NSString *relationshipKey = relationship;
if (userInfo[kMagicalRecordImportAttributeKeyMapKey])
{
relationshipKey = userInfo[kMagicalRecordImportAttributeKeyMapKey];
}
if ([value isKindOfClass:[NSSet class]])
{
// To-many relationship
// The core data set holds a collection of managed objects
NSSet *relatedObjects = (NSSet *)value;
// Our set holds a collection of dictionaries
NSMutableArray *dictSet = [NSMutableArray arrayWithCapacity:relatedObjects.count];
for (NSManagedObject *relatedObject in relatedObjects)
{
if (![localTraversalHistory containsObject:relatedObject])
{
[dictSet addObject:[relatedObject toDictionaryWithTraversalHistory:localTraversalHistory]];
}
}
dict[relationshipKey] = [NSArray arrayWithArray:dictSet];
}
else if ([value isKindOfClass:[NSOrderedSet class]])
{
// To-many relationship
// The core data set holds an ordered collection of managed objects
NSOrderedSet *relatedObjects = (NSOrderedSet *)value;
// Our ordered set holds a collection of dictionaries
NSMutableArray *dictSet = [NSMutableArray arrayWithCapacity:relatedObjects.count];
for (NSManagedObject *relatedObject in relatedObjects)
{
if (![localTraversalHistory containsObject:relatedObject])
{
[dictSet addObject:[relatedObject toDictionaryWithTraversalHistory:localTraversalHistory]];
}
}
dict[relationshipKey] = [NSArray arrayWithArray:dictSet];
}
else if ([value isKindOfClass:[NSManagedObject class]])
{
// To-one relationship
NSManagedObject *relatedObject = (NSManagedObject *)value;
if (![localTraversalHistory containsObject:relatedObject])
{
// Call toDictionary on the referenced object and put the result back into our dictionary.
dict[relationshipKey] = [relatedObject toDictionaryWithTraversalHistory:localTraversalHistory];
}
}
}
if (!traversalHistory)
{
[localTraversalHistory removeAllObjects];
}
return dict;
}
- (NSDictionary *)toDictionary
{
NSMutableSet *traversedObjects = nil;
return [self toDictionaryWithTraversalHistory:traversedObjects];
}
@end