Skip to content

Commit e2be402

Browse files
First commit
0 parents  commit e2be402

File tree

4 files changed

+203
-0
lines changed

4 files changed

+203
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.project

jquery.stringifyjson.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* jQuery stringifyJSON
3+
* http://github.com/flowersinthesand/jquery-stringifyJSON
4+
*
5+
* Copyright 2011, Donghwan Kim
6+
* Licensed under the Apache License, Version 2.0
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*/
9+
// This plugin is heavily based on Douglas Crockford's reference implementation
10+
(function($) {
11+
12+
var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
13+
meta = {
14+
'\b' : '\\b',
15+
'\t' : '\\t',
16+
'\n' : '\\n',
17+
'\f' : '\\f',
18+
'\r' : '\\r',
19+
'"' : '\\"',
20+
'\\' : '\\\\'
21+
};
22+
23+
function quote(string) {
24+
return '"' + string.replace(escapable, function(a) {
25+
var c = meta[a];
26+
return typeof c === "string" ? c : "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4);
27+
}) + '"';
28+
}
29+
30+
function f(n) {
31+
return n < 10 ? "0" + n : n;
32+
}
33+
34+
function str(key, holder) {
35+
var i, v, len, partial, value = holder[key], type = typeof value;
36+
37+
if (value && typeof value === "object" && typeof value.toJSON === "function") {
38+
value = value.toJSON(key);
39+
type = typeof value;
40+
}
41+
42+
switch (type) {
43+
case "string":
44+
return quote(value);
45+
case "number":
46+
return isFinite(value) ? String(value) : "null";
47+
case "boolean":
48+
return String(value);
49+
case "object":
50+
if (!value) {
51+
return "null";
52+
}
53+
54+
switch (Object.prototype.toString.call(value)) {
55+
case "[object Date]":
56+
return isFinite(value.valueOf()) ? '"' + value.getUTCFullYear() + "-" + f(value.getUTCMonth() + 1) + "-" + f(value.getUTCDate()) + "T" +
57+
f(value.getUTCHours()) + ":" + f(value.getUTCMinutes()) + ":" + f(value.getUTCSeconds()) + "Z" + '"' : "null";
58+
case "[object Array]":
59+
len = value.length;
60+
partial = [];
61+
for (i = 0; i < len; i++) {
62+
partial.push(str(i, value) || "null");
63+
}
64+
65+
return "[" + partial.join(",") + "]";
66+
default:
67+
partial = [];
68+
for (i in value) {
69+
if (Object.prototype.hasOwnProperty.call(value, i)) {
70+
v = str(i, value);
71+
if (v) {
72+
partial.push(quote(i) + ":" + v);
73+
}
74+
}
75+
}
76+
77+
return "{" + partial.join(",") + "}";
78+
}
79+
}
80+
}
81+
82+
$.stringifyJSON = function(value) {
83+
if (window.JSON && window.JSON.stringify) {
84+
return window.JSON.stringify(value);
85+
}
86+
87+
return str("", {"": value});
88+
};
89+
90+
}(jQuery));

test/index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>jQuery stringifyJSON Test Suite</title>
6+
<link rel="stylesheet" media="screen" href="http://code.jquery.com/qunit/qunit-git.css" />
7+
<script type="text/javascript" src="http://code.jquery.com/qunit/qunit-git.js"></script>
8+
<script type="text/javascript" src="http://code.jquery.com/jquery-git.js"></script>
9+
<script type="text/javascript" src="../jquery.stringifyjson.js"></script>
10+
<script type="text/javascript" src="test.js"></script>
11+
</head>
12+
<body>
13+
<h1 id="qunit-header">jQuery stringifyJSON Test Suite</h1>
14+
<h2 id="qunit-banner"></h2>
15+
<div id="qunit-testrunner-toolbar"></div>
16+
<h2 id="qunit-userAgent"></h2>
17+
<ol id="qunit-tests"></ol>
18+
</body>
19+
</html>

