flightcontrolhq/superjson

Is there a way to get just the string value inside `json` when doing stringify?

nooblyf opened this issue · 2 comments

when i stringify anything i get json and meta (or sometimes just json) like this

stringify({
  flasjhdfojha: 45234524,
  fadsfasd: { fasfd: { dfasdfasdf: { fasdfasd: 341234123, date: new Date() } } },
})
// {"json":{"flasjhdfojha":45234524,"fadsfasd":{"fasfd":{"dfasdfasdf":{"fasdfasd":341234123,"date":"2023-08-05T19:39:45.674Z"}}}},"meta":{"values":{"fadsfasd.fasfd.dfasdfasdf.date":["Date"]}}}

stringify("helooooo")
// {"json":"helooooo"}

is there a way to get just the value inside json as a string? something like

// string
// {"flasjhdfojha":45234524,"fadsfasd":{"fasfd":{"dfasdfasdf":{"fasdfasd":341234123,"date":"2023-08-05T19:39:45.674Z"}}}}

// string
// helooooo

what i'm currently doing is using regex to remove json (key) and meta completely (if it exists)

stringify(data).replace(/^(({\"json\":?)(\"?))|(,"meta":.*)|((\"?)(}?$))/gi, '');
Skn0tt commented

Hi @nooblyf! You could use SuperJSON.serialize, grab the json key and stringify that using JSON.stringify. That means all type information will be lost, though, losing the main benefit of SuperJSON. Depending on what you're looking for, JSON's replacer option might be a better fit.

I wanted this for logging purposes only, i guess JSON.stringify is the way to go