UNPKG

5.03 kBJavaScriptView Raw
1// Basically ripped from https://github.com/douglascrockford/JSON-js/blob/master/json2.js but added BSON support
2
3'use strict';
4
5var mongodb = require('mongodb');
6
7function f(n) {
8 // Format integers to have at least two digits.
9 return n < 10 ? '0' + n : n;
10}
11
12// JSHint warning suppression
13var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
14var escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
15var gap;
16var indent;
17var meta = { // table of character substitutions
18 '\b': '\\b',
19 '\t': '\\t',
20 '\n': '\\n',
21 '\f': '\\f',
22 '\r': '\\r',
23 '"': '\\"',
24 '\\': '\\\\',
25};
26var rep;
27
28function quote(string) {
29 escapable.lastIndex = 0;
30 return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
31 var c = meta[a];
32 return typeof c === 'string' ?
33 c :
34 '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
35 }) + '"' : '"' + string + '"';
36}
37
38function str(key, holder) {
39 var i; // The loop counter.
40 var k; // The member key.
41 var v; // The member value.
42 var length;
43 var mind = gap;
44 var partial;
45 var value = holder[key];
46
47 if (value instanceof mongodb.ObjectID) {
48 return 'ObjectId("' + value + '")';
49 } else if (value instanceof mongodb.Timestamp) {
50 return 'Timestamp(' + value.high_ + ', ' + value.low_ + ')';
51 } else if (value instanceof Date) {
52 return 'ISODate("' + value.toJSON() + '")';
53 } else if (value instanceof mongodb.DBRef) {
54 if (value.db === '') {
55 return 'DBRef("' + value.namespace + '", "' + value.oid + '")';
56 } else {
57 return 'DBRef("' + value.namespace + '", "' + value.oid + '", "' + value.db + '")';
58 }
59 } else if (value instanceof mongodb.Code) {
60 return 'Code("' + value.code + '")';
61 } else if (value instanceof mongodb.MinKey) {
62 return 'MinKey()';
63 } else if (value instanceof mongodb.MaxKey) {
64 return 'MaxKey()';
65 } else if (value instanceof mongodb.Symbol) {
66 return 'Symbol("' + value + '")';
67 }
68
69 if (value && typeof value === 'object' && typeof value.toJSON === 'function') {
70 value = value.toJSON(key);
71 }
72
73 if (typeof rep === 'function') {
74 value = rep.call(holder, key, value);
75 }
76
77 switch (typeof value) {
78 case 'string':
79 return quote(value);
80
81 case 'number':
82 return isFinite(value) ? String(value) : 'null';
83
84 case 'boolean':
85 case 'null':
86 return String(value);
87 case 'object':
88 if (!value) {
89 return 'null';
90 }
91 gap += indent;
92 partial = [];
93 if (Object.prototype.toString.apply(value) === '[object Array]') {
94 length = value.length;
95 for (i = 0; i < length; i += 1) {
96 partial[i] = str(i, value) || 'null';
97 }
98 v = partial.length === 0 ?
99 '[]' :
100 gap ?
101 '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' :
102 '[' + partial.join(',') + ']';
103 gap = mind;
104 return v;
105 }
106 if (rep && typeof rep === 'object') {
107 length = rep.length;
108 for (i = 0; i < length; i += 1) {
109 if (typeof rep[i] === 'string') {
110 k = rep[i];
111 v = str(k, value);
112 if (v) {
113 partial.push(quote(k) + (gap ? ': ' : ':') + v);
114 }
115 }
116 }
117 } else {
118 for (k in value) {
119 if (Object.prototype.hasOwnProperty.call(value, k)) {
120 v = str(k, value);
121 if (v) {
122 partial.push(quote(k) + (gap ? ': ' : ':') + v);
123 }
124 }
125 }
126 }
127 v = partial.length === 0 ?
128 '{}' :
129 gap ?
130 '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' :
131 '{' + partial.join(',') + '}';
132 gap = mind;
133 return v;
134 }
135}
136
137exports.stringify = function (value, replacer, space) {
138 var i;
139 gap = '';
140 indent = '';
141 if (typeof space === 'number') {
142 for (i = 0; i < space; i += 1) {
143 indent += ' ';
144 }
145 } else if (typeof space === 'string') {
146 indent = space;
147 }
148
149 rep = replacer;
150 if (replacer && typeof replacer !== 'function' &&
151 (typeof replacer !== 'object' ||
152 typeof replacer.length !== 'number')) {
153 throw new Error('JSON.stringify');
154 }
155 return str('', { '': value });
156};
\No newline at end of file