json-stringify.js revision 4612c07daf3859e6ebfc408dc0ef9aed5f97f166
998276643802ff9fb197fe220cbd9552da00a624Luke Smith// Automated tests should only cover js API. Use a manual test for native API
998276643802ff9fb197fe220cbd9552da00a624Luke Smithvar suite = new Y.Test.Suite("Y.JSON.stringify (JavaScript implementation)");
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 tearDown: 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"]}]));
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"]));
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 // Should throw an error
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.log("Stringified Object with cyclical reference, but should have failed.","warn","TestRunner");
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // Should throw an error
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.log("Stringified Array with cyclical reference, but should have failed.","warn","TestRunner");
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var o = [1,2,3,{key:"value",nest:[4,5,6,{foo:"bar"}]}];
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // Should throw an error
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.log("Stringified Object with cyclical reference, but should have failed.","warn","TestRunner");
998276643802ff9fb197fe220cbd9552da00a624Luke Smith arr : [ function () {} ]
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_stringifyDate : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // native toJSON method should be called if available
998276643802ff9fb197fe220cbd9552da00a624Luke Smith ref = d.toJSON ? d.toJSON() : "1946-07-06T00:00:00Z";
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"dt":"'+ref+'"}', Y.JSON.stringify({dt : d}));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function $(id) { return document.getElementById(id); }
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_image : $('input_image').value
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({foo:1,bar:[1,2,3],baz:true},[]));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('[1,true,null,{},["string",null,{}]]',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith 1,true,null,{
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO"}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },["foo"]));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO","baz":true}],"baz":true}',
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 : "FOO",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith foo : "FOO",
998276643802ff9fb197fe220cbd9552da00a624Luke Smith }, {foo : true, baz : false})); // values ignored
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_falseyIndents : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo0":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
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 },null,-4));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo_false":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },null,false));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('{"foo_empty":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
998276643802ff9fb197fe220cbd9552da00a624Luke Smith },null,""));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_indentNumber : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" \"foo\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" \"bar\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" \"baz\": [\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith" \"deep enough\": 7\n" +
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_indentString : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith"Xo\"foo\": [\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 },null,"Xo"));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.JSON.stringify({ toJSON: function () { return "toJSON"; } }));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // TODO: complex object with toJSON
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function A() {}
998276643802ff9fb197fe220cbd9552da00a624Luke Smith A.prototype.toJSON = function () { return "A"; };
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function B() {}
998276643802ff9fb197fe220cbd9552da00a624Luke Smith function C() {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith this.x = "X";
998276643802ff9fb197fe220cbd9552da00a624Luke Smith this.y = "Y";
998276643802ff9fb197fe220cbd9552da00a624Luke Smith this.z = "Z";
998276643802ff9fb197fe220cbd9552da00a624Luke Smith Y.Assert.areSame('"A"', Y.JSON.stringify(new C()));
998276643802ff9fb197fe220cbd9552da00a624Luke Smith test_replacer : function () {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // replacer applies to even simple value stringifications
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 function (k,v) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var t = typeof v;
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 } else if (t === 'number') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // undefined returned to arrays should become null
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // this refers to the object containing the key:value
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // modify the object during stringification
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 function (k,v) {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var t = typeof v;
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 } else if (t === 'number') {
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // undefined returned to arrays should become null
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // this refers to the object containing the key:value
998276643802ff9fb197fe220cbd9552da00a624Luke Smith // modify the object during stringification
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 // 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 Y.Assert.areSame(-1, str.indexOf("X"), "Date incorrectly received by replacer");
998276643802ff9fb197fe220cbd9552da00a624Luke Smith var d = new Date(),
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;