Commit bfa6ffb5 authored by Anton Likhtarov's avatar Anton Likhtarov Committed by Jordan DeLong

sort_keys option for json

Summary:
A dumb&slow implementation. I just needed something that outputs keys in a sorted order.
No easy to use API to discourage the use due to perf implications.

Test Plan: unit test

Reviewed By: delong.j@fb.com

FB internal diff: D1073757
parent 048a4518
...@@ -159,6 +159,16 @@ private: ...@@ -159,6 +159,16 @@ private:
(*this)(p.second); (*this)(p.second);
} }
template <typename Iterator>
void printKVPairs(Iterator begin, Iterator end) const {
printKV(*begin);
for (++begin; begin != end; ++begin) {
out_ += ',';
newline();
printKV(*begin);
}
}
void printObject(dynamic const& o) const { void printObject(dynamic const& o) const {
if (o.empty()) { if (o.empty()) {
out_ += "{}"; out_ += "{}";
...@@ -168,12 +178,13 @@ private: ...@@ -168,12 +178,13 @@ private:
out_ += '{'; out_ += '{';
indent(); indent();
newline(); newline();
auto it = o.items().begin(); if (opts_.sort_keys) {
printKV(*it); std::vector<std::pair<dynamic, dynamic>> items(
for (++it; it != o.items().end(); ++it) { o.items().begin(), o.items().end());
out_ += ','; std::sort(items.begin(), items.end());
newline(); printKVPairs(items.begin(), items.end());
printKV(*it); } else {
printKVPairs(o.items().begin(), o.items().end());
} }
outdent(); outdent();
newline(); newline();
......
...@@ -59,6 +59,7 @@ namespace json { ...@@ -59,6 +59,7 @@ namespace json {
, encode_non_ascii(false) , encode_non_ascii(false)
, validate_utf8(false) , validate_utf8(false)
, allow_trailing_comma(false) , allow_trailing_comma(false)
, sort_keys(false)
{} {}
// If true, keys in an object can be non-strings. (In strict // If true, keys in an object can be non-strings. (In strict
...@@ -85,6 +86,9 @@ namespace json { ...@@ -85,6 +86,9 @@ namespace json {
// Allow trailing comma in lists of values / items // Allow trailing comma in lists of values / items
bool allow_trailing_comma; bool allow_trailing_comma;
// Sort keys of all objects before printing out (potentially slow)
bool sort_keys;
}; };
/* /*
......
...@@ -331,6 +331,37 @@ TEST(Json, ParseNonStringKeys) { ...@@ -331,6 +331,37 @@ TEST(Json, ParseNonStringKeys) {
EXPECT_EQ(1.5, dval.items().begin()->first.asDouble()); EXPECT_EQ(1.5, dval.items().begin()->first.asDouble());
} }
TEST(Json, SortKeys) {
folly::json::serialization_opts opts_on, opts_off;
opts_on.sort_keys = true;
opts_off.sort_keys = false;
dynamic value = dynamic::object
("foo", "bar")
("junk", 12)
("another", 32.2)
("a",
{
dynamic::object("a", "b")
("c", "d"),
12.5,
"Yo Dawg",
{ "heh" },
nullptr
}
)
;
std::string sorted_keys =
R"({"a":[{"a":"b","c":"d"},12.5,"Yo Dawg",["heh"],null],)"
R"("another":32.2,"foo":"bar","junk":12})";
EXPECT_EQ(value, parseJson(folly::json::serialize(value, opts_on)));
EXPECT_EQ(value, parseJson(folly::json::serialize(value, opts_off)));
EXPECT_EQ(sorted_keys, folly::json::serialize(value, opts_on));
}
BENCHMARK(jsonSerialize, iters) { BENCHMARK(jsonSerialize, iters) {
folly::json::serialization_opts opts; folly::json::serialization_opts opts;
for (int i = 0; i < iters; ++i) { for (int i = 0; i < iters; ++i) {
......
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