Skip to content

Instantly share code, notes, and snippets.

@padawanR0k
Last active April 14, 2021 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save padawanR0k/1a24d1bdbd2cc62edf53ae7d0d44397b to your computer and use it in GitHub Desktop.
Save padawanR0k/1a24d1bdbd2cc62edf53ae7d0d44397b to your computer and use it in GitHub Desktop.
make own JSON.stringify
// JSON.stringify를 구현하시오
// https://www.youtube.com/watch?v=rQOpmgo99BQ
/**
* 자료
* https://reference.codeproject.com/Book/javascript/reference/global_objects/json/stringify
* https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
*/
const parsingRouter = {
'string': (v) => `"${v.toString()}"`,
'number': (v) => v.toString(),
'null': () => 'null',
'undefined': (_, valueOwnerConstructor) => valueOwnerConstructor === Array ? 'null' : undefined,
'function': (_) => 'null',
'symbol': (_, valueOwnerConstructor) => valueOwnerConstructor === Array ? 'null' : undefined,
'boolean': (v) => v.toString(),
'array': (arr, valueOwnerConstructor) => {
const accumulator = [];
const func = (item, valueOwnerConstructor) => {
const arr = [];
const v = myJSON.valueToString(item, valueOwnerConstructor);
if (v) {
arr.push(`${v}`);
}
return arr;
}
for (let item of arr) {
let node = item;
if (myJSON.getType(node) === 'array') {
let i = 0;
while (i < node.length) {
accumulator.push(func(node, node.constructor));
node = node[i];
i++;
}
} else {
accumulator.push(func(item, valueOwnerConstructor))
}
}
return myJSON.wrapStr('[', ']', accumulator);
},
'object': (obj, valueOwnerConstructor) => {
const accumulator = [];
for (let key in obj) {
const v = myJSON.valueToString(obj[key], valueOwnerConstructor);
if (v) {
accumulator.push(`"${key}":${v}`);
}
}
return myJSON.wrapStr('{', '}', accumulator);
},
};
const valueRouter = {
'null': () => 'null',
'undefined': () => undefined,
'object': (v, valueOwnerConstructor) => myJSON.valueToString(v, valueOwnerConstructor),
'array': (v, valueOwnerConstructor) => myJSON.valueToString(v, valueOwnerConstructor),
'boolean': (v) => myJSON.valueToString(v, null),
'symbol': (v) => myJSON.valueToString(v, null),
'function': (v) => myJSON.valueToString(v, null),
'number': (v) => myJSON.valueToString(v, null),
'string': (v) => myJSON.valueToString(v, null),
}
class myJSON {
constructor() {
}
static getType(value) {
if (value === null) {
return ('null');
} else if (value === undefined) {
return ('undefined');
} else {
let type = typeof value;
return (type === 'object'
? Array.isArray(value)
? 'array'
: 'object'
: type);
}
}
static valueToString(value, valueOwnerConstructor) {
const valueType = myJSON.getType(value);
let str = parsingRouter[valueType](value, valueOwnerConstructor);
return str;
}
static wrapStr(start, end, str) {
return `${start}${str.join(',')}${end}`;
}
static stringify(value) {
const valueType = myJSON.getType(value);
return valueRouter[valueType](value, value ? value.constructor : null);
}
}
const test01 = {
str: 'a',
num: 1,
'undefined': undefined,
'null': null,
symbol: Symbol('test'),
boolean: true,
}
const tests = [];
tests.push({})
tests.push(1)
tests.push(null)
tests.push('foo')
tests.push([1, '1', true, null, undefined, Symbol(''), () => ''])
tests.push([])
tests.push([1, 2, ["a", [1, 2], false], 3, ["b", "c", [1, 2]]])
function testMyJSON(v) {
try {
console.log(`==== real value ====`);
console.log(v)
console.log(`==== myJSON() ====`)
console.log(myJSON.stringify(v))
console.log(`==== JSON() ====`)
console.log(JSON.stringify(v))
console.log("\n\n")
} catch (e) {
console.error(e)
}
}
tests.map(test => testMyJSON(test))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment