Overview
Implement jsonStringify(value, replacer, space) that supports:
- All JSON types (string, number, boolean, null, array, object)
- Optional
replacerfunction(key, value) => newValue - Optional
spaceparameter for indentation
Constraints
- Do not use native
JSON.stringify undefined, functions → omitted from objects,nullin arrays- Handle nested structures
Examples
jsonStringify({ a: 1, b: undefined }, null, 2);
// '{\n "a": 1\n}'Solution
Reveal solution
function jsonStringify(value, replacer, space) {
const indent = typeof space === 'number' ? ' '.repeat(space) : (space || '');
function serialize(val, depth) {
if (val === null) return 'null';
if (val === undefined || typeof val === 'function') return undefined;
if (typeof val === 'boolean') return String(val);
if (typeof val === 'number') return isFinite(val) ? String(val) : 'null';
if (typeof val === 'string') return '"' + val.replace(/\\/g,'\\\\').replace(/"/g,'\\"').replace(/\n/g,'\\n') + '"';
if (Array.isArray(val)) {
if (val.length === 0) return '[]';
const items = val.map(v => { const s = serialize(v, depth+1); return s === undefined ? 'null' : s; });
if (!indent) return '[' + items.join(',') + ']';
const pad = indent.repeat(depth+1);
return '[\n' + items.map(i => pad + i).join(',\n') + '\n' + indent.repeat(depth) + ']';
}
const entries = [];
for (const [k, v] of Object.entries(val)) {
let newV = replacer ? replacer(k, v) : v;
const s = serialize(newV, depth+1);
if (s !== undefined) entries.push([k, s]);
}
if (entries.length === 0) return '{}';
if (!indent) return '{' + entries.map(([k,v]) => serialize(k,0)+':'+v).join(',') + '}';
const pad = indent.repeat(depth+1);
return '{\n' + entries.map(([k,v]) => pad + serialize(k,0)+': '+v).join(',\n') + '\n' + indent.repeat(depth) + '}';
}
let v = replacer ? replacer('', value) : value;
return serialize(v, 0);
}json-stringify-ii.js
JSON Stringify II
hardcodingJavaScriptRecursion
Overview
Implement jsonStringify(value, replacer, space) that supports:
- All JSON types (string, number, boolean, null, array, object)
- Optional
replacerfunction(key, value) => newValue - Optional
spaceparameter for indentation
Constraints
- Do not use native
JSON.stringify undefined, functions → omitted from objects,nullin arrays- Handle nested structures
Examples
jsonStringify({ a: 1, b: undefined }, null, 2);
// '{\n "a": 1\n}'Solution
Reveal solution
function jsonStringify(value, replacer, space) {
const indent = typeof space === 'number' ? ' '.repeat(space) : (space || '');
function serialize(val, depth) {
if (val === null) return 'null';
if (val === undefined || typeof val === 'function') return undefined;
if (typeof val === 'boolean') return String(val);
if (typeof val === 'number') return isFinite(val) ? String(val) : 'null';
if (typeof val === 'string') return '"' + val.replace(/\\/g,'\\\\').replace(/"/g,'\\"').replace(/\n/g,'\\n') + '"';
if (Array.isArray(val)) {
if (val.length === 0) return '[]';
const items = val.map(v => { const s = serialize(v, depth+1); return s === undefined ? 'null' : s; });
if (!indent) return '[' + items.join(',') + ']';
const pad = indent.repeat(depth+1);
return '[\n' + items.map(i => pad + i).join(',\n') + '\n' + indent.repeat(depth) + ']';
}
const entries = [];
for (const [k, v] of Object.entries(val)) {
let newV = replacer ? replacer(k, v) : v;
const s = serialize(newV, depth+1);
if (s !== undefined) entries.push([k, s]);
}
if (entries.length === 0) return '{}';
if (!indent) return '{' + entries.map(([k,v]) => serialize(k,0)+':'+v).join(',') + '}';
const pad = indent.repeat(depth+1);
return '{\n' + entries.map(([k,v]) => pad + serialize(k,0)+': '+v).join(',\n') + '\n' + indent.repeat(depth) + '}';
}
let v = replacer ? replacer('', value) : value;
return serialize(v, 0);
}NameTopicDifficulty
103 of 103 problems