Serialization and deserialization in JS
Hello guys, sorry to disturb you; I currently learning about JSON and I came across the words serialization and deserialization... can someone explain in simple terms what is serialization and deserialization (giving examples if possible) and why is it important please.
5 Replies
JSON is just a format for storing data
like it is a defined structured way you can store data that is easy to reas for both human and machine.
serialisation = convertion to formatted data structure (like json) for storing data
deserialisation = the opposite of serialisation
there are other formats likewise to JSON like BSON ,EJSON
I think the top answer of this question on Stack Overflow explains it the best
https://stackoverflow.com/questions/3316762/what-is-deserialize-and-serialize-in-json
What everyone said above is correct, just wanted to add practical examples; that to convert , you use
JSON.stringify()
(serialization: convert JSON object into a string ) and JSON.parse()
(deserialisation: convert string back into a JSON object )yeah I see, thanks !
you need to be a little careful with what you serialize
you can't serialize functions, for example
this is because json, while based in javascript's object syntax, it isn't javascript
and to serialize a function, you need to either convert it to a string that's then converted to the target language, or you would have to write an entire javscript interpreter alongside a json parser
this is incredibly unsafe and nearly impossible to do
there are other objects that need help to be converted, like maps (you can map an object instance to another object instance) and sets