Commit 579e7cb2 authored by Christopher Wei's avatar Christopher Wei Committed by Facebook GitHub Bot

Add indent size for json pretty printing

Summary: Adds `pretty_formating_indent_size` option which is used to change the indentation size when used with `pretty_formatting`

Reviewed By: yfeldblum

Differential Revision: D26793086

fbshipit-source-id: 53efea33d7daa5c8f8840fc7f087d2d976ed3eda
parent 2690cff7
...@@ -189,7 +189,8 @@ struct Printer { ...@@ -189,7 +189,8 @@ struct Printer {
void newline() const { void newline() const {
if (indentLevel_) { if (indentLevel_) {
out_ += to<std::string>('\n', std::string(*indentLevel_ * 2, ' ')); auto indent = *indentLevel_ * opts_.pretty_formatting_indent_width;
out_ += to<std::string>('\n', std::string(indent, ' '));
} }
} }
......
...@@ -58,6 +58,7 @@ struct serialization_opts { ...@@ -58,6 +58,7 @@ struct serialization_opts {
: allow_non_string_keys(false), : allow_non_string_keys(false),
javascript_safe(false), javascript_safe(false),
pretty_formatting(false), pretty_formatting(false),
pretty_formatting_indent_width(2),
encode_non_ascii(false), encode_non_ascii(false),
validate_utf8(false), validate_utf8(false),
allow_trailing_comma(false), allow_trailing_comma(false),
...@@ -87,6 +88,9 @@ struct serialization_opts { ...@@ -87,6 +88,9 @@ struct serialization_opts {
// try to be minimally "pretty". // try to be minimally "pretty".
bool pretty_formatting; bool pretty_formatting;
// The number of spaces to indent by when pretty_formatting is enabled.
unsigned int pretty_formatting_indent_width;
// If true, non-ASCII utf8 characters would be encoded as \uXXXX: // If true, non-ASCII utf8 characters would be encoded as \uXXXX:
// - if the code point is in [U+0000..U+FFFF] => encode as a single \uXXXX // - if the code point is in [U+0000..U+FFFF] => encode as a single \uXXXX
// - if the code point is > U+FFFF => encode as 2 UTF-16 surrogate pairs. // - if the code point is > U+FFFF => encode as 2 UTF-16 surrogate pairs.
......
...@@ -797,6 +797,41 @@ TEST(Json, SortKeys) { ...@@ -797,6 +797,41 @@ TEST(Json, SortKeys) {
inverse_sorted_keys, folly::json::serialize(value, opts_custom_sort)); inverse_sorted_keys, folly::json::serialize(value, opts_custom_sort));
} }
TEST(Json, PrettyPrintIndent) {
folly::json::serialization_opts opts;
opts.sort_keys = true;
opts.pretty_formatting = true;
opts.pretty_formatting_indent_width = 4;
// clang-format off
dynamic value = dynamic::object
("foo", "bar")
("nested",
dynamic::object
("abc", "def")
("qrs", dynamic::array("tuv", 789))
("xyz", 123)
)
("zzz", 456)
;
// clang-format on
std::string expected = R"({
"foo": "bar",
"nested": {
"abc": "def",
"qrs": [
"tuv",
789
],
"xyz": 123
},
"zzz": 456
})";
EXPECT_EQ(expected, folly::json::serialize(value, opts));
}
TEST(Json, PrintTo) { TEST(Json, PrintTo) {
std::ostringstream oss; std::ostringstream oss;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment