Rich-Harris/devalue

Support a `.toJSON`-like interface

coyotte508 opened this issue · 0 comments

Feature request

Make it so that if an object as a .toJSON() function, devalue runs on the result of that function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior

Why

It would be nice to have feature parity with JSON.stringify.

My particular usecase is regarding SvelteKit: I'm engineering a pinia-like global store that works no matter the context (layout/page load in browser/ssr, or component initialization during browser/ssr), and so I store some writable stores in the session. The issue of course being that I don't want multiple users accessing the same shared stores during ssr.

I can achieve what I want by using Object.defineProperty with non-enumerable, regardless a toJSON api would be nice.

Other

If the feature is approved, I can do the PR.