This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
COItem+JSON.m
182 lines (158 loc) · 5.05 KB
/
COItem+JSON.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#import <EtoileFoundation/NSData+Hash.h>
#import "COItem+JSON.h"
#import "COPath.h"
@implementation COItem (JSON)
static id plistValueForPrimitiveValue(id aValue, COType aType)
{
switch (COPrimitiveType(aType))
{
case kCOInt64Type: return aValue;
case kCODoubleType: return aValue;
case kCOStringType: return aValue;
case kCOBlobType: return [aValue base64String];
case kCOCompositeReferenceType:
return [aValue stringValue];
case kCOReferenceType:
if ([aValue isKindOfClass: [COPath class]])
{
return [@"path:" stringByAppendingString: [aValue stringValue]];
}
else
{
return [aValue stringValue];
}
case kCOAttachmentType: return aValue;
default:
[NSException raise: NSInvalidArgumentException format: @"unknown type %d", aType];
return nil;
}
}
static id plistValueForValue(id aValue, COType aType)
{
if (COTypeIsPrimitive(aType))
{
return plistValueForPrimitiveValue(aValue, aType);
}
else
{
NSMutableArray *collection = [NSMutableArray array];
for (id obj in aValue)
{
[collection addObject: plistValueForPrimitiveValue(obj, aType)];
}
return collection;
}
}
static id valueForPrimitivePlistValue(id aValue, COType aType)
{
switch (COPrimitiveType(aType))
{
case kCOInt64Type: return aValue;
case kCODoubleType: return aValue;
case kCOStringType: return aValue;
case kCOBlobType: return [aValue base64DecodedData];
case kCOCompositeReferenceType:
return [ETUUID UUIDWithString: aValue];
case kCOReferenceType:
if ([aValue hasPrefix: @"path:"])
{
return [COPath pathWithString: [aValue substringFromIndex: 5]];
}
else
{
return [ETUUID UUIDWithString: aValue];
}
case kCOAttachmentType: return aValue;
default:
[NSException raise: NSInvalidArgumentException format: @"unknown type %d", aType];
return nil;
}
}
static id valueForPlistValue(id aValue, COType aType)
{
if (COTypeIsPrimitive(aType))
{
return valueForPrimitivePlistValue(aValue, aType);
}
else
{
id collection;
if (COTypeIsOrdered(aType))
{
collection = [NSMutableArray array];
}
else
{
collection = [NSMutableSet set];
}
for (id obj in aValue)
{
[collection addObject: valueForPrimitivePlistValue(obj, aType)];
}
return collection;
}
}
static id exportToPlist(id aValue, COType aType)
{
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity: 2];
[result setObject: [NSNumber numberWithInt: aType] forKey: @"type"];
[result setObject: plistValueForValue(aValue, aType) forKey: @"value"];
return result;
}
static COType importTypeFromPlist(id aPlist)
{
return [[aPlist objectForKey: @"type"] intValue];
}
static id importValueFromPlist(id aPlist)
{
return valueForPlistValue([aPlist objectForKey: @"value"],
[[aPlist objectForKey: @"type"] intValue]);
}
- (id) JSONPlist
{
NSMutableDictionary *plistValues = [NSMutableDictionary dictionaryWithCapacity: [values count]];
for (NSString *key in values)
{
id plistValue = exportToPlist([values objectForKey: key], [[types objectForKey: key] intValue]);
[plistValues setObject: plistValue
forKey: key];
}
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
plistValues, @"values",
[uuid stringValue], @"uuid",
nil];
if (self.schemaName != nil) {
[dict setObject: self.schemaName forKey: @"schema"];
}
return dict;
}
- (id) initWithJSONPlist: (id)aPlist
{
ETUUID *aUUID = [ETUUID UUIDWithString: [aPlist objectForKey: @"uuid"]];
NSMutableDictionary *importedValues = [NSMutableDictionary dictionary];
NSMutableDictionary *importedTypes = [NSMutableDictionary dictionary];
for (NSString *key in [aPlist objectForKey: @"values"])
{
id objPlist = [[aPlist objectForKey: @"values"] objectForKey: key];
[importedValues setObject: importValueFromPlist(objPlist)
forKey: key];
[importedTypes setObject: [NSNumber numberWithInt: importTypeFromPlist(objPlist)]
forKey: key];
}
self = [self initWithUUID: aUUID
typesForAttributes: importedTypes
valuesForAttributes: importedValues];
self.schemaName = [aPlist objectForKey: @"schema"];
return self;
}
- (NSData *) JSONData
{
id plist = [self JSONPlist];
return [NSJSONSerialization dataWithJSONObject: plist options: 0 error: NULL];
}
- (id) initWithJSONData: (NSData *)data
{
id plist = [NSJSONSerialization JSONObjectWithData: data options:0 error: NULL];
return [self initWithJSONPlist: plist];
}
@end