json-stringify.js revision 4612c07daf3859e6ebfc408dc0ef9aed5f97f166
998276643802ff9fb197fe220cbd9552da00a624Luke Smith// Automated tests should only cover js API. Use a manual test for native API
998276643802ff9fb197fe220cbd9552da00a624Luke SmithY.JSON.useNativeStringify = false;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smithvar suite = new Y.Test.Suite("Y.JSON.stringify (JavaScript implementation)");
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smithsuite.add(new Y.Test.Case({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith name : "stringify",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith _should : {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith error : {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_failOnStringifyCyclicalRef1 : true,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_failOnStringifyCyclicalRef2 : true,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_failOnStringifyCyclicalRef3 : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith setUp: function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.one("body").append('<form id="testbed" action="">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<h3>Form used for field value extraction, stringification</h3>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="text" id="empty_text">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="text" id="text" value="text">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="radio" name="radio" id="unchecked_radio" value="unchecked">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="radio" name="radio" id="checked_radio" value="radio" checked="checked">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="checkbox" name="box" id="unchecked_box" value="unchecked">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="checkbox" name="box" id="checked_box" value="box" checked="checked">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<textarea id="empty_textarea"></textarea>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<textarea id="textarea">textarea</textarea>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<select id="select">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<option value="unselected">Unselected</option>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<option value="selected" selected="selected">Selected</option>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '</select>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<select id="multiple_select" multiple="multiple" size="3">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<option value="unselected">Unselected</option>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<option value="selected" selected="selected">Selected</option>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<option value="selected also" selected="selected">Selected also</option>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '</select>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<button id="button" type="button">content; no value</button>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<button id="button_with_value" type="button" value="button value">content and value</button>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<button id="button_submit" type="submit">content; no value</button>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<button id="button_submit_with_value" type="submit" value="submit button value">content and value</button>' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="button" id="input_button" value="input button">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<input type="submit" id="input_submit" value="input submit">' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '<!--input type="image" id="input_image" src="404.png" value="input image"-->' +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '</form>');
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith tearDown: function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.one("#testbed").remove(true);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyNatives: function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('[true,false,null,-0.12345,"string",{"object with one member":["array with one element"]}]',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify([true,false,null,-0.12345,"string",{"object with one member":["array with one element"]}]));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith test_stringifyEscapes: function () {
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith Y.Assert.areSame('["\\\\","\\\\\'","\\\\\\\"","\\\\\\\"","\\b","\\b","\\\\b","\\\\\\b","\\\\\\b","\\b","\\\\x08","\\\\\\b","\\\\\\\\x08","\\n","\\\\n","\\\\\\n"]',
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith Y.JSON.stringify(['\\','\\\'', "\\\"", '\\"', "\b", '\b', "\\b","\\\b", '\\\b', "\x08", "\\x08", "\\\x08","\\\\x08", "\n", "\\n", "\\\n"]));
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith },
4612c07daf3859e6ebfc408dc0ef9aed5f97f166Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyObject : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // stringify sorts the keys
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"one":1,"two":true,"three":false,"four":null,"five":"String with\\nnewline","six":{"nested":-0.12345}}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({one:1,two:true,three:false,four:null,five:"String with\nnewline",six : {nested:-0.12345}}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_failOnStringifyCyclicalRef1 : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var o = { key: 'value' };
998276643802ff9fb197fe220cbd9552da00a624Luke Smith o.recurse = o;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // Should throw an error
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify(o);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.log("Stringified Object with cyclical reference, but should have failed.","warn","TestRunner");
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_failOnStringifyCyclicalRef2 : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var o = [1,2,3];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith o[3] = o;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // Should throw an error
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify(o);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.log("Stringified Array with cyclical reference, but should have failed.","warn","TestRunner");
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_failOnStringifyCyclicalRef3 : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var o = [1,2,3,{key:"value",nest:[4,5,6,{foo:"bar"}]}];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith o[4] = o[3].x = o[3].nest[4] =
998276643802ff9fb197fe220cbd9552da00a624Luke Smith o[3].nest[3].y = o[3].nest[3].z = o;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // Should throw an error
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify(o);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.log("Stringified Object with cyclical reference, but should have failed.","warn","TestRunner");
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyFunction : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"arr":[null]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith functions : function (are,ignored) {},
998276643802ff9fb197fe220cbd9552da00a624Luke Smith arr : [ function () {} ]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyRegex : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"regex":{},"arr":[{}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith regex : /are treated as objects/,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith arr : [ new RegExp("in array") ]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyUndefined : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"arr":[null]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith undef : undefined,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith arr : [ undefined ]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyDate : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // native toJSON method should be called if available
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var d = new Date(Date.UTC(1946,6,6)),
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ref = d.toJSON ? d.toJSON() : "1946-07-06T00:00:00Z";
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"dt":"'+ref+'"}', Y.JSON.stringify({dt : d}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyFormValue : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function $(id) { return document.getElementById(id); }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var data = {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith empty_text : $('empty_text').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith text : $('text').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith unchecked_radio : $('unchecked_radio').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith checked_radio : $('checked_radio').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith unchecked_box : $('unchecked_box').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith checked_box : $('checked_box').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith empty_textarea : $('empty_textarea').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith textarea : $('textarea').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith select : $('select').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith multiple_select : $('multiple_select').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // Buttons commented out for now because IE reports values
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // differently
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //button : $('button').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //button_with_value : $('button_with_value').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //button_submit : $('button_submit').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //button_submit_with_value: $('button_submit_with_value').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith input_button : $('input_button').value,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith input_submit : $('input_submit').value//,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //input_image : $('input_image').value
998276643802ff9fb197fe220cbd9552da00a624Luke Smith };
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"empty_text":"",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"text":"text",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"unchecked_radio":"unchecked",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"checked_radio":"radio",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"unchecked_box":"unchecked",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"checked_box":"box",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"empty_textarea":"",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"textarea":"textarea",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"select":"selected",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"multiple_select":"selected",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //'"button":"",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //'"button_with_value":"button value",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //'"button_submit":"",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //'"button_submit_with_value":"submit button value",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"input_button":"input button",'+
998276643802ff9fb197fe220cbd9552da00a624Luke Smith '"input_submit":"input submit"}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith //'"input_image":"input image"}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify(data));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smithsuite.add(new Y.Test.Case({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith name : "whitelist",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_emptyWhitelistArray : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({foo:1,bar:[1,2,3],baz:true},[]));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('[1,true,null,{},["string",null,{}]]',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify([
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 1,true,null,{
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : false,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : -0.12345
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },["string",undefined,/some regex/]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ],[]));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_whitelistArray : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO"}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : [
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 1,2,3,{
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : "FOO",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ],
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [1,2,3],
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },["foo"]));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO","baz":true}],"baz":true}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : [
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 1,2,3,{
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : "FOO",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ],
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 1,2,3,{
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : "FOO",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ],
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true},["foo","baz"]));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /*
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // REMOVED for spec compatibility
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_whitelistObject : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // (undocumented) supports an obj literal as well
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO","baz":true}],"baz":true}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : [
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 1,2,3,{
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : "FOO",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ],
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 1,2,3,{
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : "FOO",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ],
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : true
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }, {foo : true, baz : false})); // values ignored
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smithsuite.add(new Y.Test.Case({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith name : "formatting",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_falseyIndents : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo0":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo0 : [ 2, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [ 4, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : [ 6, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith "deep enough" : 7
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },null,0));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith /* Commented out because FF3.5 has infinite loop bug for neg indents
998276643802ff9fb197fe220cbd9552da00a624Luke Smith * Fixed in FF for next version.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo-4":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith "foo-4" : [ 2, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [ 4, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : [ 6, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith "deep enough" : 7
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },null,-4));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith */
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo_false":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo_false : [ 2, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [ 4, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : [ 6, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith "deep enough" : 7
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },null,false));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo_empty":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo_empty : [ 2, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [ 4, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : [ 6, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith "deep enough" : 7
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },null,""));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_indentNumber : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame("{\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" \"foo\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" 2,\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" {\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" \"bar\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" 4,\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" {\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" \"baz\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" 6,\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" {\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" \"deep enough\": 7\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" }\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" ]\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" }\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" ]\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" }\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" ]\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"}",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : [ 2, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [ 4, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : [ 6, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith "deep enough" : 7
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },null,2));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_indentString : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame("{\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"Xo\"foo\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXo2,\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXo{\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXo\"bar\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXo4,\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXo{\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXoXo\"baz\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXoXoXo6,\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXoXoXo{\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXoXoXoXo\"deep enough\": 7\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXoXoXo}\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXoXo]\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXoXo}\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXoXo]\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"XoXo}\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"Xo]\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"}",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : [ 2, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith bar : [ 4, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith baz : [ 6, {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith "deep enough" : 7
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },null,"Xo"));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smithsuite.add(new Y.Test.Case({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith name : "toJSON",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_toJSON_on_object: function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('"toJSON"',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({ toJSON: function () { return "toJSON"; } }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // TODO: complex object with toJSON
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_toJSON_on_proto: function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function A() {}
998276643802ff9fb197fe220cbd9552da00a624Luke Smith A.prototype.toJSON = function () { return "A"; };
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function B() {}
998276643802ff9fb197fe220cbd9552da00a624Luke Smith B.prototype = new A();
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function C() {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith this.x = "X";
998276643802ff9fb197fe220cbd9552da00a624Luke Smith this.y = "Y";
998276643802ff9fb197fe220cbd9552da00a624Luke Smith this.z = "Z";
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith C.prototype = new B();
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('"A"', Y.JSON.stringify(new C()));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smithsuite.add(new Y.Test.Case({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith name : "replacer",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_replacer : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // replacer applies to even simple value stringifications
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame("20",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify(10,function (k,v) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return typeof(v) === 'number' ? v * 2 : v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // replacer is applied to every nested property as well.
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // can modify the host object en route
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // executes from the context of the key:value container
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"num":2,"alpha":"ABC","obj":{"nested_num":100,"alpha":"abc"},"arr":[2,null,4]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith num: 1,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith alpha: "abc",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ignore: "me",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith change: "to a function",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith toUpper: true,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith obj: {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith nested_num: 50,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith undef: undefined,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith alpha: "abc"
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith arr: [1, 7, 2]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function (k,v) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var t = typeof v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (k === 'change') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // this property should then be ignored
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return function () {};
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (k === 'ignore') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // this property should then be ignored
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return undefined;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (t === 'number') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // undefined returned to arrays should become null
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return v % 7 ? v * 2 : undefined;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // this refers to the object containing the key:value
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (t === 'string' && (this.toUpper)) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // modify the object during stringification
998276643802ff9fb197fe220cbd9552da00a624Luke Smith delete this.toUpper;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return v.toUpperCase();
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // replacer works in conjunction with indent
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame("{\n_\"num\": 2,\n_\"alpha\": \"ABC\",\n_\"obj\": {\n__\"nested_num\": 100,\n__\"alpha\": \"abc\"\n_},\n_\"arr\": [\n__2,\n__null,\n__4\n_]\n}",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({
998276643802ff9fb197fe220cbd9552da00a624Luke Smith num: 1,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith alpha: "abc",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ignore: "me",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith change: "to a function",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith toUpper: true,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith obj: {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith nested_num: 50,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith undef: undefined,
998276643802ff9fb197fe220cbd9552da00a624Luke Smith alpha: "abc"
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith arr: [1, 7, 2]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function (k,v) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var t = typeof v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith if (k === 'change') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // this property should then be ignored
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return function () {};
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (k === 'ignore') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // this property should then be ignored
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return undefined;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (t === 'number') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // undefined returned to arrays should become null
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return v % 7 ? v * 2 : undefined;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // this refers to the object containing the key:value
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else if (t === 'string' && (this.toUpper)) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // modify the object during stringification
998276643802ff9fb197fe220cbd9552da00a624Luke Smith delete this.toUpper;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return v.toUpperCase();
998276643802ff9fb197fe220cbd9552da00a624Luke Smith } else {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },'_'));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_replacer_after_toJSON : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"a":"ABC"}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({a:{ toJSON: function () { return "abc"; } } },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function (k,v) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return typeof v === 'string' ? v.toUpperCase() : v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // Date instances in ES5 have toJSON that outputs toISOString, which
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // means the replacer should never receive a Date instance
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var str = Y.JSON.stringify([new Date()], function (k,v) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return (v instanceof Date) ? "X" : v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith });
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame(-1, str.indexOf("X"), "Date incorrectly received by replacer");
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_replacer_returning_Date : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var d = new Date(),
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ref = Y.JSON.stringify(d);
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"dt":'+ref+',"date_from_replacer":{}}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({ dt: d, date_from_replacer: 1 },
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function (k,v) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith return typeof v === 'number' ? d : v;
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }
998276643802ff9fb197fe220cbd9552da00a624Luke Smith}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith
998276643802ff9fb197fe220cbd9552da00a624Luke SmithY.Test.Runner.add(suite);