Skip to content

Commit

Permalink
sort_keys option for json
Browse files Browse the repository at this point in the history
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: [email protected]

FB internal diff: D1073757
  • Loading branch information
alikhtarov authored and jdelong committed Dec 20, 2013
1 parent 048a451 commit bfa6ffb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
23 changes: 17 additions & 6 deletions folly/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ struct Printer {
(*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 {
if (o.empty()) {
out_ += "{}";
Expand All @@ -168,12 +178,13 @@ struct Printer {
out_ += '{';
indent();
newline();
auto it = o.items().begin();
printKV(*it);
for (++it; it != o.items().end(); ++it) {
out_ += ',';
newline();
printKV(*it);
if (opts_.sort_keys) {
std::vector<std::pair<dynamic, dynamic>> items(
o.items().begin(), o.items().end());
std::sort(items.begin(), items.end());
printKVPairs(items.begin(), items.end());
} else {
printKVPairs(o.items().begin(), o.items().end());
}
outdent();
newline();
Expand Down
4 changes: 4 additions & 0 deletions folly/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace json {
, encode_non_ascii(false)
, validate_utf8(false)
, allow_trailing_comma(false)
, sort_keys(false)
{}

// If true, keys in an object can be non-strings. (In strict
Expand All @@ -85,6 +86,9 @@ namespace json {

// Allow trailing comma in lists of values / items
bool allow_trailing_comma;

// Sort keys of all objects before printing out (potentially slow)
bool sort_keys;
};

/*
Expand Down
31 changes: 31 additions & 0 deletions folly/test/JsonTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,37 @@ TEST(Json, ParseNonStringKeys) {
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) {
folly::json::serialization_opts opts;
for (int i = 0; i < iters; ++i) {
Expand Down

0 comments on commit bfa6ffb

Please sign in to comment.