JSON.stringify takes toJSON methods into consideration (#tilPost)

I was reading this great article on JSON.stringify by Valeri Karpov when I discovered something I didn’t know before.
You can use JSON.stringify to serialize objects and store them let’s say in localStorage. It turns out that JSON.s…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

I was reading this great article on JSON.stringify by Valeri Karpov when I discovered something I didn't know before.

You can use JSON.stringify to serialize objects and store them let's say in localStorage. It turns out that JSON.stringify checks if the object to serialize includes a toJSON method. If it does it will use this method to evaluate the result of the serialization.

const zoo = {
  animals: {
    list: ['cat', 'dog', 'duck'],
    // toJSON will be called by JSON.stringify
    toJSON: () => {
      return ['?', '?', '?']
    }
  }
}

console.log(JSON.stringify(zoo, null, 2));

By including a toJSON method you can manipulate the data that should go into serialization. You can use this functionality to e.g. clean up logs and not store sensitive information in a database. Pretty sweet. ?

Result of the logged object printing emojis instead of animal names

If you want to learn more you can check MDN or have a look at a chapter of Exploring JavaScript written by Axel Rauschmayer.


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate
APA
Stefan Judis | Sciencx (2022-12-09T06:13:54+00:00) » JSON.stringify takes toJSON methods into consideration (#tilPost). Retrieved from https://www.scien.cx/2019/06/03/json-stringify-takes-tojson-methods-into-consideration-tilpost/.
MLA
" » JSON.stringify takes toJSON methods into consideration (#tilPost)." Stefan Judis | Sciencx - Monday June 3, 2019, https://www.scien.cx/2019/06/03/json-stringify-takes-tojson-methods-into-consideration-tilpost/
HARVARD
Stefan Judis | Sciencx Monday June 3, 2019 » JSON.stringify takes toJSON methods into consideration (#tilPost)., viewed 2022-12-09T06:13:54+00:00,<https://www.scien.cx/2019/06/03/json-stringify-takes-tojson-methods-into-consideration-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » JSON.stringify takes toJSON methods into consideration (#tilPost). [Internet]. [Accessed 2022-12-09T06:13:54+00:00]. Available from: https://www.scien.cx/2019/06/03/json-stringify-takes-tojson-methods-into-consideration-tilpost/
CHICAGO
" » JSON.stringify takes toJSON methods into consideration (#tilPost)." Stefan Judis | Sciencx - Accessed 2022-12-09T06:13:54+00:00. https://www.scien.cx/2019/06/03/json-stringify-takes-tojson-methods-into-consideration-tilpost/
IEEE
" » JSON.stringify takes toJSON methods into consideration (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2019/06/03/json-stringify-takes-tojson-methods-into-consideration-tilpost/. [Accessed: 2022-12-09T06:13:54+00:00]
rf:citation
» JSON.stringify takes toJSON methods into consideration (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2019/06/03/json-stringify-takes-tojson-methods-into-consideration-tilpost/ | 2022-12-09T06:13:54+00:00
https://github.com/addpipe/simple-recorderjs-demo