# [CS] Json์˜ ์ง๋ ฌํ™”(Serialization), ์—ญ์ง๋ ฌํ™”(Deserialization)
Study Repository

[CS] Json์˜ ์ง๋ ฌํ™”(Serialization), ์—ญ์ง๋ ฌํ™”(Deserialization)

by rlaehddnd0422

Json

  • Json์€ Java-Script Object Notation์œผ๋กœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด ๋ฌธ๋ฒ•์œผ๋กœ ๊ตฌ์กฐํ™”๋œ ๋ฐ์ดํ„ฐ ๊ตํ™˜ ํ˜•์‹
  • ์ž๋ฐ”๋‚˜ ํŒŒ์ด์ฌ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋“ฑ ์—ฌ๋Ÿฌ ์–ธ์–ด์—์„œ ๋ฐ์ดํ„ฐ ๊ตํ™˜ ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

JavaScript ๊ฐ์ฒด ๋ฌธ๋ฒ•

ํ‚ค(key)์™€ ๊ฐ’(value)๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

ex) {"name" : "kim", "age" : 26}

 

์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํ‚ค์— ๋Œ€ํ•ด ์ค‘๋ณต์œผ๋กœ ๊ฐ’์„ ์„ ์–ธํ•˜๋ฉด ๋งˆ์ง€๋ง‰์— ์“ฐ์ธ ๊ฐ’์œผ๋กœ ๋ฎ์–ด ์”Œ์šด๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

// file.json
{
    "name" : "kim",
    "name" : "choi"
}
const fs = require('fs')
const path = require('path')
const file = fs.readFileSync(path.join(__dirname, "file.json"))
console.log(file)
console.log(JSON.parse(file))
  • ์œ„ file.json ํŒŒ์ผ์„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ง„์˜์—์„œ ๋ถˆ๋Ÿฌ์™€ "name"์„ ์ถœ๋ ฅํ•ด๋ณด๋ฉด "choi"๊ฐ€ ์ฐํžŒ๋‹ค๋Š” ๊ฒƒ.

 

Json ์ง๋ ฌํ™”, ์—ญ์ง๋ ฌํ™”

์ง๋ ฌํ™”์™€ ์—ญ์ง๋ ฌํ™” ๊ณผ์ •

 

  • ์ง๋ ฌํ™”(Serialization)๋ž€ ์™ธ๋ถ€ ์‹œ์Šคํ…œ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐ”์ดํŠธ ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ์ˆ ๋กœ, ์‰ฝ๊ฒŒ ๋งํ•ด ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด์˜ ์ฝ”๋“œ๋‚ด์˜ ๊ฐ์ฒด๋‚˜, ํ•ด์‹œ ํ…Œ์ด๋ธ”, ๋”•์…”๋„ˆ๋ฆฌ ๋“ฑ์„ JSON ํŒŒ์ผ๋กœ ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ ์ง๋ ฌํ™”(Serialization)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฐ˜๋Œ€๋กœ ์—ญ์ง๋ ฌํ™”(Deserialization)๋Š” JSON ํŒŒ์ผ์„ ์™ธ๋ถ€ ์‹œ์Šคํ…œ์˜ ๋ฐ”์ดํŠธ ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ(JSON)์„ ๊ฐ์ฒด๋‚˜ ํ•ด์‹œ ํ…Œ์ด๋ธ”, ๋”•์…”๋„ˆ๋ฆฌ ๋“ฑ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.
{
    "letterId" : 1,
    "reportType" : "ABUSE",
    "reportContent" : "์‹ ๊ณ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค."
}

 

