Make json output a bit prettier.

This commit is contained in:
chriseth 2019-07-02 18:06:16 +02:00
parent 60525dbf52
commit 776fec1913
2 changed files with 14 additions and 10 deletions

View File

@ -23,6 +23,8 @@
#include <libdevcore/CommonIO.h> #include <libdevcore/CommonIO.h>
#include <boost/algorithm/string/replace.hpp>
#include <sstream> #include <sstream>
#include <map> #include <map>
#include <memory> #include <memory>
@ -44,9 +46,9 @@ namespace
class StreamWriterBuilder: public Json::StreamWriterBuilder class StreamWriterBuilder: public Json::StreamWriterBuilder
{ {
public: public:
explicit StreamWriterBuilder(map<string, string> const& _settings) explicit StreamWriterBuilder(map<string, Json::Value> const& _settings)
{ {
for (auto const& iter :_settings) for (auto const& iter: _settings)
this->settings_[iter.first] = iter.second; this->settings_[iter.first] = iter.second;
} }
}; };
@ -89,14 +91,16 @@ bool parse(Json::CharReaderBuilder& _builder, string const& _input, Json::Value&
string jsonPrettyPrint(Json::Value const& _input) string jsonPrettyPrint(Json::Value const& _input)
{ {
static map<string, string> settings{{"indentation", " "}}; static map<string, Json::Value> settings{{"indentation", " "}, {"enableYAMLCompatibility", true}};
static StreamWriterBuilder writerBuilder(settings); static StreamWriterBuilder writerBuilder(settings);
return print(_input, writerBuilder); string result = print(_input, writerBuilder);
boost::replace_all(result, " \n", "\n");
return result;
} }
string jsonCompactPrint(Json::Value const& _input) string jsonCompactPrint(Json::Value const& _input)
{ {
static map<string, string> settings{{"indentation", ""}}; static map<string, Json::Value> settings{{"indentation", ""}};
static StreamWriterBuilder writerBuilder(settings); static StreamWriterBuilder writerBuilder(settings);
return print(_input, writerBuilder); return print(_input, writerBuilder);
} }

View File

@ -45,12 +45,12 @@ BOOST_AUTO_TEST_CASE(json_pretty_print)
BOOST_CHECK( BOOST_CHECK(
"{\n" "{\n"
" \"1\" : 1,\n" " \"1\": 1,\n"
" \"2\" : \"2\",\n" " \"2\": \"2\",\n"
" \"3\" : \n" " \"3\":\n"
" {\n" " {\n"
" \"3.1\" : \"3.1\",\n" " \"3.1\": \"3.1\",\n"
" \"3.2\" : 2\n" " \"3.2\": 2\n"
" }\n" " }\n"
"}" == jsonPrettyPrint(json)); "}" == jsonPrettyPrint(json));
} }