🔀 merge branch 'feature/serialization_class' into develop (#418)

Moved all dump()-related functions into a class "serializer". This fix includes a lot of performance improvements yielding a 7% speedup for serialization. Details on the individual steps can be found in the commit messages.

Individual benchmark numbers:

before:

dump jeopardy.json                            5   374555228 ns/op
dump jeopardy.json with indent                5   430953700 ns/op
dump numbers/floats.json                      5   622938509 ns/op
dump numbers/signed_ints.json                20    82177979 ns/op

after:

dump jeopardy.json                            5   335449757 ns/op -11%
dump jeopardy.json with indent                5   375467773 ns/op -13%
dump numbers/floats.json                      5   584611852 ns/op -7%
dump numbers/signed_ints.json                20    68289574 ns/op -17%
This commit is contained in:
Niels Lohmann 2017-02-28 21:04:16 +01:00
commit 41f9b32554
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
4 changed files with 1223 additions and 1072 deletions

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -49,44 +49,53 @@ TEST_CASE("convenience functions")
SECTION("string escape")
{
CHECK(json::escape_string("\"") == "\\\"");
CHECK(json::escape_string("\\") == "\\\\");
CHECK(json::escape_string("\b") == "\\b");
CHECK(json::escape_string("\f") == "\\f");
CHECK(json::escape_string("\n") == "\\n");
CHECK(json::escape_string("\r") == "\\r");
CHECK(json::escape_string("\t") == "\\t");
const auto check_escaped = [](const char* original,
const char* escaped)
{
std::stringstream ss;
json::serializer s(ss);
s.dump_escaped(original);
CHECK(ss.str() == escaped);
};
CHECK(json::escape_string("\x01") == "\\u0001");
CHECK(json::escape_string("\x02") == "\\u0002");
CHECK(json::escape_string("\x03") == "\\u0003");
CHECK(json::escape_string("\x04") == "\\u0004");
CHECK(json::escape_string("\x05") == "\\u0005");
CHECK(json::escape_string("\x06") == "\\u0006");
CHECK(json::escape_string("\x07") == "\\u0007");
CHECK(json::escape_string("\x08") == "\\b");
CHECK(json::escape_string("\x09") == "\\t");
CHECK(json::escape_string("\x0a") == "\\n");
CHECK(json::escape_string("\x0b") == "\\u000b");
CHECK(json::escape_string("\x0c") == "\\f");
CHECK(json::escape_string("\x0d") == "\\r");
CHECK(json::escape_string("\x0e") == "\\u000e");
CHECK(json::escape_string("\x0f") == "\\u000f");
CHECK(json::escape_string("\x10") == "\\u0010");
CHECK(json::escape_string("\x11") == "\\u0011");
CHECK(json::escape_string("\x12") == "\\u0012");
CHECK(json::escape_string("\x13") == "\\u0013");
CHECK(json::escape_string("\x14") == "\\u0014");
CHECK(json::escape_string("\x15") == "\\u0015");
CHECK(json::escape_string("\x16") == "\\u0016");
CHECK(json::escape_string("\x17") == "\\u0017");
CHECK(json::escape_string("\x18") == "\\u0018");
CHECK(json::escape_string("\x19") == "\\u0019");
CHECK(json::escape_string("\x1a") == "\\u001a");
CHECK(json::escape_string("\x1b") == "\\u001b");
CHECK(json::escape_string("\x1c") == "\\u001c");
CHECK(json::escape_string("\x1d") == "\\u001d");
CHECK(json::escape_string("\x1e") == "\\u001e");
CHECK(json::escape_string("\x1f") == "\\u001f");
check_escaped("\"", "\\\"");
check_escaped("\\", "\\\\");
check_escaped("\b", "\\b");
check_escaped("\f", "\\f");
check_escaped("\n", "\\n");
check_escaped("\r", "\\r");
check_escaped("\t", "\\t");
check_escaped("\x01", "\\u0001");
check_escaped("\x02", "\\u0002");
check_escaped("\x03", "\\u0003");
check_escaped("\x04", "\\u0004");
check_escaped("\x05", "\\u0005");
check_escaped("\x06", "\\u0006");
check_escaped("\x07", "\\u0007");
check_escaped("\x08", "\\b");
check_escaped("\x09", "\\t");
check_escaped("\x0a", "\\n");
check_escaped("\x0b", "\\u000b");
check_escaped("\x0c", "\\f");
check_escaped("\x0d", "\\r");
check_escaped("\x0e", "\\u000e");
check_escaped("\x0f", "\\u000f");
check_escaped("\x10", "\\u0010");
check_escaped("\x11", "\\u0011");
check_escaped("\x12", "\\u0012");
check_escaped("\x13", "\\u0013");
check_escaped("\x14", "\\u0014");
check_escaped("\x15", "\\u0015");
check_escaped("\x16", "\\u0016");
check_escaped("\x17", "\\u0017");
check_escaped("\x18", "\\u0018");
check_escaped("\x19", "\\u0019");
check_escaped("\x1a", "\\u001a");
check_escaped("\x1b", "\\u001b");
check_escaped("\x1c", "\\u001c");
check_escaped("\x1d", "\\u001d");
check_escaped("\x1e", "\\u001e");
check_escaped("\x1f", "\\u001f");
}
}

View file

@ -213,6 +213,18 @@ TEST_CASE("object inspection")
"{\n \"array\": [\n 1,\n 2,\n 3,\n 4\n ],\n \"boolean\": false,\n \"null\": null,\n \"number\": 42,\n \"object\": {},\n \"string\": \"Hello world\"\n}");
}
SECTION("indent=x")
{
CHECK(j.dump().size() == 94);
CHECK(j.dump(1).size() == 127);
CHECK(j.dump(2).size() == 142);
CHECK(j.dump(512).size() == 7792);
// important test, because it yields a resize of the indent_string
// inside the dump() function
CHECK(j.dump(1024).size() == 15472);
}
SECTION("dump and floating-point numbers")
{
auto s = json(42.23).dump();