annotate annotator_files/lib/vendor/json2.js @ 3:6356e78ccf5c

new version contains Annotator JS files to be used with FilesystemSite.
author casties
date Thu, 05 Apr 2012 19:37:27 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
1 /*
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
2 http://www.JSON.org/json2.js
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
3 2010-11-17
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
4
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
5 Public Domain.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
6
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
7 NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
8
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
9 See http://www.JSON.org/js.html
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
10
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
11
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
12 This code should be minified before deployment.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
13 See http://javascript.crockford.com/jsmin.html
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
14
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
15 USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
16 NOT CONTROL.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
17
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
18
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
19 This file creates a global JSON object containing two methods: stringify
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
20 and parse.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
21
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
22 JSON.stringify(value, replacer, space)
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
23 value any JavaScript value, usually an object or array.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
24
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
25 replacer an optional parameter that determines how object
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
26 values are stringified for objects. It can be a
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
27 function or an array of strings.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
28
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
29 space an optional parameter that specifies the indentation
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
30 of nested structures. If it is omitted, the text will
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
31 be packed without extra whitespace. If it is a number,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
32 it will specify the number of spaces to indent at each
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
33 level. If it is a string (such as '\t' or ' '),
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
34 it contains the characters used to indent at each level.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
35
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
36 This method produces a JSON text from a JavaScript value.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
37
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
38 When an object value is found, if the object contains a toJSON
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
39 method, its toJSON method will be called and the result will be
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
40 stringified. A toJSON method does not serialize: it returns the
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
41 value represented by the name/value pair that should be serialized,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
42 or undefined if nothing should be serialized. The toJSON method
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
43 will be passed the key associated with the value, and this will be
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
44 bound to the value
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
45
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
46 For example, this would serialize Dates as ISO strings.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
47
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
48 Date.prototype.toJSON = function (key) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
49 function f(n) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
50 // Format integers to have at least two digits.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
51 return n < 10 ? '0' + n : n;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
52 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
53
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
54 return this.getUTCFullYear() + '-' +
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
55 f(this.getUTCMonth() + 1) + '-' +
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
56 f(this.getUTCDate()) + 'T' +
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
57 f(this.getUTCHours()) + ':' +
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
58 f(this.getUTCMinutes()) + ':' +
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
59 f(this.getUTCSeconds()) + 'Z';
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
60 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
61
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
62 You can provide an optional replacer method. It will be passed the
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
63 key and value of each member, with this bound to the containing
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
64 object. The value that is returned from your method will be
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
65 serialized. If your method returns undefined, then the member will
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
66 be excluded from the serialization.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
67
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
68 If the replacer parameter is an array of strings, then it will be
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
69 used to select the members to be serialized. It filters the results
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
70 such that only members with keys listed in the replacer array are
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
71 stringified.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
72
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
73 Values that do not have JSON representations, such as undefined or
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
74 functions, will not be serialized. Such values in objects will be
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
75 dropped; in arrays they will be replaced with null. You can use
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
76 a replacer function to replace those with JSON values.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
77 JSON.stringify(undefined) returns undefined.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
78
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
79 The optional space parameter produces a stringification of the
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
80 value that is filled with line breaks and indentation to make it
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
81 easier to read.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
82
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
83 If the space parameter is a non-empty string, then that string will
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
84 be used for indentation. If the space parameter is a number, then
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
85 the indentation will be that many spaces.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
86
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
87 Example:
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
88
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
89 text = JSON.stringify(['e', {pluribus: 'unum'}]);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
90 // text is '["e",{"pluribus":"unum"}]'
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
91
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
92
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
93 text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
94 // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
95
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
96 text = JSON.stringify([new Date()], function (key, value) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
97 return this[key] instanceof Date ?
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
98 'Date(' + this[key] + ')' : value;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
99 });
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
100 // text is '["Date(---current time---)"]'
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
101
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
102
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
103 JSON.parse(text, reviver)
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
104 This method parses a JSON text to produce an object or array.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
105 It can throw a SyntaxError exception.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
106
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
107 The optional reviver parameter is a function that can filter and
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
108 transform the results. It receives each of the keys and values,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
109 and its return value is used instead of the original value.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
110 If it returns what it received, then the structure is not modified.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
111 If it returns undefined then the member is deleted.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
112
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
113 Example:
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
114
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
115 // Parse the text. Values that look like ISO date strings will
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
116 // be converted to Date objects.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
117
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
118 myData = JSON.parse(text, function (key, value) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
119 var a;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
120 if (typeof value === 'string') {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
121 a =
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
122 /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
123 if (a) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
124 return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
125 +a[5], +a[6]));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
126 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
127 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
128 return value;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
129 });
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
130
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
131 myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
132 var d;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
133 if (typeof value === 'string' &&
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
134 value.slice(0, 5) === 'Date(' &&
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
135 value.slice(-1) === ')') {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
136 d = new Date(value.slice(5, -1));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
137 if (d) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
138 return d;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
139 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
140 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
141 return value;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
142 });
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
143
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
144
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
145 This is a reference implementation. You are free to copy, modify, or
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
146 redistribute.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
147 */
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
148
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
149 /*jslint evil: true, strict: false, regexp: false */
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
150
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
151 /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
152 call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
153 getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
154 lastIndex, length, parse, prototype, push, replace, slice, stringify,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
155 test, toJSON, toString, valueOf
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
156 */
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
157
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
158
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
159 // Create a JSON object only if one does not already exist. We create the
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
160 // methods in a closure to avoid creating global variables.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
161
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
162 if (!this.JSON) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
163 this.JSON = {};
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
164 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
165
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
166 (function () {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
167 "use strict";
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
168
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
169 function f(n) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
170 // Format integers to have at least two digits.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
171 return n < 10 ? '0' + n : n;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
172 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
173
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
174 if (typeof Date.prototype.toJSON !== 'function') {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
175
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
176 Date.prototype.toJSON = function (key) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
177
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
178 return isFinite(this.valueOf()) ?
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
179 this.getUTCFullYear() + '-' +
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
180 f(this.getUTCMonth() + 1) + '-' +
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
181 f(this.getUTCDate()) + 'T' +
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
182 f(this.getUTCHours()) + ':' +
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
183 f(this.getUTCMinutes()) + ':' +
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
184 f(this.getUTCSeconds()) + 'Z' : null;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
185 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
186
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
187 String.prototype.toJSON =
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
188 Number.prototype.toJSON =
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
189 Boolean.prototype.toJSON = function (key) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
190 return this.valueOf();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
191 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
192 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
193
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
194 var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
195 escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
196 gap,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
197 indent,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
198 meta = { // table of character substitutions
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
199 '\b': '\\b',
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
200 '\t': '\\t',
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
201 '\n': '\\n',
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
202 '\f': '\\f',
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
203 '\r': '\\r',
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
204 '"' : '\\"',
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
205 '\\': '\\\\'
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
206 },
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
207 rep;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
208
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
209
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
210 function quote(string) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
211
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
212 // If the string contains no control characters, no quote characters, and no
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
213 // backslash characters, then we can safely slap some quotes around it.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
214 // Otherwise we must also replace the offending characters with safe escape
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
215 // sequences.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
216
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
217 escapable.lastIndex = 0;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
218 return escapable.test(string) ?
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
219 '"' + string.replace(escapable, function (a) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
220 var c = meta[a];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
221 return typeof c === 'string' ? c :
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
222 '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
223 }) + '"' :
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
224 '"' + string + '"';
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
225 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
226
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
227
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
228 function str(key, holder) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
229
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
230 // Produce a string from holder[key].
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
231
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
232 var i, // The loop counter.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
233 k, // The member key.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
234 v, // The member value.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
235 length,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
236 mind = gap,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
237 partial,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
238 value = holder[key];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
239
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
240 // If the value has a toJSON method, call it to obtain a replacement value.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
241
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
242 if (value && typeof value === 'object' &&
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
243 typeof value.toJSON === 'function') {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
244 value = value.toJSON(key);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
245 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
246
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
247 // If we were called with a replacer function, then call the replacer to
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
248 // obtain a replacement value.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
249
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
250 if (typeof rep === 'function') {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
251 value = rep.call(holder, key, value);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
252 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
253
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
254 // What happens next depends on the value's type.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
255
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
256 switch (typeof value) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
257 case 'string':
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
258 return quote(value);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
259
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
260 case 'number':
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
261
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
262 // JSON numbers must be finite. Encode non-finite numbers as null.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
263
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
264 return isFinite(value) ? String(value) : 'null';
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
265
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
266 case 'boolean':
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
267 case 'null':
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
268
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
269 // If the value is a boolean or null, convert it to a string. Note:
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
270 // typeof null does not produce 'null'. The case is included here in
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
271 // the remote chance that this gets fixed someday.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
272
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
273 return String(value);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
274
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
275 // If the type is 'object', we might be dealing with an object or an array or
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
276 // null.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
277
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
278 case 'object':
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
279
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
280 // Due to a specification blunder in ECMAScript, typeof null is 'object',
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
281 // so watch out for that case.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
282
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
283 if (!value) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
284 return 'null';
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
285 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
286
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
287 // Make an array to hold the partial results of stringifying this object value.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
288
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
289 gap += indent;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
290 partial = [];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
291
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
292 // Is the value an array?
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
293
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
294 if (Object.prototype.toString.apply(value) === '[object Array]') {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
295
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
296 // The value is an array. Stringify every element. Use null as a placeholder
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
297 // for non-JSON values.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
298
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
299 length = value.length;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
300 for (i = 0; i < length; i += 1) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
301 partial[i] = str(i, value) || 'null';
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
302 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
303
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
304 // Join all of the elements together, separated with commas, and wrap them in
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
305 // brackets.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
306
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
307 v = partial.length === 0 ? '[]' :
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
308 gap ? '[\n' + gap +
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
309 partial.join(',\n' + gap) + '\n' +
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
310 mind + ']' :
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
311 '[' + partial.join(',') + ']';
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
312 gap = mind;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
313 return v;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
314 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
315
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
316 // If the replacer is an array, use it to select the members to be stringified.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
317
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
318 if (rep && typeof rep === 'object') {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
319 length = rep.length;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
320 for (i = 0; i < length; i += 1) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
321 k = rep[i];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
322 if (typeof k === 'string') {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
323 v = str(k, value);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
324 if (v) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
325 partial.push(quote(k) + (gap ? ': ' : ':') + v);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
326 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
327 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
328 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
329 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
330
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
331 // Otherwise, iterate through all of the keys in the object.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
332
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
333 for (k in value) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
334 if (Object.hasOwnProperty.call(value, k)) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
335 v = str(k, value);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
336 if (v) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
337 partial.push(quote(k) + (gap ? ': ' : ':') + v);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
338 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
339 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
340 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
341 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
342
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
343 // Join all of the member texts together, separated with commas,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
344 // and wrap them in braces.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
345
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
346 v = partial.length === 0 ? '{}' :
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
347 gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
348 mind + '}' : '{' + partial.join(',') + '}';
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
349 gap = mind;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
350 return v;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
351 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
352 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
353
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
354 // If the JSON object does not yet have a stringify method, give it one.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
355
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
356 if (typeof JSON.stringify !== 'function') {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
357 JSON.stringify = function (value, replacer, space) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
358
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
359 // The stringify method takes a value and an optional replacer, and an optional
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
360 // space parameter, and returns a JSON text. The replacer can be a function
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
361 // that can replace values, or an array of strings that will select the keys.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
362 // A default replacer method can be provided. Use of the space parameter can
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
363 // produce text that is more easily readable.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
364
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
365 var i;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
366 gap = '';
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
367 indent = '';
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
368
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
369 // If the space parameter is a number, make an indent string containing that
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
370 // many spaces.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
371
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
372 if (typeof space === 'number') {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
373 for (i = 0; i < space; i += 1) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
374 indent += ' ';
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
375 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
376
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
377 // If the space parameter is a string, it will be used as the indent string.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
378
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
379 } else if (typeof space === 'string') {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
380 indent = space;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
381 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
382
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
383 // If there is a replacer, it must be a function or an array.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
384 // Otherwise, throw an error.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
385
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
386 rep = replacer;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
387 if (replacer && typeof replacer !== 'function' &&
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
388 (typeof replacer !== 'object' ||
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
389 typeof replacer.length !== 'number')) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
390 throw new Error('JSON.stringify');
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
391 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
392
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
393 // Make a fake root object containing our value under the key of ''.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
394 // Return the result of stringifying the value.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
395
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
396 return str('', {'': value});
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
397 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
398 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
399
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
400
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
401 // If the JSON object does not yet have a parse method, give it one.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
402
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
403 if (typeof JSON.parse !== 'function') {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
404 JSON.parse = function (text, reviver) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
405
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
406 // The parse method takes a text and an optional reviver function, and returns
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
407 // a JavaScript value if the text is a valid JSON text.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
408
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
409 var j;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
410
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
411 function walk(holder, key) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
412
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
413 // The walk method is used to recursively walk the resulting structure so
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
414 // that modifications can be made.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
415
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
416 var k, v, value = holder[key];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
417 if (value && typeof value === 'object') {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
418 for (k in value) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
419 if (Object.hasOwnProperty.call(value, k)) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
420 v = walk(value, k);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
421 if (v !== undefined) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
422 value[k] = v;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
423 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
424 delete value[k];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
425 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
426 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
427 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
428 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
429 return reviver.call(holder, key, value);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
430 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
431
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
432
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
433 // Parsing happens in four stages. In the first stage, we replace certain
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
434 // Unicode characters with escape sequences. JavaScript handles many characters
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
435 // incorrectly, either silently deleting them, or treating them as line endings.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
436
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
437 text = String(text);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
438 cx.lastIndex = 0;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
439 if (cx.test(text)) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
440 text = text.replace(cx, function (a) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
441 return '\\u' +
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
442 ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
443 });
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
444 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
445
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
446 // In the second stage, we run the text against regular expressions that look
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
447 // for non-JSON patterns. We are especially concerned with '()' and 'new'
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
448 // because they can cause invocation, and '=' because it can cause mutation.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
449 // But just to be safe, we want to reject all unexpected forms.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
450
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
451 // We split the second stage into 4 regexp operations in order to work around
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
452 // crippling inefficiencies in IE's and Safari's regexp engines. First we
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
453 // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
454 // replace all simple value tokens with ']' characters. Third, we delete all
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
455 // open brackets that follow a colon or comma or that begin the text. Finally,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
456 // we look to see that the remaining characters are only whitespace or ']' or
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
457 // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
458
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
459 if (/^[\],:{}\s]*$/
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
460 .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
461 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
462 .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
463
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
464 // In the third stage we use the eval function to compile the text into a
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
465 // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
466 // in JavaScript: it can begin a block or an object literal. We wrap the text
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
467 // in parens to eliminate the ambiguity.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
468
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
469 j = eval('(' + text + ')');
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
470
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
471 // In the optional fourth stage, we recursively walk the new structure, passing
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
472 // each name/value pair to a reviver function for possible transformation.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
473
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
474 return typeof reviver === 'function' ?
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
475 walk({'': j}, '') : j;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
476 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
477
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
478 // If the text is not JSON parseable, then a SyntaxError is thrown.
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
479
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
480 throw new SyntaxError('JSON.parse');
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
481 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
482 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
483 }());