RejectedSoftware Forums

Sign up

toPrettyJson

Silly question:
I'm coming from toy languages such as JavaScript/Ruby/Python and when I want to output pretty JSON to the console most of these have some form of a pretty formatter that goes something like:

//javascript
console.log(JSON.stringify(jsonObj, null, 2));

// output
{
  "foo": "hello"
}

For the life of me, I cant figure out how "toPrettyJson" works. can someone give me a quick example?
here is how i thought its supposed to work

Json data = <some data>;
Json pretty;
toPrettyJson(pretty, data, 2);
writeln(pretty.toString());

Re: toPrettyJson

Json input = parseJsonString("{\"key\" : \"value\"}");
Appender!string output;
toPrettyJson(output, input);
writefln("%s", output.data);

toPrettyJson actually does not look like a convenience function. More like a primitive for efficient Json-to-string serialization that can be probably used for "prettier" one. Funny to mention that documentation references some "toPrettyJSON" that is supposed to do additional indentation, but it is nowhere to be found.

Re: toPrettyJson

Am 21.01.2013 20:48, schrieb Dicebot:

Json input = parseJsonString("{\"key\" : \"value\"}");
Appender!string output;
toPrettyJson(output, input);
writefln("%s", output.data);

toPrettyJson actually does not look like a convenience function. More like a primitive for efficient
Json-to-string serialization that can be probably used for "prettier" one. Funny to mention that
documentation references some "toPrettyJSON" that is supposed to do additional indentation, but it
is nowhere to be found.

I've actually stumbled over that myself some days ago (that it's not convenient when all you want is
a string). There is a second 'overload' string toPrettyJson(in Json) now (typo also fixed).

Re: toPrettyJson

awesome, thanks

Re: toPrettyJson

Just for the record, we have now:

  • Json.toString()
  • Json.toPrettyString()
  • writeJsonString(dst, json) (was toJson)
  • writePrettyJsonString(dst, json) (was toPrettyJson)

The old toXYZ functions are still there as aliases but will be deprecated after the next release.