json.html revision a87b7acef05cfdc407cb96d1f42a0e533a699a80
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith <link type="text/css" rel="stylesheet" href="/assets/dpSyntaxHighlighter.css">
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith <link type="text/css" rel="stylesheet" href="/build/test/assets/test-console.css">
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith <style type="text/css" id="styleblock" class="highlight-ignore">
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith font: normal 125%/1.4 Arial, sans-serif;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .yui-skin-sam .yui-console .yui-console-content {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith font-size: 10px;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .yui-skin-sam .yui-console-entry-pass .yui-console-entry-cat {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith background: #070;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith color: #fff;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .yui-skin-sam .yui-console-entry-fail .yui-console-entry-cat {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith background: #700;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith color: #fff;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .highlight-example {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith display: inline;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith float: left;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith width: 650px;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .highlight-example h2 {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith display: none;
b0cfaf8e62a833866fd861fe7ff60747ba997be0Luke Smith .yui-skin-sam .yui-console-entry-time {
b0cfaf8e62a833866fd861fe7ff60747ba997be0Luke Smith display: none;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <h3>Form used for field value extraction, stringification</h3>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <input type="radio" name="radio" id="unchecked_radio" value="unchecked">
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <input type="radio" name="radio" id="checked_radio" value="radio" checked="checked">
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <input type="checkbox" name="box" id="unchecked_box" value="unchecked">
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <input type="checkbox" name="box" id="checked_box" value="box" checked="checked">
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <option value="selected" selected="selected">Selected</option>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <select id="multiple_select" multiple="multiple" size="3">
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <option value="selected" selected="selected">Selected</option>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <option value="selected also" selected="selected">Selected also</option>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <button id="button" type="button">content; no value</button>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <button id="button_with_value" type="button" value="button value">content and value</button>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <button id="button_submit" type="submit">content; no value</button>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <button id="button_submit_with_value" type="submit" value="submit button value">content and value</button>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <input type="button" id="input_button" value="input button">
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <input type="submit" id="input_submit" value="input submit">
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <input type="image" id="input_image" src="404.png" value="input image">
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<script type="text/javascript" src="/build/yui/yui.js"></script>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<script type="text/javascript" src="/build/console/console-debug.js"></script>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<script type="text/javascript" src="/build/json/json-debug.js"></script>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<script type="text/javascript" src="/build/test/test-debug.js"></script>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith filter : 'debug',
3b384bcd33151a8845965775e0732450ad8cfdb0Luke Smith useBrowserConsole : false,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith logInclude : { TestRunner: true, json: true, JSON: true }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith}).use('test','console','json',function (Y) {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smithvar d = document,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith suite = new Y.Test.Suite("JSON Tests"),
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith JSON_STRING;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke SmithJSON_STRING = '[\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "JSON Test Pattern pass1",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith {"object with 1 member":["array with 1 element"]},\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "integer": 1234567890,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "real": -9876.543210,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "e": 0.123456789e-12,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "E": 1.234567890E+34,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "": 23456789012E66,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "space": " ",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "quote": "\\"",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "backslash": "\\\\",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "controls": "\\b\\f\\n\\r\\t",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "slash": "/ & \\/",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "alpha": "abcdefghijklmnopqrstuvwxyz",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "digit": "0123456789",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "0123456789": "digit",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "special": "`1~!@#$%^&*()_+-={\':[,]}|;.</>?",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "hex": "\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "true": true,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "false": false,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "null": null,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "array":[ ],\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "object":{ },\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "address": "50 St. James Street",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "comment": "// /* <!-- --",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "# -- --> */": " ",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith " s p a c e d " :[1,2 , 3\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "jsontext": "{\\\"object with 1 member\\\":[\\\"array with 1 element\\\"]}",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "quotes": "" \\u0022 %22 0x22 034 "",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "\\/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?" : "A key can be any string"\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith/*****************************/
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith/* Tests begin here */
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith/*****************************/
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith// wrapped in a function to allow repeating tests with native behavior disabled
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smithfunction addTests() {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith name : "parse",
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnEmptyString : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnFunction : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnRegex : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnNew : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnquotedVal : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnquotedKey : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnclosedObject : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnclosedArray : true,
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith test_failOnExtraCommaInObject : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnDoubleExtraCommaInObject : true,
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith test_failOnExtraCommaInArray : true,
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith test_failOnDoubleExtraCommaInArray : true,
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith test_failOnMissingValue : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnCommaAfterClose : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnValueAfterClose : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnExtraClose : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnExpression : true,
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith test_failOnZeroPrefixedNumber : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnHex : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnIllegalBackslashEscape : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnMissingColon : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnDoubleColon : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnCommaInsteadOfColon : true,
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith test_failOnExtraClose : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnColonInsteadOfComma : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnSingleQuote : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnTabCharacter : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnLineBreakChar : true,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnMismatchedClose : true
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_emptyObject : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_emptyArray : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_JSONNatives : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // Note: backslashes are double escaped to emulate string returned from
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith var data = Y.JSON.parse('{"obj":{},"arr":[],"f":false,"t":true,"n":null,"int":12345,"fl":1.2345,"str":"String\\nwith\\tescapes"}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("String\nwith\tescapes",data.str);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_basics : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith var data = Y.JSON.parse(JSON_STRING);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isArray(data[1]["object with 1 member"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1,data[1]["object with 1 member"].length);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1234567890,data[8].integer);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(-9876.543210,data[8].real);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(0.123456789e-12,data[8].e);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1.234567890e+34,data[8].E);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(23456789012E66,data[8][""]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(0,data[8].zero);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1,data[8].one);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(" ",data[8].space);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('"',data[8].quote);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("\\",data[8].backslash);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("\b\f\n\r\t",data[8].controls);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("/ & \/",data[8].slash);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("abcdefghijklmnopqrstuvwxyz",data[8].alpha);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("ABCDEFGHIJKLMNOPQRSTUVWXYZ",data[8].ALPHA);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("0123456789",data[8].digit);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("digit",data[8]["0123456789"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1234567890,data[8].integer);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("`1~!@#$%^&*()_+-={':[,]}|;.</>?",data[8].special);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",data[8].hex);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(true,data[8]["true"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(false,data[8]["false"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isNull(data[8]["null"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isArray(data[8]["array"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isObject(data[8]["object"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("http://www.JSON.org/",data[8].url);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("// /* <!-- --",data[8].comment);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(" ",data[8]["# -- --> */"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.ArrayAssert.itemsAreSame([1,2,3,4,5,6,7],data[8][" s p a c e d "]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.ArrayAssert.itemsAreSame([1,2,3,4,5,6,7],data[8].compact);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("{\"object with 1 member\":[\"array with 1 element\"]}",data[8].jsontext);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("" \u0022 %22 0x22 034 "",data[8].quotes);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("A key can be any string",data[8]["\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(98.6,data[10]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(99.44,data[11]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1066,data[12]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(0.1e1,data[14]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1e-1,data[15]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1e00,data[16]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(2e+00,data[17]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(2e-00,data[18]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("rosebud",data[19]);
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith test_nonObjectWrapper : function () {
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith //Y.Assert.areSame('this is a string',Y.JSON.parse('"this is a string"'));
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith //Y.Assert.areSame(1.2345,Y.JSON.parse("1.2345"));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnEmptyString : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed empty string, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnFunction : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"fn":function(){}}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed a function, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnRegex : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"re":/abc/}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed regular expression literal, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnNew : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"dt":new Date()}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed <code>new Date()</code>, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnquotedVal : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"foo":bar}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed unquoted non-native value, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnquotedKey : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed unquoted object key, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnclosedObject : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"unclosed":"object"');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed unclosed object, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnclosedArray : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('["unclosed array"');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed unclosed array, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnExtraCommaInObject : function () {
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // JS validator will allow, FF 3.1b2 native will allow. IE8 errors.
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // eval will fail in IE6-7, but pass in others
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // Trailing commas are invalid, but not a security risk, so acceptable
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"extra":"comma",}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed object with extra comma, but should have failed.","warn","TestRunner");
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith throw new Error("Parsed object with extra comma, but should have failed.");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnDoubleExtraCommaInObject : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"extra":"commas",,}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed object with two extra commas, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnExtraCommaInArray : function () {
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // Correct failure in IE6-8. FF accepts trailing commas without error
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // Trailing commas are invalid, but not a security risk, so acceptable
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('["extra","comma",]');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed array with extra comma, but should have failed.","warn","TestRunner");
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith throw new Error("Parsed array with extra comma, but should have failed.");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnDoubleExtraCommaInArray : function () {
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // Correct failure in IE6-8. FF accepts trailing commas without error
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // Trailing commas are invalid, but not a security risk, so acceptable
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('["extra","commas",,]');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed array with two extra commas, but should have failed.","warn","TestRunner");
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith throw new Error("Parsed array with two extra commas, but should have failed.");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnMissingValue : function () {
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // Correct failure in IE6-8. FF accepts trailing commas without error
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // Trailing commas are invalid, but not a security risk, so acceptable
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith var data = Y.JSON.parse('[,"<-- missing value"]');
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith Y.log("Parsed array with missing value ("+data[0]+"), but should have failed.","warn","TestRunner");
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith throw new Error("Parsed array with missing value ("+data[0]+"), but should have failed.");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnCommaAfterClose : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed comma after array close, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnValueAfterClose : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"misplaced":"value"}" after close"');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed string value after object close, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnExtraClose : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed extra closing curly brace on object, but should have failed.","warn","TestRunner");
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith Y.log(Y.JSON.stringify(data,null,4),"warn","TestLogger");
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith throw new Error("Parsed extra closing curly brace on object, but should have failed.");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnExpression : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed expression, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnZeroPrefixedNumber : function () {
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // Correct failure in IE8. FF accepts leading zeros without error
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // Leading zeros are invalid, but not a security risk, so acceptable
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed zero prefixed number, but should have failed.","warn","TestRunner");
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith throw new Error("Parsed zero prefixed number, but should have failed.");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnHex : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed hex value, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnIllegalBackslashEscape : function () {
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // Correctly fails in all but IE8's native parse.
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // The spec does not specify a limitation to the escape characters a
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // decoder supports, so either is acceptable.
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith var data = Y.JSON.parse('["illegal backslash escape: \\x15"]');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed illegal backslash escape \\x15, but should have failed.","warn","TestRunner");
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith throw new Error("Parsed illegal backslash escape \\x15, but should have failed.");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnMissingColon : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed object with missing colon, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnDoubleColon : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed double colon in object, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnCommaInsteadOfColon : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed comma in place of colon, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnColonInsteadOfComma : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed colon in place of comma, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnSingleQuote : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed single quote, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnLineBreakChar : function () {
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // FF3.1b2 currently allows linebreak chars in native implementation
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // Harmless, so permissable
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed unescaped line break character, but should have failed.","warn","TestRunner");
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith throw new Error("Parsed unescaped line break character, but should have failed.");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnMismatchedClose : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed curly brace close for array, but should have failed.","warn","TestRunner");
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith test_failOnStringifyCyclicalRef1 : true,
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith test_failOnStringifyCyclicalRef2 : true,
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith test_failOnStringifyCyclicalRef3 : true
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyNatives: function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('[true,false,null,-0.12345,"string",{"object with one member":["array with one element"]}]',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify([true,false,null,-0.12345,"string",{"object with one member":["array with one element"]}]));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyObject : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // stringify sorts the keys
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.Assert.areSame('{"one":1,"two":true,"three":false,"four":null,"five":"String with\\nnewline","six":{"nested":-0.12345}}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify({one:1,two:true,three:false,four:null,five:"String with\nnewline",six : {nested:-0.12345}}));
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith test_failOnStringifyCyclicalRef1 : function () {
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith var o = { key: 'value' };
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith // Should throw an error
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith Y.log("Stringified Object with cyclical reference, but should have failed.","warn","TestRunner");
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith test_failOnStringifyCyclicalRef2 : function () {
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith var o = [1,2,3];
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith // Should throw an error
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith Y.log("Stringified Array with cyclical reference, but should have failed.","warn","TestRunner");
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith test_failOnStringifyCyclicalRef3 : function () {
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith var o = [1,2,3,{key:"value",nest:[4,5,6,{foo:"bar"}]}];
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith o[4] = o[3].x = o[3].nest[4] =
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith o[3].nest[3].y = o[3].nest[3].z = o;
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith // Should throw an error
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith Y.log("Stringified Object with cyclical reference, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyFunction : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith functions : function (are,ignored) {},
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith arr : [ function () {} ]
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyRegex : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith regex : /are treated as objects/,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyUndefined : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith undef : undefined,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith arr : [ undefined ]
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyDate : function () {
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith // native toJSON method should be called if available
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith var d = new Date(Date.UTC(1946,6,6)),
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith ref = d.toJSON ? d.toJSON() : "1946-07-06T00:00:00Z";
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith Y.Assert.areSame('{"dt":"'+ref+'"}', Y.JSON.stringify({dt : d}));
80c2303f32a65a670907dba765dfde42dc319087Luke Smith test_stringifyFormValue : function () {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith function $(id) { return document.getElementById(id); }
80c2303f32a65a670907dba765dfde42dc319087Luke Smith var data = {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith empty_text : $('empty_text').value,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith text : $('text').value,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith unchecked_radio : $('unchecked_radio').value,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith checked_radio : $('checked_radio').value,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith unchecked_box : $('unchecked_box').value,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith checked_box : $('checked_box').value,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith empty_textarea : $('empty_textarea').value,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith textarea : $('textarea').value,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith select : $('select').value,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith multiple_select : $('multiple_select').value,
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith // Buttons commented out for now because IE reports values
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith // differently
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith //button : $('button').value,
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith //button_with_value : $('button_with_value').value,
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith //button_submit : $('button_submit').value,
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith //button_submit_with_value: $('button_submit_with_value').value,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith input_button : $('input_button').value,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith input_submit : $('input_submit').value,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith input_image : $('input_image').value
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith //'"button_submit_with_value":"submit button value",'+
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_emptyWhitelistArray : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify({foo:1,bar:[1,2,3],baz:true},[]));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('[1,true,null,{},["string",null,{}]]',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith 1,true,null,{
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith foo : false,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith bar : -0.12345
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_whitelistArray : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO"}]}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith bar : [1,2,3],
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO","baz":true}],"baz":true}',
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // REMOVED for spec compatibility
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_whitelistObject : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // (undocumented) supports an obj literal as well
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO","baz":true}],"baz":true}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }, {foo : true, baz : false})); // values ignored
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith test_falseyIndents : function () {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith Y.Assert.areSame('{"foo0":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
80c2303f32a65a670907dba765dfde42dc319087Luke Smith foo0 : [ 2, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith bar : [ 4, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith baz : [ 6, {
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith /* Commented out because FF3.5 has infinite loop bug for neg indents
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith * Fixed in FF for next version.
80c2303f32a65a670907dba765dfde42dc319087Luke Smith Y.Assert.areSame('{"foo-4":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith bar : [ 4, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith baz : [ 6, {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith },null,-4));
80c2303f32a65a670907dba765dfde42dc319087Luke Smith Y.Assert.areSame('{"foo_false":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
80c2303f32a65a670907dba765dfde42dc319087Luke Smith foo_false : [ 2, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith bar : [ 4, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith baz : [ 6, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith },null,false));
80c2303f32a65a670907dba765dfde42dc319087Luke Smith Y.Assert.areSame('{"foo_empty":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
80c2303f32a65a670907dba765dfde42dc319087Luke Smith foo_empty : [ 2, {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith bar : [ 4, {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith baz : [ 6, {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith },null,""));
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith test_indentNumber : function () {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith foo : [ 2, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith bar : [ 4, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith baz : [ 6, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith test_indentString : function () {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith foo : [ 2, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith bar : [ 4, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith baz : [ 6, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_reviver : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith var data = Y.JSON.parse(JSON_STRING, function (k,v) {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith switch (k) {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith if (typeof v === 'number') {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith return -(Math.abs(v|0));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(-1234567890, data[8].integer);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(-9876, data[8].real);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(-1, data[8].one);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(-3, data[8][" s p a c e d "][2]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("NEW ITEM", data[8][" s p a c e d "][99]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("NEW ITEM", data[8].compact[99]);
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith test_toJSON_on_object: function () {
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith Y.JSON.stringify({ toJSON: function () { return "toJSON" } }));
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith // TODO: complex object with toJSON
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith test_toJSON_on_proto: function () {
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith function A() {}
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith function B() {}
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith function C() {
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith Y.Assert.areSame('"A"', Y.JSON.stringify(new C()));
80c2303f32a65a670907dba765dfde42dc319087Luke Smith test_replacer : function () {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // replacer applies to even simple value stringifications
80c2303f32a65a670907dba765dfde42dc319087Luke Smith Y.JSON.stringify(10,function (k,v) {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith return typeof(v) === 'number' ? v * 2 : v;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // replacer is applied to every nested property as well.
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // can modify the host object en route
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // executes from the context of the key:value container
80c2303f32a65a670907dba765dfde42dc319087Luke Smith Y.Assert.areSame('{"num":2,"alpha":"ABC","obj":{"nested_num":100,"alpha":"abc"},"arr":[2,null,4]}',
80c2303f32a65a670907dba765dfde42dc319087Luke Smith toUpper: true,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith nested_num: 50,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith undef: undefined,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith arr: [1, 7, 2]
80c2303f32a65a670907dba765dfde42dc319087Luke Smith ,function (k,v) {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith var t = typeof v;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith if (k === 'change') {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // this property should then be ignored
80c2303f32a65a670907dba765dfde42dc319087Luke Smith return function () {};
80c2303f32a65a670907dba765dfde42dc319087Luke Smith } else if (k === 'ignore') {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // this property should then be ignored
80c2303f32a65a670907dba765dfde42dc319087Luke Smith return undefined;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith } else if (t === 'number') {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // undefined returned to arrays should become null
80c2303f32a65a670907dba765dfde42dc319087Luke Smith return v % 7 ? v * 2 : undefined;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // this refers to the object containing the key:value
80c2303f32a65a670907dba765dfde42dc319087Luke Smith } else if (t === 'string' && (this.toUpper)) {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // modify the object during stringification
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // replacer works in conjunction with indent
80c2303f32a65a670907dba765dfde42dc319087Luke 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}",
80c2303f32a65a670907dba765dfde42dc319087Luke Smith toUpper: true,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith nested_num: 50,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith undef: undefined,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith arr: [1, 7, 2]
80c2303f32a65a670907dba765dfde42dc319087Luke Smith ,function (k,v) {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith var t = typeof v;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith if (k === 'change') {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // this property should then be ignored
80c2303f32a65a670907dba765dfde42dc319087Luke Smith return function () {};
80c2303f32a65a670907dba765dfde42dc319087Luke Smith } else if (k === 'ignore') {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // this property should then be ignored
80c2303f32a65a670907dba765dfde42dc319087Luke Smith return undefined;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith } else if (t === 'number') {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // undefined returned to arrays should become null
80c2303f32a65a670907dba765dfde42dc319087Luke Smith return v % 7 ? v * 2 : undefined;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // this refers to the object containing the key:value
80c2303f32a65a670907dba765dfde42dc319087Luke Smith } else if (t === 'string' && (this.toUpper)) {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // modify the object during stringification
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith test_replacer_after_toJSON : function () {
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith Y.JSON.stringify({a:{ toJSON: function () { return "abc"; } } },
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith function (k,v) {
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith return typeof v === 'string' ? v.toUpperCase() : v;
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith // Date instances in ES5 have toJSON that outputs toISOString, which
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith // means the replacer should never receive a Date instance
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith var str = Y.JSON.stringify([new Date()], function (k,v) {
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith Y.Assert.areSame(-1, str.indexOf("X"), "Date incorrectly received by replacer");
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith test_replacer_returning_Date : function () {
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith var d = new Date(),
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith Y.Assert.areSame('{"dt":'+ref+',"date_from_replacer":{}}',
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith Y.JSON.stringify({ dt: d, date_from_replacer: 1 },
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith function (k,v) {
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith return typeof v === 'number' ? d : v;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_ : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smithvar yconsole = new Y.Console({
80c2303f32a65a670907dba765dfde42dc319087Luke Smith newestOnTop: false,
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith height: '580px'
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith// If native JSON is available, run the tests again without native calls
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith test_disableNative : function () {
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith Y.log("Rerunning tests without native JSON API","section","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<script type="text/javascript" src="/assets/dpSyntaxHighlighter.js"></script>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<script type="text/javascript" src="/assets/dpSyntaxHighlightExample.js"></script>