본문 바로가기

Frontend/Javascript

Serialize와 Deserialize의 차이점 (feat.Primitive Type, Reference Type)

dSerialize와 Deserialize

이 둘이 계속 헷갈린다. 사실 이를 잘 안쓰기도 했고, JSON.stringify()를 보면 직렬화 하는구나~정도만 생각했지 직렬화가 뭔데?라고 설명하라고 하면 설명하지 못하는 상태였다.

 

다시금 보던 와중, https://blog.postman.com/when-and-how-to-use-json-serialization-in-postman/ 이 글을 보며 Serialize와 Deserialize에 대하여 다시 공부하게 되었다. 

 

Serializing

: store & convert complex data

메모리로부터 데이터를 읽거나 쓸 때 사용한다. 흔한 data type 사용 시에는 multiple systems 사이에서 사용한다. 

complex data structure > simpler types로 변경

 

- variable -> JSON.stringify() -> <string>

 

if)array of numbers를 string으로 변환하고 싶을때, JSON.stringify()를 사용한다.

 

let arrayOfNumbers = [1,2,3,4]; // array of Numbers 
JSON.stringify(arrayOfNumbers); 

// '[1,2,3,4,5]'

 

 

Deserializing

<string> -> JSON.parse() -> variable

 

let strubgData = "[1,2,3,4,5]"; 
JSON.parse(stringData); 

//[1,2,3,4,5]

 

추가적으로, Primitive Type은 원시타입을 말하며 정수,실수,문자,논리형과 같이 실제 데이터 값을 저장하는 Type이다.

Reference Type은 객체의 번지 참조(주소 저장)을 하며, 메모리 번지 값을 통해 객체를 참조하는 Type이다.

 

Primitive Type은 Serialized될 필요가 없지만, arrays나 obejct와 같은 complex data(위에서 말하는 Reference Type)는 변수에 저장할 경우 Serialized가 되어야 한다.