json.html revision a87b7acef05cfdc407cb96d1f42a0e533a699a80
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<!doctype html>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<html>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<head>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith <title>Test Page</title>
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 h1 {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith font: normal 125%/1.4 Arial, sans-serif;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .yui-skin-sam .yui-console .yui-console-content {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith font-size: 10px;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .yui-skin-sam .yui-console-entry-pass .yui-console-entry-cat {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith background: #070;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith color: #fff;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .yui-skin-sam .yui-console-entry-fail .yui-console-entry-cat {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith background: #700;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith color: #fff;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .highlight-example {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith display: inline;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith float: left;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith width: 650px;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith .highlight-example h2 {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith display: none;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
b0cfaf8e62a833866fd861fe7ff60747ba997be0Luke Smith .yui-skin-sam .yui-console-entry-time {
b0cfaf8e62a833866fd861fe7ff60747ba997be0Luke Smith display: none;
b0cfaf8e62a833866fd861fe7ff60747ba997be0Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith </style>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith</head>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<body class="yui-skin-sam">
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<h1>Tests</h1>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<div id="testbed"></div>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith<form action=''>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <h3>Form used for field value extraction, stringification</h3>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <input type="text" id="empty_text">
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <input type="text" id="text" value="text">
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 <textarea id="empty_textarea"></textarea>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <textarea id="textarea">textarea</textarea>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <select id="select">
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <option value="unselected">Unselected</option>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <option value="selected" selected="selected">Selected</option>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith </select>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <select id="multiple_select" multiple="multiple" size="3">
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <option value="unselected">Unselected</option>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <option value="selected" selected="selected">Selected</option>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith <option value="selected also" selected="selected">Selected also</option>
80c2303f32a65a670907dba765dfde42dc319087Luke Smith </select>
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">
80c2303f32a65a670907dba765dfde42dc319087Luke Smith</form>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
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<script type="text/javascript">
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke SmithYUI({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith base : '/build/',
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 Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smithvar d = document,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith suite = new Y.Test.Suite("JSON Tests"),
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith JSON_STRING;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke SmithJSON_STRING = '[\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "JSON Test Pattern pass1",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith {"object with 1 member":["array with 1 element"]},\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith {},\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith [],\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith -42,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith true,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith false,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith null,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith {\
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 "zero": 0,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "one": 1,\
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 "url": "http://www.JSON.org/",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "comment": "// /* <!-- --",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "# -- --> */": " ",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith " s p a c e d " :[1,2 , 3\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith\
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": "&#34; \\u0022 %22 0x22 034 &#x22;",\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "\\/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?" : "A key can be any string"\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith 0.5 ,98.6\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith99.44\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith1066,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith1e1,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith0.1e1,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith1e-1,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith1e00,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith2e+00,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith2e-00,\
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith"rosebud"]';
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
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 Smithsuite.add(new Y.Test.Case({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith name : "parse",
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith _should : {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith error : {
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 }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_emptyObject : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isObject(Y.JSON.parse("{}"));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_emptyArray : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isObject(Y.JSON.parse("[]"));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_JSONNatives : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // Note: backslashes are double escaped to emulate string returned from
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // server.
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.isObject(data);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isObject(data.obj);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isArray(data.arr);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isNull(data.n);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(false,data.f);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(true,data.t);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(12345,data.int);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(1.2345,data.fl);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("String\nwith\tescapes",data.str);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_basics : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith var data = Y.JSON.parse(JSON_STRING);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isArray(data);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(20,data.length);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isString(data[0]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isObject(data[1]);
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.isNumber(data[4]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isBoolean(data[5]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isBoolean(data[6]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isNull(data[7]);
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
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
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("&#34; \u0022 %22 0x22 034 &#x22;",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
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(0.5,data[9]);
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(1e1,data[13]);
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]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
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(true,Y.JSON.parse('true'));
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith //Y.Assert.areSame(12345,Y.JSON.parse("12345"));
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith //Y.Assert.areSame(1.2345,Y.JSON.parse("1.2345"));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(null,Y.JSON.parse("null"));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnEmptyString : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isString(Y.JSON.parse(""));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed empty string, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
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 },
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 },
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 },
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 },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnUnquotedKey : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{foo:1}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed unquoted object key, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnCommaAfterClose : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('["comma","after","close"],');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed comma after array close, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
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 },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnExtraClose : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith var data = Y.JSON.parse('{"foo":1}}');
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 },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnExpression : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"foo":1+2}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed expression, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
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.JSON.parse('{"foo":01}');
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 },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnHex : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"foo":0x14}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed hex value, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
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 },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnMissingColon : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"foo" null}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed object with missing colon, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnDoubleColon : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"foo"::1}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed double colon in object, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnCommaInsteadOfColon : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('{"foo",1}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed comma in place of colon, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnColonInsteadOfComma : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('["colon instead of":"comma"]');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed colon in place of comma, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnSingleQuote : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse("{'foo':1}");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed single quote, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnLineBreakChar : function () {
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // FF3.1b2 currently allows linebreak chars in native implementation
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith // Harmless, so permissable
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse("[\"line\nbreak\"]");
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 },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_failOnMismatchedClose : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith // parse should throw an error
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.parse('["mismatched"}');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.log("Parsed curly brace close for array, but should have failed.","warn","TestRunner");
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith}));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smithsuite.add(new Y.Test.Case({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith name : "stringify",
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith _should : {
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith error : {
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith test_failOnStringifyCyclicalRef1 : true,
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith test_failOnStringifyCyclicalRef2 : true,
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith test_failOnStringifyCyclicalRef3 : true
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith }
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith },
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith
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 },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
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}}));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith test_failOnStringifyCyclicalRef1 : function () {
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith var o = { key: 'value' };
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith o.recurse = o;
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith // Should throw an error
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith Y.JSON.stringify(o);
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith Y.log("Stringified Object with cyclical reference, but should have failed.","warn","TestRunner");
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith },
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith test_failOnStringifyCyclicalRef2 : function () {
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith var o = [1,2,3];
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith o[3] = o;
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith // Should throw an error
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith Y.JSON.stringify(o);
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith Y.log("Stringified Array with cyclical reference, but should have failed.","warn","TestRunner");
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith },
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith
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;
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith // Should throw an error
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith Y.JSON.stringify(o);
9ec1231716824e974ee4e5f1ebc143f32802fa7bLuke Smith Y.log("Stringified Object with cyclical reference, but should have failed.","warn","TestRunner");
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith },
43b5d5546545f891acfcdc4960557d9028fb1109Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyFunction : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('{"arr":[null]}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith functions : function (are,ignored) {},
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith arr : [ function () {} ]
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyRegex : function () {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.Assert.areSame('{"regex":{},"arr":[{}]}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith regex : /are treated as objects/,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith arr : [ new RegExp("in array") ]
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_stringifyUndefined : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('{"arr":[null]}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith undef : undefined,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith arr : [ undefined ]
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
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
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith Y.Assert.areSame('{"dt":"'+ref+'"}', Y.JSON.stringify({dt : d}));
80c2303f32a65a670907dba765dfde42dc319087Luke Smith },
80c2303f32a65a670907dba765dfde42dc319087Luke Smith
80c2303f32a65a670907dba765dfde42dc319087Luke Smith test_stringifyFormValue : function () {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith function $(id) { return document.getElementById(id); }
80c2303f32a65a670907dba765dfde42dc319087Luke Smith
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
80c2303f32a65a670907dba765dfde42dc319087Luke Smith };
80c2303f32a65a670907dba765dfde42dc319087Luke Smith
80c2303f32a65a670907dba765dfde42dc319087Luke Smith Y.Assert.areSame('{'+
80c2303f32a65a670907dba765dfde42dc319087Luke Smith '"empty_text":"",'+
80c2303f32a65a670907dba765dfde42dc319087Luke Smith '"text":"text",'+
80c2303f32a65a670907dba765dfde42dc319087Luke Smith '"unchecked_radio":"unchecked",'+
80c2303f32a65a670907dba765dfde42dc319087Luke Smith '"checked_radio":"radio",'+
80c2303f32a65a670907dba765dfde42dc319087Luke Smith '"unchecked_box":"unchecked",'+
80c2303f32a65a670907dba765dfde42dc319087Luke Smith '"checked_box":"box",'+
80c2303f32a65a670907dba765dfde42dc319087Luke Smith '"empty_textarea":"",'+
80c2303f32a65a670907dba765dfde42dc319087Luke Smith '"textarea":"textarea",'+
80c2303f32a65a670907dba765dfde42dc319087Luke Smith '"select":"selected",'+
80c2303f32a65a670907dba765dfde42dc319087Luke Smith '"multiple_select":"selected",'+
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith //'"button":"",'+
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith //'"button_with_value":"button value",'+
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith //'"button_submit":"",'+
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith //'"button_submit_with_value":"submit button value",'+
80c2303f32a65a670907dba765dfde42dc319087Luke Smith '"input_button":"input button",'+
80c2303f32a65a670907dba765dfde42dc319087Luke Smith '"input_submit":"input submit",'+
80c2303f32a65a670907dba765dfde42dc319087Luke Smith '"input_image":"input image"}',
80c2303f32a65a670907dba765dfde42dc319087Luke Smith Y.JSON.stringify(data));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith}));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smithsuite.add(new Y.Test.Case({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith name : "whitelist",
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_emptyWhitelistArray : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('{}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify({foo:1,bar:[1,2,3],baz:true},[]));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('[1,true,null,{},["string",null,{}]]',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify([
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith 1,true,null,{
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith foo : false,
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith bar : -0.12345
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },["string",undefined,/some regex/]
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith ],[]));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_whitelistArray : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO"}]}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith foo : [
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith 1,2,3,{
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith foo : "FOO",
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith baz : true
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith ],
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith bar : [1,2,3],
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith baz : true
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },["foo"]));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.Assert.areSame('{"foo":[1,2,3,{"foo":"FOO","baz":true}],"baz":true}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith foo : [
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith 1,2,3,{
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith foo : "FOO",
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith baz : true
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith ],
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith bar : [
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith 1,2,3,{
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith foo : "FOO",
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith baz : true
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith ],
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith baz : true},["foo","baz"]));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
80c2303f32a65a670907dba765dfde42dc319087Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
80c2303f32a65a670907dba765dfde42dc319087Luke Smith /*
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 Y.JSON.stringify({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith foo : [
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith 1,2,3,{
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith foo : "FOO",
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith baz : true
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith ],
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith bar : [
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith 1,2,3,{
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith foo : "FOO",
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith baz : true
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith ],
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith baz : true
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }, {foo : true, baz : false})); // values ignored
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
80c2303f32a65a670907dba765dfde42dc319087Luke Smith */
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith}));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smithsuite.add(new Y.Test.Case({
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith name : "formatting",
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith test_falseyIndents : function () {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith Y.Assert.areSame('{"foo0":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.JSON.stringify({
80c2303f32a65a670907dba765dfde42dc319087Luke Smith foo0 : [ 2, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith bar : [ 4, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith baz : [ 6, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "deep enough" : 7
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }]
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }]
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }]
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith },null,0));
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith
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 Y.JSON.stringify({
80c2303f32a65a670907dba765dfde42dc319087Luke Smith "foo-4" : [ 2, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith bar : [ 4, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith baz : [ 6, {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith "deep enough" : 7
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }]
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }]
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }]
80c2303f32a65a670907dba765dfde42dc319087Luke Smith },null,-4));
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith */
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith
80c2303f32a65a670907dba765dfde42dc319087Luke Smith Y.Assert.areSame('{"foo_false":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.JSON.stringify({
80c2303f32a65a670907dba765dfde42dc319087Luke Smith foo_false : [ 2, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith bar : [ 4, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith baz : [ 6, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith "deep enough" : 7
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith }]
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }]
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }]
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith },null,false));
80c2303f32a65a670907dba765dfde42dc319087Luke Smith
80c2303f32a65a670907dba765dfde42dc319087Luke Smith Y.Assert.areSame('{"foo_empty":[2,{"bar":[4,{"baz":[6,{"deep enough":7}]}]}]}',
80c2303f32a65a670907dba765dfde42dc319087Luke Smith Y.JSON.stringify({
80c2303f32a65a670907dba765dfde42dc319087Luke Smith foo_empty : [ 2, {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith bar : [ 4, {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith baz : [ 6, {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith "deep enough" : 7
80c2303f32a65a670907dba765dfde42dc319087Luke Smith }]
80c2303f32a65a670907dba765dfde42dc319087Luke Smith }]
80c2303f32a65a670907dba765dfde42dc319087Luke Smith }]
80c2303f32a65a670907dba765dfde42dc319087Luke Smith },null,""));
80c2303f32a65a670907dba765dfde42dc319087Luke Smith
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith },
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith test_indentNumber : function () {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.Assert.areSame("{\n\
80c2303f32a65a670907dba765dfde42dc319087Luke Smith \"foo\": [\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith 2,\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith {\n\
80c2303f32a65a670907dba765dfde42dc319087Luke Smith \"bar\": [\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith 4,\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith {\n\
80c2303f32a65a670907dba765dfde42dc319087Luke Smith \"baz\": [\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith 6,\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith {\n\
80c2303f32a65a670907dba765dfde42dc319087Luke Smith \"deep enough\": 7\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith }\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith ]\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith }\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith ]\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith }\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith ]\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith}",
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.JSON.stringify({
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith foo : [ 2, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith bar : [ 4, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith baz : [ 6, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith "deep enough" : 7
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith }]
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith }]
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith }]
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith },null,2));
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith },
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith test_indentString : function () {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.Assert.areSame("{\n\
80c2303f32a65a670907dba765dfde42dc319087Luke SmithXo\"foo\": [\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke SmithXoXo2,\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke SmithXoXo{\n\
80c2303f32a65a670907dba765dfde42dc319087Luke SmithXoXoXo\"bar\": [\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke SmithXoXoXoXo4,\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke SmithXoXoXoXo{\n\
80c2303f32a65a670907dba765dfde42dc319087Luke SmithXoXoXoXoXo\"baz\": [\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke SmithXoXoXoXoXoXo6,\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke SmithXoXoXoXoXoXo{\n\
80c2303f32a65a670907dba765dfde42dc319087Luke SmithXoXoXoXoXoXoXo\"deep enough\": 7\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke SmithXoXoXoXoXoXo}\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke SmithXoXoXoXoXo]\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke SmithXoXoXoXo}\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke SmithXoXoXo]\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke SmithXoXo}\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke SmithXo]\n\
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith}",
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith Y.JSON.stringify({
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith foo : [ 2, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith bar : [ 4, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith baz : [ 6, {
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith "deep enough" : 7
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith }]
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith }]
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith }]
32189201db6958c385f2d9e706b0639b6f2057d2Luke Smith },null,"Xo"));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith}));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smithsuite.add(new Y.Test.Case({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith name : "reviver",
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_reviver : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith var data = Y.JSON.parse(JSON_STRING, function (k,v) {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith switch (k) {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith case "alpha" : return "LOWER CASE";
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith case "ALPHA" : return "upper case";
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith case "true" :
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith case "false" :
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith case "null" : return undefined;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith if (typeof v === 'number') {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith return -(Math.abs(v|0));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith if (Y.Lang.isArray(v)) {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith v[99] = "NEW ITEM";
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith return v;
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith });
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("LOWER CASE", data[8].alpha);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("upper case", data[8].ALPHA);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isUndefined(data[8]["true"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isUndefined(data[8]["false"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.isUndefined(data[8]["null"]);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame(-42, data[4]);
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
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame("NEW ITEM", data[99]);
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]);
e98c881d5075aeee5eaaecb155d7a70c3035b300Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith}));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smithsuite.add(new Y.Test.Case({
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith name : "toJSON",
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith test_toJSON_on_object: function () {
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith Y.Assert.areSame('"toJSON"',
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith Y.JSON.stringify({ toJSON: function () { return "toJSON" } }));
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith // TODO: complex object with toJSON
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith },
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith test_toJSON_on_proto: function () {
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith function A() {}
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith A.prototype.toJSON = function () { return "A" };
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith function B() {}
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith B.prototype = new A();
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith function C() {
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith this.x = "X";
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith this.y = "Y";
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith this.z = "Z";
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith }
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith C.prototype = new B();
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith Y.Assert.areSame('"A"', Y.JSON.stringify(new C()));
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith }
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith}));
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smithsuite.add(new Y.Test.Case({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith name : "replacer",
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
80c2303f32a65a670907dba765dfde42dc319087Luke Smith test_replacer : function () {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith // replacer applies to even simple value stringifications
80c2303f32a65a670907dba765dfde42dc319087Luke Smith Y.Assert.areSame("20",
80c2303f32a65a670907dba765dfde42dc319087Luke Smith Y.JSON.stringify(10,function (k,v) {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith return typeof(v) === 'number' ? v * 2 : v;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith }));
80c2303f32a65a670907dba765dfde42dc319087Luke Smith
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 Y.JSON.stringify({
80c2303f32a65a670907dba765dfde42dc319087Luke Smith num: 1,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith alpha: "abc",
80c2303f32a65a670907dba765dfde42dc319087Luke Smith ignore: "me",
80c2303f32a65a670907dba765dfde42dc319087Luke Smith change: "to a function",
80c2303f32a65a670907dba765dfde42dc319087Luke Smith toUpper: true,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith obj: {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith nested_num: 50,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith undef: undefined,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith alpha: "abc"
80c2303f32a65a670907dba765dfde42dc319087Luke Smith },
80c2303f32a65a670907dba765dfde42dc319087Luke Smith arr: [1, 7, 2]
80c2303f32a65a670907dba765dfde42dc319087Luke Smith }
80c2303f32a65a670907dba765dfde42dc319087Luke Smith ,function (k,v) {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith var t = typeof v;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith
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
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 delete this.toUpper;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith return v.toUpperCase();
80c2303f32a65a670907dba765dfde42dc319087Luke Smith } else {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith return v;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith }
80c2303f32a65a670907dba765dfde42dc319087Luke Smith }));
80c2303f32a65a670907dba765dfde42dc319087Luke Smith
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 Y.JSON.stringify({
80c2303f32a65a670907dba765dfde42dc319087Luke Smith num: 1,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith alpha: "abc",
80c2303f32a65a670907dba765dfde42dc319087Luke Smith ignore: "me",
80c2303f32a65a670907dba765dfde42dc319087Luke Smith change: "to a function",
80c2303f32a65a670907dba765dfde42dc319087Luke Smith toUpper: true,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith obj: {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith nested_num: 50,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith undef: undefined,
80c2303f32a65a670907dba765dfde42dc319087Luke Smith alpha: "abc"
80c2303f32a65a670907dba765dfde42dc319087Luke Smith },
80c2303f32a65a670907dba765dfde42dc319087Luke Smith arr: [1, 7, 2]
80c2303f32a65a670907dba765dfde42dc319087Luke Smith }
80c2303f32a65a670907dba765dfde42dc319087Luke Smith ,function (k,v) {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith var t = typeof v;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith
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
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 delete this.toUpper;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith return v.toUpperCase();
80c2303f32a65a670907dba765dfde42dc319087Luke Smith } else {
80c2303f32a65a670907dba765dfde42dc319087Luke Smith return v;
80c2303f32a65a670907dba765dfde42dc319087Luke Smith }
80c2303f32a65a670907dba765dfde42dc319087Luke Smith },'_'));
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith },
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith test_replacer_after_toJSON : function () {
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith Y.Assert.areSame('{"a":"ABC"}',
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 }));
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith
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 return (v instanceof Date) ? "X" : v;
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith });
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith Y.Assert.areSame(-1, str.indexOf("X"), "Date incorrectly received by replacer");
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith },
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith test_replacer_returning_Date : function () {
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith var d = new Date(),
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith ref = Y.JSON.stringify(d);
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith
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;
a87b7acef05cfdc407cb96d1f42a0e533a699a80Luke Smith }));
80c2303f32a65a670907dba765dfde42dc319087Luke Smith }
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith}));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
80c2303f32a65a670907dba765dfde42dc319087Luke Smith/*
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smithsuite.add(new Y.Test.Case({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith name : "unicode",
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith test_ : function () {
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith Y.Assert.areSame();
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith },
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith}));
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith*/
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith}
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smithvar yconsole = new Y.Console({
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith contentBox:"log",
80c2303f32a65a670907dba765dfde42dc319087Luke Smith newestOnTop: false,
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith height: '580px'
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith}).render();
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith//yconsole.hideCategory('info');
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke SmithaddTests();
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith// If native JSON is available, run the tests again without native calls
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smithif (Y.JSON.useNativeParse) {
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith suite.add(new Y.Test.Case({
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith test_disableNative : function () {
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith Y.JSON.useNativeParse = false;
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith Y.JSON.useNativeStringify = false;
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith Y.log("Rerunning tests without native JSON API","section","TestRunner");
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith }
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith }));
481402e2c2b8e4d633d9c023579d0250205ccc8eLuke Smith addTests();
80c2303f32a65a670907dba765dfde42dc319087Luke Smith}
80c2303f32a65a670907dba765dfde42dc319087Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke SmithY.Test.Runner.add(suite);
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke SmithY.Test.Runner.run();
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith});
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith</script>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<script type="text/javascript" src="/assets/dpSyntaxHighlighter.js"></script>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith<script type="text/javascript" src="/assets/dpSyntaxHighlightExample.js"></script>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith</body>
a9322fe09a9ab78373155a607fc8b0d1b8f903e9Luke Smith</html>