TL;DR: Is there a built-in way in ServiceStack.Text to produce pretty-printed JSON?
I am using ServiceStack.Text for doing JSON serialization. It works really good so far, but the created JSON (using .ToJSON()
) is not formated with whitespaces or newlines (most likely to save space when sending over the network). However, in some circumstances it would be nice to have the JSON formatted for easier human-readability.
The .Dump ()
method does some sort of formatting, however does not produce valid JSON (i.e. the surrounding doublequotes are missing).
The
T.Dump()
andT.PrintDump()
extension methods in ServiceStack.Text are just a pretty formatted version of the JSV Format that's created with the TypeSerializer class orT.ToJsv()
Extension method. It is only to provide a human friendly dump of data, it's not parseable.The new
string.IndentJson()
extension method available from v4.5.5 will let you pretty-print JSON otherwise you can install a Pretty JSONView extension for Chrome or Firefox to see pretty JSON or you can paste the JSON in jsonprettyprint.comTo get pretty JSON:
The resulting json string can be parsed back:
Having a pretty json format would be good from the servicestack text. As a workaround because I created a a plugin to format the json when I needed it. Hopefully a future release of service stack I can get rid of this code.
Download the dll from the link below, (it gives you an extension method to format the json) http://www.markdavidrogers.com/json-pretty-printerbeautifier-library-for-net/
I used this in stead of something like json.net as I wanted to make sure I did not change the servicestack serialization of the json.
Then I created the following pluging
In you startup code register the new plugin
To call an example service from c#
Here is an equivilent xml one