flightcontrolhq / superjson

Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more.
https://www.flightcontrol.dev?ref=superjson
MIT License
4.02k stars 87 forks source link

Pretty Printing #222

Closed Zamiell closed 1 year ago

Zamiell commented 1 year ago

Hello, and thanks for the library.

With normal JSON.stringify, I can create pretty output like so:

console.log(JSON.stringify(request, undefined, 2));

I would like to emulate this behavior, but with superjson.

I imagine that the API could be changed from:

static stringify: (object: SuperJSONValue) => string;

To something like:

class StringifyOptions {
  pretty = false;
}

static stringify: (object: SuperJSONValue, options = new StringifyOptions()) => string;

Or, more simply, but less flexible for future additions:

static stringify: (object: SuperJSONValue, pretty = false) => string;
Skn0tt commented 1 year ago

Hi @Zamiell! Pretty-Printing is out-of-scope for SuperJSON. If you want to prettify your output, you could do the following:

JSON.stringify(SuperJSON.serialize(request), undefined, 2)

Does that solve your problem?

Zamiell commented 1 year ago

Ok. I'll close this issue then, thank you for the quick response.