@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ReportSendRequest {

    @Null
    private Long memberId;

    @NotBlank(message = "์‹ ๊ณ ํ•  ํŽธ์ง€ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
    private Long letterId;

    @NotBlank(message = "์‹ ๊ณ ์œ ํ˜•์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
    private String reportType;

    @NotBlank(message = "์‹ ๊ณ  ๋‚ด์šฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
    private String reportContent;
}

 

์œ„ ์˜ˆ์‹œ๋กœ, ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ ๊ธฐ๋ฐ˜์œผ๋กœ JSON ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  JSON ํŒŒ์ผ์„ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์ด ์—ญ์ง๋ ฌํ™”๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ JSON ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ž‘์—…์€ ํ”„๋ก ํŠธ์—”๋“œ์—์„œ ์ˆ˜ํ–‰ํ•˜๊ณ , JSON ํŒŒ์ผ์„ ์ฝ์–ด ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ž‘์—…์ธ ์—ญ์ง๋ ฌํ™” ๊ณผ์ •์€ ๋ฐฑ์—”๋“œ์—์„œ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค.

 

JSON์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด(์ž๋ฐ”, ํŒŒ์ด์ฌ) ํ”„๋ ˆ์ž„์›Œํฌ ๋“ฑ์— ๋…๋ฆฝ์ ์ด๊ธฐ ๋•Œ๋ฌธ์—, 

  1. ์„œ๋กœ ๋‹ค๋ฅธ ์‹œ์Šคํ…œ๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตํ™˜ํ•˜๊ธฐ์— ์ข‹๋‹ค๋Š” ์ ์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋ฉฐ, ์ฃผ๋กœ API์˜ ๋ฐ˜ํ™˜ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  2. (yml, xml์„ ๋” ์„ ํ˜ธํ•˜๊ธด ํ•˜์ง€๋งŒ) ์‹œ์Šคํ…œ์„ ๊ตฌ์„ฑํ•˜๋Š” ์„ค์ •ํŒŒ์ผ์— ํ™œ์šฉ๋˜๊ธฐ๋„ ํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

Java์˜ Serializable ์ธํ„ฐํŽ˜์ด์Šค

public class Student implements Serializable {
    public static final long serialVersionUID = 1234L;
 
    private long studentId;
    private String name;
    private transient int age;	// transient ๋ณ€์ˆ˜๋Š” ์ง๋ ฌํ™”์—์„œ ์ œ์™ธ
 
    public Student(long studentId, String name, int age) {
        this.studentId = studentId;
        this.name = name;
        this.age = age;
    }
    
    @Override
    public String toString() {
        return String.format("%d - %s - %d", studentId, name, age);
    }

}

// ์ถœ์ฒ˜: https://code-lab1.tistory.com/289 [์ฝ”๋“œ ์—ฐ๊ตฌ์†Œ:ํ‹ฐ์Šคํ† ๋ฆฌ]
  • ์ž๋ฐ”๋Š” Serializable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์œผ๋กœ ์ง๋ ฌํ™”/์—ญ์ง๋ ฌํ™” ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜์ง€๋งŒ, ์น˜๋ช…์ ์ธ ๋ณด์•ˆ ์ด์Šˆ์™€ ์—„๊ฒฉํ•œ ํƒ€์ž…์ฒดํฌ, ๊ฐ์ฒด ๊ตฌ์กฐ ๋ณ€๊ฒฝ์˜ ์–ด๋ ค์›€ ๋“ฑ ๋‹จ์ ์ด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ๋˜์ง€๋Š” ์•Š์ง€๋งŒ, ์ด๋Ÿฐ ๊ธฐ๋Šฅ๋„ ์ œ๊ณตํ•œ๋‹ค๋Š” ์ ์„ ์•Œ์•„๋‘ก์‹œ๋‹ค!
  • ์ค‘์š”ํ•œ ์ •๋ณด๋Š” ๋ฐฉ์–ด์ ์œผ๋กœ transient ํ‚ค์›Œ๋“œ๋ฅผ ๋„ฃ์–ด ์ง๋ ฌํ™” ๊ณผ์ •์—์„œ ํ•„๋“œ์˜ ์ƒํƒœ๊ฐ’ ์ „์†ก์„ ๋ง‰์•„ ์—ญ์ง๋ ฌํ™” ๊ณผ์ •์—์„œ ๋ณต์‚ฌํ•  ๊ฐ’ ์ž์ฒด๋ฅผ ์•„์˜ˆ ๋ณด๋‚ด์ง€ ์•Š๋„๋ก ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์ฐธ๊ณ  ์ž๋ฃŒ

 

 

[Effective Java] Item 3. private ์ƒ์„ฑ์ž๋‚˜ enum ํƒ€์ž…์œผ๋กœ ์‹ฑ๊ธ€ํ†ค์ž„์„ ๋ณด์žฅํ•˜์ž.

์‹ฑ๊ธ€ํ†ค์ด๋ž€? ์‹ฑ๊ธ€ํ†ค์ด๋ž€ ์ธ์Šคํ„ด์Šค๋ฅผ ๋‹จ 1๊ฐœ๋งŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค. ex) ๋ฌด์ƒํƒœ ๊ฐ์ฒด, ์„ค๊ณ„ ์ƒ ์œ ์ผํ•ด์•ผ ํ•˜๋Š” ์‹œ์Šคํ…œ ์ปดํฌ๋„ŒํŠธ (service, repository) ์‹ฑ๊ธ€ํ†ค ์žฅ์  ํ•œ ๋ฒˆ์˜ ๊ฐ์ฒด ์ƒ์„ฑ์œผ๋กœ

rlaehddnd0422.tistory.com

 

[Java] ์ง๋ ฌํ™”(Serialization)์™€ ์—ญ์ง๋ ฌํ™”(Deserialization)๋ž€? transient ๋ณ€์ˆ˜๋ž€?

์ง๋ ฌํ™”(Serializaion)๋ž€? ์—ญ์ง๋ ฌํ™”(Deserialization)๋ž€? ์ง๋ ฌํ™”๋Š” ๊ฐ์ฒด๋ฅผ ์ €์žฅ ๊ฐ€๋Šฅํ•œ ์ƒํƒœ(์˜ˆ๋ฅผ ๋“ค์–ด ๋””์Šคํฌ์— ํŒŒ์ผ ํ˜•ํƒœ ๋“ฑ) ํ˜น์€ ์ „์†ก ๊ฐ€๋Šฅํ•œ ์ƒํƒœ(๋„คํŠธ์›Œํฌ ์ƒ์˜ ๋ฐ์ดํ„ฐ ์ŠคํŠธ๋ฆผ ํ˜•ํƒœ)๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„

code-lab1.tistory.com

 

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