Jieunny์˜ ๋ธ”๋กœ๊ทธ

S3) Unit 1. [์‹ค์Šต] JSON.stringify ๋ณธ๋ฌธ

CodeStates/Training

S3) Unit 1. [์‹ค์Šต] JSON.stringify

Jieunny 2023. 2. 14. 12:00

๐Ÿ“ฃ ์žฌ๊ท€๋กœ JSON.stringify ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ

1๏ธโƒฃ Test ์กฐ๊ฑด

โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "9"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "null"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "true"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "false"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด ""Hello world""๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "[]"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "[8]"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "["hi"]"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "[8,"hi"]"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "[1,0,-1,-0.3,0.3,1343.32,3345,0.00011999]"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "[8,[[],3,4]]"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "[[[["foo"]]]]"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "{}"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "{"a":"apple"}"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "{"foo":true,"bar":false,"baz":null}"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "{"boolean, true":true,"boolean, false":false,"null":null}"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "{"a":{"b":"c"}}"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "{"a":["b","c"]}"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "[{"a":"b"},{"c":"d"}]"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด "{"a":[],"c":{},"b":true}"๋กœ ๋ณ€ํ™˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
โœ“ ํ•จ์ˆ˜์™€ undefined๋Š” stringify๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

2๏ธโƒฃ ์ž‘์„ฑ ์ฝ”๋“œ

function stringifyJSON(obj) {
  // your code goes here

  // 1. number๋‚˜ null, boolean์€ ๊ทธ๋ƒฅ String์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์ถœ๋ ฅ
  if(typeof(obj) === 'number' || obj === null ||  typeof(obj) === 'boolean') return String(obj);

  // 2. string์€ ""์”Œ์›Œ์„œ ์ถœ๋ ฅ
  else if(typeof(obj) === 'string') return `"${obj}"`;

  // 3. undefined, function ์€ stringift ํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค.
  else if(typeof(obj) === 'undefined' || typeof(obj) === 'function') return 0;

  // 4. ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ ์š”์†Œ ๋Œ๋ฉด์„œ ์š”์†Œ ๊ฐ๊ฐ์„ stringifyJSON ํ•จ์ˆ˜์— ๋‹ด์•„์„œ JSON์œผ๋กœ ๋ณ€ํ•œ ๊ฐ’์„ ๋‹ค์‹œ ๋ฐฐ์—ด์— ๋ฐ›์•„์„œ ๋ฐ˜ํ™˜
  else if(Array.isArray(obj)) {
    const answer = [];
    for(let i of obj){
      let temp = stringifyJSON(i);
      answer.push(temp);
    }
    return `[${answer}]`;
  }

  // 5. ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ๋„ ๋ฐฐ์—ด์ด๋ž‘ ๋น„์Šทํ•˜๊ฒŒ, ๋Œ€์‹  ํ‚ค/๊ฐ’์„ ๋‘˜ ๋‹ค JSON์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ค˜์•ผํ•จ.
  else if(typeof(obj) === 'object'){
    let answer = '';
    const keys = Object.keys(obj);
    const values = Object.values(obj);
    
    for(let i=0; i<keys.length; i++){
      if(typeof(values[i]) === 'undefined' || typeof(values[i]) === 'function') return '{}';
  
      let newKey = stringifyJSON(keys[i]);
      let newValue = stringifyJSON(values[i]);

      if(i !== 0) answer += ',' + newKey + ":" + newValue;
      else answer += newKey + ':' + newValue;
    }
    return `{${answer}}`;
  }

};