CaribouCode
CaribouCode

Reputation: 14428

Reverse json-stringify-safe

In my NextJS React app I use getInitialProps to fetch data that has circular references in. The getInitialProps method serialises using JSON.stringify, so when circular references are involved NextJS throws this error.

A solution is to use the json-stringify-safe package which works like this:

const stringify = require('json-stringify-safe');

const test = { a: 'hello' };
test.b = test;

const testWithoutCircularReferences = JSON.parse(stringify(test));
console.log(testWithoutCircularReferences);
// Output: {"a":"hello","b":"[Circular ~]"}

For the object to be usable in my React, I need to reverse this, but this widely used package doesn't seem to have a parse function, or something similar to reverse the original stringify I did. Is there a way to achieve this?

Upvotes: 0

Views: 844

Answers (1)

Dirk Bester
Dirk Bester

Reputation: 1945

https://github.com/samchon/typia for Typescript based I/O. Single line parse/stringify/validate etc. But you need Typescript classes.

Upvotes: 1

Related Questions