-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLOJSKeyedArchiver.j
99 lines (80 loc) · 3.01 KB
/
LOJSKeyedArchiver.j
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
/*
* LOJSKeyedArchiver.j
*
* Created by Martin Carlberg on Feb 29, 2012.
* Copyright 2012, All rights reserved.
*/
@import <Foundation/CPCoder.j>
var LOJSKeyedArchiverClassKey = @"_type";
@implementation LOJSKeyedArchiver : CPCoder {
id _js;
}
+ (id)archivedDataWithRootObject:(id)rootObject {
var js = {};
var archiver = [[self alloc] initForWritingWithMutableData:js];
return [archiver _encodeObject:rootObject];
}
+ (BOOL)allowsKeyedCoding {
return YES;
}
- (id)initForWritingWithMutableData:(id)js {
if (self = [super init])
{
_js = js;
}
return self;
}
- (void)encodeObject:(id)objectToEncode forKey:(CPString)aKey {
_js[aKey] = [self _encodeObject:objectToEncode];
}
- (id)_encodeObject:(id)objectToEncode {
var encodedJS = {};
if ([self _isObjectAPrimitive:objectToEncode]) {
encodedJS = objectToEncode;
} else if ([objectToEncode isKindOfClass:[CPArray class]]) { // Override CPArray's default encoding because we want native JS Objects
var encodedArray = [];
for (var i = 0; i < [objectToEncode count]; i++) {
encodedArray[i] = [self _encodeObject:[objectToEncode objectAtIndex:i]];
}
encodedJS = encodedArray;
} else if ([objectToEncode isKindOfClass:[CPDictionary class]]) { // Override CPDictionary's default encoding because we want native JS Objects
var encodedDictionary = {};
var keys = [objectToEncode allKeys];
for (var i = 0; i < [keys count]; i++) {
var key = [keys objectAtIndex:i];
encodedDictionary[key] = [self _encodeObject:[objectToEncode objectForKey:key]];
}
encodedJS = encodedDictionary;
} else if (objectToEncode === [CPNull null]) { // Override CPNull's default encoding because we want native JS Objects
encodedJS = nil;
} else {
encodedJS = [objectToEncode jsonWithCoder:self];
// var archiver = [[[self class] alloc] initForWritingWithMutableData:encodedJS];
// encodedJS[LOJSKeyedArchiverClassKey] = [objectToEncode loObjectType];
// [objectToEncode encodeWithCoder:archiver];
}
return encodedJS;
}
- (void)encodeNumber:(int)aNumber forKey:(CPString)aKey {
[self encodeObject:aNumber forKey:aKey];
}
- (void)encodeBool:(BOOL) aBOOL forKey:(CPString) aKey {
[self encodeObject:aBOOL ? @"Yes" : "No" forKey:aKey];
}
- (void)encodeInt:(int)anInt forKey:(CPString)aKey {
[self encodeObject:anInt forKey:aKey];
}
- (id)_encodeDictionaryOfObjects:(CPDictionary)dictionaryToEncode forKey:(CPString)aKey {
var encodedDictionary = {};
var keys = [dictionaryToEncode allKeys];
for (var i = 0; i < [keys count]; i++)
{
encodedDictionary[keys[i]] = [self _encodeObject:[dictionaryToEncode objectForKey:keys[i]]];
}
_js[aKey] = encodedDictionary;
}
- (BOOL)_isObjectAPrimitive:(id)anObject {
var typeOfObject = typeof(anObject);
return (typeOfObject === "string" || typeOfObject === "number" || typeOfObject === "boolean" || anObject === null);
}
@end