10 examples of 'mdn json stringify' in JavaScript

Every line of 'mdn json stringify' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
130function stringify(obj, n) {
131 n = n || 0;
132 if (n > 2) { // 遍历深度限制
133 return '...'
134 }
135
136 if (type(obj) == 'array') {
137 var str = '['
138 for (var i = 0; i < obj.length; i++) {
139 str += stringify(obj[i]) + ', ';
140 }
141 str = str.replace(/, $/, '');
142 str += ']';
143 return str;
144 }
145 if (type(obj) == 'object') {
146 var str = '{\n';
147 var _obj = {};
148 for (var i = 0; i < (obj || {}).length; i++) { // for ie
149 var item = obj[i];
150 if (item !== undefined) {
151 _obj[i] = item;
152 }
153 }
154 for (var i in obj) {
155 _obj[i] = obj[i];
156 }
157
158 var max = 10;
159 var maxI = 0;
160 for (var i in _obj) {
161 if (maxI++ > max) { // 遍历长度限制
162 str += '.';
163 continue
164 }
165 str += Array(n + 2).join(' ') + '"' + i + '": ' + stringify(_obj[i], n + 1) + ',\n';
166 }
167 str = str.replace(/,\n$/, '');
168 str += '\n' + Array(n + 1).join(' ') + '}';
169 return str;
170 }
171
172 if (typeof obj == 'string') {
173 return '<sub style="color:red">"</sub>' +
174 obj.replace(/\n/g, '<br>').replace(/</g, '&lt;').replace(/>/g, '&gt;') + '<sub style="color:red">"</sub>';
175 }
176
177 return obj + '';
178
179}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

122export function stringify(
123 nodes: Array<Ast<any>>,
124 namesToValues: { [index: string]: any },
125): string {
126 let result = "";
127
128 for (const node of nodes) {
129 switch (node.tag) {
130 case "wildcard":
131 continue;
132 case "namedWildcard":
133 case "namedSegment":
134 const value = namesToValues[node.value];
135 if (value == null) {
136 throw new Error(`no value provided for name \`${ node.value }\``);
137 }
138 result += value;
139 continue;
140 case "staticContent":
141 result += node.value;
142 continue;
143 case "optionalSegment":
144 // only add optional segments if values are present.
145 // optional segments are only included if values are provided
146 // for all names (of named segments) within the optional segment
147 if (astContainsAnySegmentsForParams(node.value, namesToValues)) {
148 // recurse into the optional segment
149 // optional segments values are always arrays
150 result += stringify(node.value, namesToValues);
151 }
152 continue;
153 default:
154 throw new Error(`unknown tag \`${ node.tag }\``);
155 }
156 }
157
158 return result;
159}
18function stringify(data) {
19 return JSON.stringify(data)
20}
288function stringify(node) {
289 const { type } = node;
290 switch (type) {
291 case 'BooleanLiteral':
292 case 'NullLiteral':
293 case 'NumericLiteral':
294 case 'StringLiteral':
295 case 'TemplateLiteral':
296 return JSON.stringify(literalValue(node));
297
298 case 'Identifier':
299 return JSON.stringify(node.name);
300
301 case 'ArrayExpression':
302 return `[${node.elements.map(stringify)}]`;
303
304 case 'ObjectExpression':
305 return `{${node.properties.map(stringify).filter(Boolean)}}`;
306
307 case 'ObjectProperty': {
308 const { key, value } = node;
309 if (t.isIdentifier(value, { name: 'undefined' })) {
310 return '';
311 }
312 return `${stringify(key)}:${stringify(value)}`;
313 }
314 }
315
316 // istanbul ignore next
317 throw new Error(`Can't handle type "${type}"`);
318}
64function ppJSON(v) { return v instanceof RegExp ? v.toString() : (typeof v == "bigint" ? v.toString() : JSON.stringify(v, null, 2)); }
8function stringify (asJson5: boolean, value: object): string {
9 return asJson5
10 ? json5.stringify(value, undefined, 2)
11 : JSON.stringify(value, undefined, 2)
12}
20export function stringify(v: any) {
21 const cache = new Map();
22 return JSON.stringify(v, function(key, value) {
23 if (typeof value === 'object' && value !== null) {
24 if (cache.has(value)) {
25 // Circular reference found, discard key
26 return;
27 }
28 // Store value in our map
29 cache.set(value, true);
30 }
31 return value;
32 });
33}
7function stringify(v) {
8 var type = typeof v;
9 switch (type) {
10 case "number":
11 return v;
12 case "boolean":
13 case "string":
14 case "function":
15 return "\"" + v + "\"";
16 default:
17 var r = [];
18 if (v instanceof Array) {
19 for ( var i = 0; i < v.length; i++) {
20 r.push(stringify(v[i]))
21 }
22 return "[" + r.join(",") + "]"
23 } else if (v instanceof Date) {
24 return v.getTime() + ""
25 } else {
26 for ( var n in v) {
27 r.push(n + ":" + stringify(v[n]))
28 }
29 return "{" + r.join(",") + "}"
30 }
31 }
32}
22function stringifyJSON(data: any): string {
23 return JSON.stringify(data, null, " ");
24}
31function prettyPrintJson(json) {
32 return JSON.stringify(json, null, ' ');
33}

Related snippets