test/test.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
function setup() {
2+
var i;
3+
4+
this.JSON = window.JSON;
5+
window.JSON = undefined;
6+
7+
this.toJSON = {
8+
Boolean: Boolean.prototype.toJSON,
9+
Number: Number.prototype.toJSON,
10+
String: String.prototype.toJSON,
11+
Function: Function.prototype.toJSON,
12+
Array: Array.prototype.toJSON,
13+
Date: Date.prototype.toJSON,
14+
RegExp: RegExp.prototype.toJSON,
15+
Object: Object.prototype.toJSON
16+
};
17+
18+
for (i in this.toJSON) {
19+
if (Object.prototype.hasOwnProperty.call(this.toJSON, i)) {
20+
window[i].prototype.toJSON = undefined;
21+
}
22+
}
23+
}
24+
25+
function teardown() {
26+
var i;
27+
28+
window.JSON = this.JSON;
29+
for (i in this.toJSON) {
30+
if (Object.prototype.hasOwnProperty.call(this.toJSON, i)) {
31+
window[i].prototype.toJSON = this.toJSON[i];
32+
}
33+
}
34+
}
35+
36+
module("jQuery.stringifyJSON", {setup: setup, teardown: teardown});
37+
38+
$.each({
39+
chars: {
40+
value: ["안녕", "Güete Tag", "你好", "Hello"],
41+
answer: ['"안녕"', '"Güete Tag"', '"你好"', '"Hello"']
42+
},
43+
char: {
44+
value: ['\"', '\\', '\/', '\b', '\f', '\n', '\r', '\t'],
45+
answer: ['"\\""', '"\\\\"', '"/"', '"\\b"', '"\\f"', '"\\n"', '"\\r"', '"\\t"']
46+
},
47+
nonfinite: {
48+
value: [NaN, Infinity, -NaN, -Infinity],
49+
answer: ["null", "null", "null", "null"]
50+
},
51+
zero: {
52+
value: [0],
53+
answer: ["0"]
54+
},
55+
integer: {
56+
value: [1, -2, 3, -4, 5, -6, 7, -8, 9],
57+
answer: ["1", "-2", "3", "-4", "5", "-6", "7", "-8", "9"]
58+
},
59+
fractional: {
60+
value: [0.1, -0.2, 0.3, -0.4, 0.5, -0.6, 0.7, -0.8, 0.9],
61+
answer: ["0.1", "-0.2", "0.3", "-0.4", "0.5", "-0.6", "0.7", "-0.8", "0.9"]
62+
},
63+
exponential: {
64+
value: [1e+20, -1E+20, 1e+21, -1E+21, 1e21, -1E21, 1e-6, -1E-6, 1e-7, -1E-7],
65+
answer: ["100000000000000000000", "-100000000000000000000", "1e+21", "-1e+21", "1e+21", "-1e+21", "0.000001", "-0.000001", "1e-7", "-1e-7"]
66+
},
67+
boolean: {
68+
value: [true, false],
69+
answer: ["true", "false"]
70+
},
71+
date: {
72+
value: [new Date(1322990436623)],
73+
answer: ['"2011-12-04T09:20:36Z"']
74+
},
75+
misc: {
76+
value: [null, undefined, $.noop, /regexp/, {toJSON: function() {return "?";}}],
77+
answer: ["null", undefined, undefined, "{}", '"?"']
78+
},
79+
array: {
80+
value: [[], ["array", 'a', NaN, 0, true, null, undefined, []]],
81+
answer: ["[]", '["array","a",null,0,true,null,null,[]]']
82+
},
83+
object: {
84+
value: [{}, {string: "array", "NaN": NaN, zero: 0, boolean: true, "null": null, "undefined": undefined, array: [], nested: {}, fn: $.noop}],
85+
answer: ["{}", '{"string":"array","NaN":null,"zero":0,"boolean":true,"null":null,"array":[],"nested":{}}']
86+
}
87+
}, function(testName, data) {
88+
test(testName, function() {
89+
$.each(data.value, function(i, val) {
90+
deepEqual($.stringifyJSON(val), data.answer[i]);
91+
});
92+
});
93+
});

0 commit comments

Comments
 (0)