Skip to content

Commit

Permalink
Update JSON support to json2.js, added in Objective-J. Deprecate CPJS…
Browse files Browse the repository at this point in the history
…ObjectCreateJSON and CPJSObjectCreateWithJSON.
  • Loading branch information
Tom Robinson authored and Francisco Ryan Tolmasky I committed Jun 13, 2009
1 parent 0ca573c commit 774ea99
Show file tree
Hide file tree
Showing 4 changed files with 485 additions and 118 deletions.
4 changes: 2 additions & 2 deletions Foundation/CPString.j
Original file line number Diff line number Diff line change
Expand Up @@ -659,15 +659,15 @@ var CPStringHashes = new objj_dictionary();
*/
+ (CPString)JSONFromObject:(JSObject)anObject
{
return CPJSObjectCreateJSON(anObject);
return JSON.stringify(anObject);
}

/*!
Returns a JavaScript object decoded from the string's JSON representation.
*/
- (JSObject)objectFromJSON
{
return CPJSObjectCreateWithJSON(self);
return JSON.parse(self);
}

@end
Expand Down
120 changes: 4 additions & 116 deletions Foundation/CPValue.j
Original file line number Diff line number Diff line change
Expand Up @@ -99,126 +99,14 @@ var CPValueValueKey = @"CPValueValueKey";

@end

var _JSONCharacterEncodings = {};

_JSONCharacterEncodings['\b'] = "\\b";
_JSONCharacterEncodings['\t'] = "\\t";
_JSONCharacterEncodings['\n'] = "\\n";
_JSONCharacterEncodings['\f'] = "\\f";
_JSONCharacterEncodings['\r'] = "\\r";
_JSONCharacterEncodings['"'] = "\\\"";
_JSONCharacterEncodings['\\'] = "\\\\";

// FIXME: Workaround for https://trac.280north.com/ticket/16
var _JSONEncodedCharacters = new RegExp("[\\\"\\\\\\x00-\\x1f\\x7f-\\x9f]", 'g');

function CPJSObjectCreateJSON(aJSObject)
{
// typeof new Number() and new String() gives you "object",
// so valueof in those cases.
var type = typeof aJSObject,
valueOf = aJSObject ? aJSObject.valueOf() : null,
typeValueOf = typeof valueOf;

if (type != typeValueOf)
{
type = typeValueOf;
aJSObject = valueOf;
}

switch (type)
{
case "string": // If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe sequences.

if (!_JSONEncodedCharacters.test(aJSObject))
return '"' + aJSObject + '"';

return '"' + aJSObject.replace(_JSONEncodedCharacters, _CPJSObjectEncodeCharacter) + '"';


case "number": // JSON numbers must be finite. Encode non-finite numbers as null.
return isFinite(aJSObject) ? String(aJSObject) : "null";

case "boolean":
case "null": return String(aJSObject);

case "object": // Due to a specification blunder in ECMAScript,
// typeof null is 'object', so watch out for that case.

if (!aJSObject)
return "null";

// If the object has a toJSON method, call it, and stringify the result.

if (typeof aJSObject.toJSON === "function")
return CPJSObjectCreateJSON(aJSObject.toJSON());

var array = [];

// If the object is an array. Stringify every element. Use null as a placeholder
// for non-JSON values.
if (aJSObject.slice)
{
var index = 0,
count = aJSObject.length;

for (; index < count; ++index)
array.push(CPJSObjectCreateJSON(aJSObject[index]) || "null");

// Join all of the elements together and wrap them in brackets.
return '[' + array.join(',') + ']';
}


// Otherwise, iterate through all of the keys in the object.
var key = NULL;

for (key in aJSObject)
{
if (!(typeof key === "string"))
continue;

var value = CPJSObjectCreateJSON(aJSObject[key]);

if (value)
array.push(CPJSObjectCreateJSON(key) + ':' + value);
}

// Join all of the member texts together and wrap them in braces.
return '{' + array.join(',') + '}';
}
}

var _CPJSObjectEncodeCharacter = function(aCharacter)
{
var encoding = _JSONCharacterEncodings[aCharacter];

if (encoding)
return encoding;

encoding = aCharacter.charCodeAt(0);

return '\\u00' + FLOOR(encoding / 16).toString(16) + (encoding % 16).toString(16);
CPLog.warn("CPJSObjectCreateJSON deprecated, use JSON.stringify() or CPString's objectFromJSON");
return JSON.stringify(aJSObject);
}

var _JSONBackslashCharacters = new RegExp("\\\\.", 'g'),
_JSONSimpleValueTokens = new RegExp("\"[^\"\\\\\\n\\r]*\"|true|false|null|-?\\d+(?:\\.\\d*)?(?:[eE][+\\-]?\\d+)?", 'g'),
_JSONValidOpenBrackets = new RegExp("(?:^|:|,)(?:\\s*\\[)+", 'g'),
_JSONValidExpression = new RegExp("^[\\],:{}\\s]*$");

function CPJSObjectCreateWithJSON(aString)
{
if (_JSONValidExpression.test(aString.replace(_JSONBackslashCharacters, '@').replace(_JSONSimpleValueTokens, ']').replace(_JSONValidOpenBrackets, '')))
return eval('(' + aString + ')');

return nil;
CPLog.warn("CPJSObjectCreateWithJSON deprecated, use JSON.parse() or CPString's JSONFromObject");
return JSON.parse(aString);
}

/*
var _JSONBackslashCharacters = /\\./g,
_JSONSimpleValueTokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
_JSONValidOpenBrackets = /(?:^|:|,)(?:\s*\[)+/g,
_JSONValidExpression = /^[\],:{}\s]*$/;
*/
1 change: 1 addition & 0 deletions Objective-J/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ task :Products => [$BROWSER_FILE, $RHINO_FILE, $LICENSE_PRODUCT]

Files = [ 'constants.js',
'utilities.js',
'json2.js',
'runtime.js',
'dictionary.js',
'plist.js',
Expand Down

0 comments on commit 774ea99

Please sign in to comment.