diff --git a/src/json.hpp b/src/json.hpp index 6c501949..dfd78e58 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -1463,11 +1463,6 @@ class basic_json switch (m_type) { - case (value_t::null): - { - return "null"; - } - case (value_t::object): { if (m_value.object->empty()) @@ -1558,6 +1553,11 @@ class basic_json { return std::to_string(m_value.number_float); } + + default: + { + return "null"; + } } } diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 6b8bd5f3..d76cd09a 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -1463,11 +1463,6 @@ class basic_json switch (m_type) { - case (value_t::null): - { - return "null"; - } - case (value_t::object): { if (m_value.object->empty()) @@ -1558,6 +1553,11 @@ class basic_json { return std::to_string(m_value.number_float); } + + default: + { + return "null"; + } } } diff --git a/test/unit.cpp b/test/unit.cpp index e38ea663..4112b375 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -3915,3 +3915,61 @@ TEST_CASE("deserialization") CHECK(j == json({"foo", 1, 2, 3, false, {{"one", 1}}})); } } + + +TEST_CASE("convenience functions") +{ + SECTION("type name as string") + { + CHECK(json(json::value_t::null).type_name() == "null"); + CHECK(json(json::value_t::object).type_name() == "object"); + CHECK(json(json::value_t::array).type_name() == "array"); + CHECK(json(json::value_t::number_integer).type_name() == "number"); + CHECK(json(json::value_t::number_float).type_name() == "number"); + CHECK(json(json::value_t::boolean).type_name() == "boolean"); + CHECK(json(json::value_t::string).type_name() == "string"); + } + + 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"); + + 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"); + } +}