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