๐Ÿ Python json vs pickle vs marshal

json

ํŒŒ์ด์ฌ ๊ฐ์ฒด๋ฅผ JSON ํ˜•์‹์œผ๋กœ ์ง๋ ฌํ™”(์ธ์ฝ”๋”ฉ) ๋˜๋Š” ์—ญ์ง๋ ฌํ™”(๋””์ฝ”๋”ฉ) ํ•˜๋Š” ๋ชจ๋“ˆ์ด๋‹ค.

json example 1

json example 2

pickle

ํŒŒ์ด์ฌ ๊ฐ์ฒด ๊ตฌ์กฐ๋ฅผ ์ง๋ ฌํ™” ๋˜๋Š” ์—ญ์ง๋ ฌํ™” ํ•˜๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ ํ”„๋กœํ† ์ฝœ์ด๋‹ค. pickling์€ ํŒŒ์ด์ฌ ๊ฐ์ฒด ๊ณ„์ธต ๊ตฌ์กฐ๊ฐ€ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ณ€ํ™˜๋˜๋Š” ์ ˆ์ฐจ์ด๋ฉฐ, unpickling์€ ๊ทธ ๋ฐ˜๋Œ€ ์—ฐ์‚ฐ์ด๋‹ค.

pickle ๋ชจ๋“ˆ์€ ์•ˆ์ „ํ•˜์ง€ ์•Š๋‹ค. ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋งŒ unpickle ํ•ด์•ผ ํ•œ๋‹ค. ์‹ ๋ขฐํ•  ์ˆ˜ ์—†๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” json ๋ชจ๋“ˆ์ด ๋” ์•ˆ์ „ํ•˜๋‹ค.

์ผ๋ฐ˜ ํ…์ŠคํŠธ๋ฅผ ํŒŒ์ผ๋กœ ์ €์žฅํ•  ๋•Œ๋Š” ํŒŒ์ผ ์ž…์ถœ๋ ฅ์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ pickle ๋ชจ๋“ˆ์„ ์ด์šฉํ•˜๋ฉด ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋ฃŒํ˜•์˜ ๋ณ€๊ฒฝ์—†์ด ํŒŒ์ผ๋กœ ์ €์žฅํ•˜์—ฌ ๊ทธ๋Œ€๋กœ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ฃผ๋กœ ํ…์ŠคํŠธ ์ด์™ธ์˜ ์ž๋ฃŒํ˜•์„ ํŒŒ์ผ๋กœ ์ €์žฅํ•  ๋•Œ ์“ฐ์ธ๋‹ค.

pickle example 1

pickle example 2

marshal

Go์—์„œ์˜ Marshal์€ ๋ฐ์ดํ„ฐ๋ฅผ JSON ํฌ๋งท์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ธ์ฝ”๋”ฉ ํ•จ์ˆ˜๋กœ, ํŒŒ์ด์ฌ์˜ json๊ณผ ๋น„์Šทํ•˜๋‹ค. ๊ฐ™์€ ์ด๋ฆ„์˜ ๋‚ด๋ถ€ ๋ชจ๋“ˆ์ด ํŒŒ์ด์ฌ์—๋„ ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ์˜ค๋Š˜์—์•ผ ์•Œ์•˜๋‹ค. ์ด๋ฆ„์€ ๊ฐ™์ง€๋งŒ ๋™์ž‘ํ•˜๋Š” ๋ฐฉ์‹์€ ์ „ํ˜€ ๋‹ค๋ฅด๋‹ค.

ํŒŒ์ด์ฌ์˜ marshal์€ pickle๋ณด๋‹ค ์ข€ ๋” ์›์‹œ์ ์ธ ์ง๋ ฌํ™” ๋ชจ๋“ˆ๋กœ,์ฃผ๋กœ ํŒŒ์ด์ฌ์˜ .pyc ํŒŒ์ผ์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด ์กด์žฌํ•œ๋‹ค.

pickle vs marshal

pickle๊ณผ marshal์€ ์•„์ฃผ ๋น„์Šทํ•ด ๋ณด์ด์ง€๋งŒ ์ฐจ์ด์ ์ด ๋ถ„๋ช…ํžˆ ์žˆ๋‹ค.

  • pickle ๋ชจ๋“ˆ์€ ์ด๋ฏธ ์ง๋ ฌํ™”๋œ ๊ฐ์ฒด๋ฅผ ์ถ”์ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜์ค‘์— ๋™์ผํ•œ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ๊ฐ€ ๋‹ค์‹œ ์ง๋ ฌํ™”๋˜์ง€ ์•Š๋Š”๋‹ค. marshal์€ ๊ทธ๋ ‡์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์žฌ๊ท€์  ๊ฐ์ฒด๋ฅผ marshalingํ•˜๋ ค๊ณ  ์‹œ๋„ํ•˜๋ฉด ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ์ถฉ๋Œํ•œ๋‹ค.
  • marshal์€ ์‚ฌ์šฉ์ž ์ •์˜ ํด๋ž˜์Šค์™€ ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๋ฅผ ์ง๋ ฌํ™”ํ•  ์ˆ˜ ์—†๋‹ค.
  • marshal์€ ํŒŒ์ด์ฌ ๋ฒ„์ „์—์„œ ํ˜ธํ™˜์„ฑ ๋ณด์žฅ์ด ์•ˆ๋œ๋‹ค.

pickle vs json

pickle๊ณผ json ์‚ฌ์ด์—๋Š” ๊ทผ๋ณธ์ ์ธ ์ฐจ์ด์ ์ด ์žˆ๋‹ค.

  • json์€ ํ…์ŠคํŠธ ์ง๋ ฌํ™” ํ˜•์‹์ธ ๋ฐ˜๋ฉด, pickle์€ ์ด์ง„ ์ง๋ ฌํ™” ํ˜•์‹์ด๋‹ค.
  • json์€ ์‚ฌ๋žŒ์ด ์ฝ์„ ์ˆ˜ ์žˆ์ง€๋งŒ pickle์€ ์ฝ์„ ์ˆ˜ ์—†๋‹ค.
  • json์€ ์ƒํ˜ธ ์šด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ ํŒŒ์ด์ฌ ์ƒํƒœ๊ณ„ ์™ธ๋ถ€์—์„œ๋„ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ˜๋ฉด, pickle์€ ์˜ค์ง ํŒŒ์ด์ฌ ์ „์šฉ์ด๋‹ค.

References


Written by@ugaemi
Record things I want to remember

๐Ÿฑ GitHub๐Ÿ“š Reading Space