00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import "CPObject.j"
00024 @import "CPCoder.j"
00025
00026
00032 @implementation CPValue : CPObject
00033 {
00034 JSObject _JSObject;
00035 }
00036
00042 + (id)valueWithJSObject:(JSObject)aJSObject
00043 {
00044 return [[self alloc] initWithJSObject:aJSObject];
00045 }
00046
00052 - (id)initWithJSObject:(JSObject)aJSObject
00053 {
00054 self = [super init];
00055
00056 if (self)
00057 _JSObject = aJSObject;
00058
00059 return self;
00060 }
00061
00065 - (JSObject)JSObject
00066 {
00067 return _JSObject;
00068 }
00069
00070 @end
00071
00072 var CPValueValueKey = @"CPValueValueKey";
00073
00074 @implementation CPValue (CPCoding)
00075
00081 - (id)initWithCoder:(CPCoder)aCoder
00082 {
00083 self = [super init];
00084
00085 if (self)
00086 _JSObject = CPJSObjectCreateWithJSON([aCoder decodeObjectForKey:CPValueValueKey]);
00087
00088 return self;
00089 }
00090
00095 - (void)encodeWithCoder:(CPCoder)aCoder
00096 {
00097 [aCoder encodeObject:CPJSObjectCreateJSON(_JSObject) forKey:CPValueValueKey];
00098 }
00099
00100 @end
00101
00102 var _JSONCharacterEncodings = {};
00103
00104 _JSONCharacterEncodings['\b'] = "\\b";
00105 _JSONCharacterEncodings['\t'] = "\\t";
00106 _JSONCharacterEncodings['\n'] = "\\n";
00107 _JSONCharacterEncodings['\f'] = "\\f";
00108 _JSONCharacterEncodings['\r'] = "\\r";
00109 _JSONCharacterEncodings['"'] = "\\\"";
00110 _JSONCharacterEncodings['\\'] = "\\\\";
00111
00112
00113 var _JSONEncodedCharacters = new RegExp("[\\\"\\\\\\x00-\\x1f\\x7f-\\x9f]", 'g');
00114
00115 function CPJSObjectCreateJSON(aJSObject)
00116 {
00117
00118
00119 var type = typeof aJSObject,
00120 valueOf = aJSObject ? aJSObject.valueOf() : null,
00121 typeValueOf = typeof valueOf;
00122
00123 if (type != typeValueOf)
00124 {
00125 type = typeValueOf;
00126 aJSObject = valueOf;
00127 }
00128
00129 switch (type)
00130 {
00131 case "string":
00132
00133
00134
00135 if (!_JSONEncodedCharacters.test(aJSObject))
00136 return '"' + aJSObject + '"';
00137
00138 return '"' + aJSObject.replace(_JSONEncodedCharacters, _CPJSObjectEncodeCharacter) + '"';
00139
00140
00141 case "number":
00142 return isFinite(aJSObject) ? String(aJSObject) : "null";
00143
00144 case "boolean":
00145 case "null": return String(aJSObject);
00146
00147 case "object":
00148
00149
00150 if (!aJSObject)
00151 return "null";
00152
00153
00154
00155 if (typeof aJSObject.toJSON === "function")
00156 return CPJSObjectCreateJSON(aJSObject.toJSON());
00157
00158 var array = [];
00159
00160
00161
00162 if (aJSObject.slice)
00163 {
00164 var index = 0,
00165 count = aJSObject.length;
00166
00167 for (; index < count; ++index)
00168 array.push(CPJSObjectCreateJSON(aJSObject[index]) || "null");
00169
00170
00171 return '[' + array.join(',') + ']';
00172 }
00173
00174
00175
00176 var key = NULL;
00177
00178 for (key in aJSObject)
00179 {
00180 if (!(typeof key === "string"))
00181 continue;
00182
00183 var value = CPJSObjectCreateJSON(aJSObject[key]);
00184
00185 if (value)
00186 array.push(CPJSObjectCreateJSON(key) + ':' + value);
00187 }
00188
00189
00190 return '{' + array.join(',') + '}';
00191 }
00192 }
00193
00194 var _CPJSObjectEncodeCharacter = function(aCharacter)
00195 {
00196 var encoding = _JSONCharacterEncodings[aCharacter];
00197
00198 if (encoding)
00199 return encoding;
00200
00201 encoding = aCharacter.charCodeAt(0);
00202
00203 return '\\u00' + FLOOR(encoding / 16).toString(16) + (encoding % 16).toString(16);
00204 }
00205
00206 var _JSONBackslashCharacters = new RegExp("\\\\.", 'g'),
00207 _JSONSimpleValueTokens = new RegExp("\"[^\"\\\\\\n\\r]*\"|true|false|null|-?\\d+(?:\\.\\d*)?(?:[eE][+\\-]?\\d+)?", 'g'),
00208 _JSONValidOpenBrackets = new RegExp("(?:^|:|,)(?:\\s*\\[)+", 'g'),
00209 _JSONValidExpression = new RegExp("^[\\],:{}\\s]*$");
00210
00211 function CPJSObjectCreateWithJSON(aString)
00212 {
00213 if (_JSONValidExpression.test(aString.replace(_JSONBackslashCharacters, '@').replace(_JSONSimpleValueTokens, ']').replace(_JSONValidOpenBrackets, '')))
00214 return eval('(' + aString + ')');
00215
00216 return nil;
00217 }
00218
00219
00220
00221
00222
00223
00224