Merge branch 'develop' into move-construction

This commit is contained in:
Niels Lohmann 2017-07-30 19:49:24 +02:00 committed by GitHub
commit aad55219f8
10 changed files with 974 additions and 999 deletions

View file

@ -3,8 +3,6 @@ cmake_minimum_required(VERSION 3.0)
# define the project # define the project
project(nlohmann_json VERSION 2.1.1 LANGUAGES CXX) project(nlohmann_json VERSION 2.1.1 LANGUAGES CXX)
enable_testing()
option(JSON_BuildTests "Build the unit tests" ON) option(JSON_BuildTests "Build the unit tests" ON)
# define project variables # define project variables
@ -26,6 +24,7 @@ target_include_directories(${JSON_TARGET_NAME} INTERFACE
# create and configure the unit test target # create and configure the unit test target
if(JSON_BuildTests) if(JSON_BuildTests)
enable_testing()
add_subdirectory(test) add_subdirectory(test)
endif() endif()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 645 KiB

File diff suppressed because it is too large Load diff

View file

@ -1284,6 +1284,25 @@ TEST_CASE("single CBOR roundtrip")
// compare parsed JSON values // compare parsed JSON values
CHECK(j1 == j2); CHECK(j1 == j2);
SECTION("roundtrips")
{
SECTION("std::ostringstream")
{
std::ostringstream ss;
json::to_cbor(j1, ss);
json j3 = json::from_cbor(ss.str());
CHECK(j1 == j3);
}
SECTION("std::string")
{
std::string s;
json::to_cbor(j1, s);
json j3 = json::from_cbor(s);
CHECK(j1 == j3);
}
}
// check with different start index // check with different start index
packed.insert(packed.begin(), 5, 0xff); packed.insert(packed.begin(), 5, 0xff);
CHECK(j1 == json::from_cbor(packed, 5)); CHECK(j1 == json::from_cbor(packed, 5));
@ -1519,16 +1538,60 @@ TEST_CASE("CBOR roundtrips", "[hide]")
std::ifstream f_json(filename); std::ifstream f_json(filename);
json j1 = json::parse(f_json); json j1 = json::parse(f_json);
// parse CBOR file SECTION("std::vector<uint8_t>")
std::ifstream f_cbor(filename + ".cbor", std::ios::binary); {
std::vector<uint8_t> packed( // parse CBOR file
(std::istreambuf_iterator<char>(f_cbor)), std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
std::istreambuf_iterator<char>()); std::vector<uint8_t> packed(
json j2; (std::istreambuf_iterator<char>(f_cbor)),
CHECK_NOTHROW(j2 = json::from_cbor(packed)); std::istreambuf_iterator<char>());
json j2;
CHECK_NOTHROW(j2 = json::from_cbor(packed));
// compare parsed JSON values // compare parsed JSON values
CHECK(j1 == j2); CHECK(j1 == j2);
}
SECTION("std::ifstream")
{
// parse CBOR file
std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
json j2;
CHECK_NOTHROW(j2 = json::from_cbor(f_cbor));
// compare parsed JSON values
CHECK(j1 == j2);
}
SECTION("uint8_t* and size")
{
// parse CBOR file
std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
std::vector<uint8_t> packed(
(std::istreambuf_iterator<char>(f_cbor)),
std::istreambuf_iterator<char>());
json j2;
CHECK_NOTHROW(j2 = json::from_cbor({packed.data(), packed.size()}));
// compare parsed JSON values
CHECK(j1 == j2);
}
SECTION("output to output adapters")
{
// parse CBOR file
std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
std::vector<uint8_t> packed(
(std::istreambuf_iterator<char>(f_cbor)),
std::istreambuf_iterator<char>());
SECTION("std::vector<uint8_t>")
{
std::vector<uint8_t> vec;
json::to_cbor(j1, vec);
CHECK(vec == packed);
}
}
} }
} }
} }

View file

@ -182,32 +182,32 @@ TEST_CASE("const_iterator class")
{ {
json j(json::value_t::null); json j(json::value_t::null);
json::const_iterator it = j.cbegin(); json::const_iterator it = j.cbegin();
CHECK_THROWS_AS(it->type_name(), json::invalid_iterator&); CHECK_THROWS_AS(std::string(it->type_name()), json::invalid_iterator&);
CHECK_THROWS_WITH(it->type_name(), "[json.exception.invalid_iterator.214] cannot get value"); CHECK_THROWS_WITH(std::string(it->type_name()), "[json.exception.invalid_iterator.214] cannot get value");
} }
SECTION("number") SECTION("number")
{ {
json j(17); json j(17);
json::const_iterator it = j.cbegin(); json::const_iterator it = j.cbegin();
CHECK(it->type_name() == "number"); CHECK(std::string(it->type_name()) == "number");
it = j.cend(); it = j.cend();
CHECK_THROWS_AS(it->type_name(), json::invalid_iterator&); CHECK_THROWS_AS(std::string(it->type_name()), json::invalid_iterator&);
CHECK_THROWS_WITH(it->type_name(), "[json.exception.invalid_iterator.214] cannot get value"); CHECK_THROWS_WITH(std::string(it->type_name()), "[json.exception.invalid_iterator.214] cannot get value");
} }
SECTION("object") SECTION("object")
{ {
json j({{"foo", "bar"}}); json j({{"foo", "bar"}});
json::const_iterator it = j.cbegin(); json::const_iterator it = j.cbegin();
CHECK(it->type_name() == "string"); CHECK(std::string(it->type_name()) == "string");
} }
SECTION("array") SECTION("array")
{ {
json j({1, 2, 3, 4}); json j({1, 2, 3, 4});
json::const_iterator it = j.cbegin(); json::const_iterator it = j.cbegin();
CHECK(it->type_name() == "number"); CHECK(std::string(it->type_name()) == "number");
} }
} }
} }

View file

@ -166,32 +166,32 @@ TEST_CASE("iterator class")
{ {
json j(json::value_t::null); json j(json::value_t::null);
json::iterator it = j.begin(); json::iterator it = j.begin();
CHECK_THROWS_AS(it->type_name(), json::invalid_iterator&); CHECK_THROWS_AS(std::string(it->type_name()), json::invalid_iterator&);
CHECK_THROWS_WITH(it->type_name(), "[json.exception.invalid_iterator.214] cannot get value"); CHECK_THROWS_WITH(std::string(it->type_name()), "[json.exception.invalid_iterator.214] cannot get value");
} }
SECTION("number") SECTION("number")
{ {
json j(17); json j(17);
json::iterator it = j.begin(); json::iterator it = j.begin();
CHECK(it->type_name() == "number"); CHECK(std::string(it->type_name()) == "number");
it = j.end(); it = j.end();
CHECK_THROWS_AS(it->type_name(), json::invalid_iterator&); CHECK_THROWS_AS(std::string(it->type_name()), json::invalid_iterator&);
CHECK_THROWS_WITH(it->type_name(), "[json.exception.invalid_iterator.214] cannot get value"); CHECK_THROWS_WITH(std::string(it->type_name()), "[json.exception.invalid_iterator.214] cannot get value");
} }
SECTION("object") SECTION("object")
{ {
json j({{"foo", "bar"}}); json j({{"foo", "bar"}});
json::iterator it = j.begin(); json::iterator it = j.begin();
CHECK(it->type_name() == "string"); CHECK(std::string(it->type_name()) == "string");
} }
SECTION("array") SECTION("array")
{ {
json j({1, 2, 3, 4}); json j({1, 2, 3, 4});
json::iterator it = j.begin(); json::iterator it = j.begin();
CHECK(it->type_name() == "number"); CHECK(std::string(it->type_name()) == "number");
} }
} }
} }

View file

@ -41,12 +41,31 @@ json parser_helper(const std::string& s)
{ {
json j; json j;
json::parser(nlohmann::detail::input_adapter(s)).parse(true, j); json::parser(nlohmann::detail::input_adapter(s)).parse(true, j);
// if this line was reached, no exception ocurred
// -> check if result is the same without exceptions
json j_nothrow;
CHECK_NOTHROW(json::parser(nlohmann::detail::input_adapter(s), nullptr, false).parse(true, j_nothrow));
CHECK(j_nothrow == j);
return j; return j;
} }
bool accept_helper(const std::string& s) bool accept_helper(const std::string& s)
{ {
return json::parser(nlohmann::detail::input_adapter(s)).accept(true); // 1. parse s without exceptions
json j;
CHECK_NOTHROW(json::parser(nlohmann::detail::input_adapter(s), nullptr, false).parse(true, j));
const bool ok_noexcept = not j.is_discarded();
// 2. accept s
const bool ok_accept = json::parser(nlohmann::detail::input_adapter(s)).accept(true);
// 3. check if both approaches come to the same result
CHECK(ok_noexcept == ok_accept);
// 4. return result
return ok_accept;
} }
TEST_CASE("parser class") TEST_CASE("parser class")
@ -590,8 +609,8 @@ TEST_CASE("parser class")
SECTION("overflow") SECTION("overflow")
{ {
// overflows during parsing yield an exception, but is accepted anyway // overflows during parsing
CHECK(accept_helper("1.18973e+4932")); CHECK(not accept_helper("1.18973e+4932"));
} }
SECTION("invalid numbers") SECTION("invalid numbers")

View file

@ -36,7 +36,7 @@ void check_escaped(const char* original, const char* escaped, const bool ensure_
void check_escaped(const char* original, const char* escaped, const bool ensure_ascii) void check_escaped(const char* original, const char* escaped, const bool ensure_ascii)
{ {
std::stringstream ss; std::stringstream ss;
json::serializer s(nlohmann::detail::output_adapter_factory<char>::create(ss), ' '); json::serializer s(nlohmann::detail::output_adapter<char>(ss), ' ');
s.dump_escaped(original, ensure_ascii); s.dump_escaped(original, ensure_ascii);
CHECK(ss.str() == escaped); CHECK(ss.str() == escaped);
} }
@ -45,15 +45,15 @@ TEST_CASE("convenience functions")
{ {
SECTION("type name as string") SECTION("type name as string")
{ {
CHECK(json(json::value_t::null).type_name() == "null"); CHECK(std::string(json(json::value_t::null).type_name()) == "null");
CHECK(json(json::value_t::object).type_name() == "object"); CHECK(std::string(json(json::value_t::object).type_name()) == "object");
CHECK(json(json::value_t::array).type_name() == "array"); CHECK(std::string(json(json::value_t::array).type_name()) == "array");
CHECK(json(json::value_t::number_integer).type_name() == "number"); CHECK(std::string(json(json::value_t::number_integer).type_name()) == "number");
CHECK(json(json::value_t::number_unsigned).type_name() == "number"); CHECK(std::string(json(json::value_t::number_unsigned).type_name()) == "number");
CHECK(json(json::value_t::number_float).type_name() == "number"); CHECK(std::string(json(json::value_t::number_float).type_name()) == "number");
CHECK(json(json::value_t::boolean).type_name() == "boolean"); CHECK(std::string(json(json::value_t::boolean).type_name()) == "boolean");
CHECK(json(json::value_t::string).type_name() == "string"); CHECK(std::string(json(json::value_t::string).type_name()) == "string");
CHECK(json(json::value_t::discarded).type_name() == "discarded"); CHECK(std::string(json(json::value_t::discarded).type_name()) == "discarded");
} }
SECTION("string escape") SECTION("string escape")

View file

@ -91,14 +91,19 @@ TEST_CASE("deserialization")
{ {
SECTION("stream") SECTION("stream")
{ {
std::stringstream ss1, ss2, ss3; std::stringstream ss1, ss2, ss3, ss4;
ss1 << "[\"foo\",1,2,3,false,{\"one\":1}"; ss1 << "[\"foo\",1,2,3,false,{\"one\":1}";
ss2 << "[\"foo\",1,2,3,false,{\"one\":1}"; ss2 << "[\"foo\",1,2,3,false,{\"one\":1}";
ss3 << "[\"foo\",1,2,3,false,{\"one\":1}"; ss3 << "[\"foo\",1,2,3,false,{\"one\":1}";
ss4 << "[\"foo\",1,2,3,false,{\"one\":1}";
CHECK_THROWS_AS(json::parse(ss1), json::parse_error&); CHECK_THROWS_AS(json::parse(ss1), json::parse_error&);
CHECK_THROWS_WITH(json::parse(ss2), CHECK_THROWS_WITH(json::parse(ss2),
"[json.exception.parse_error.101] parse error at 29: syntax error - unexpected end of input; expected ']'"); "[json.exception.parse_error.101] parse error at 29: syntax error - unexpected end of input; expected ']'");
CHECK(not json::accept(ss3)); CHECK(not json::accept(ss3));
json j_error;
CHECK_NOTHROW(j_error = json::parse(ss1, nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("string") SECTION("string")
@ -108,6 +113,10 @@ TEST_CASE("deserialization")
CHECK_THROWS_WITH(json::parse(s), CHECK_THROWS_WITH(json::parse(s),
"[json.exception.parse_error.101] parse error at 29: syntax error - unexpected end of input; expected ']'"); "[json.exception.parse_error.101] parse error at 29: syntax error - unexpected end of input; expected ']'");
CHECK(not json::accept(s)); CHECK(not json::accept(s));
json j_error;
CHECK_NOTHROW(j_error = json::parse(s, nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("operator<<") SECTION("operator<<")
@ -260,6 +269,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u'}; uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u'};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 2") SECTION("case 2")
@ -267,6 +280,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u', '1'}; uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u', '1'};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 3") SECTION("case 3")
@ -274,6 +291,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u', '1', '1', '1', '1', '1', '1', '1', '1'}; uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', '\\', 'u', '1', '1', '1', '1', '1', '1', '1', '1'};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 4") SECTION("case 4")
@ -281,6 +302,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', 'u', '1', '1', '1', '1', '1', '1', '1', '1', '\\'}; uint8_t v[] = {'\"', 'a', 'a', 'a', 'a', 'a', 'a', 'u', '1', '1', '1', '1', '1', '1', '1', '1', '\\'};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 5") SECTION("case 5")
@ -288,6 +313,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 0x7F, 0xC1}; uint8_t v[] = {'\"', 0x7F, 0xC1};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 6") SECTION("case 6")
@ -295,6 +324,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 0x7F, 0xDF, 0x7F}; uint8_t v[] = {'\"', 0x7F, 0xDF, 0x7F};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 7") SECTION("case 7")
@ -302,6 +335,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 0x7F, 0xDF, 0xC0}; uint8_t v[] = {'\"', 0x7F, 0xDF, 0xC0};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 8") SECTION("case 8")
@ -309,6 +346,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 0x7F, 0xE0, 0x9F}; uint8_t v[] = {'\"', 0x7F, 0xE0, 0x9F};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 9") SECTION("case 9")
@ -316,6 +357,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 0x7F, 0xEF, 0xC0}; uint8_t v[] = {'\"', 0x7F, 0xEF, 0xC0};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 10") SECTION("case 10")
@ -323,6 +368,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 0x7F, 0xED, 0x7F}; uint8_t v[] = {'\"', 0x7F, 0xED, 0x7F};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 11") SECTION("case 11")
@ -330,6 +379,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 0x7F, 0xF0, 0x8F}; uint8_t v[] = {'\"', 0x7F, 0xF0, 0x8F};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 12") SECTION("case 12")
@ -337,6 +390,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 0x7F, 0xF0, 0xC0}; uint8_t v[] = {'\"', 0x7F, 0xF0, 0xC0};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 13") SECTION("case 13")
@ -344,6 +401,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 0x7F, 0xF3, 0x7F}; uint8_t v[] = {'\"', 0x7F, 0xF3, 0x7F};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 14") SECTION("case 14")
@ -351,6 +412,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 0x7F, 0xF3, 0xC0}; uint8_t v[] = {'\"', 0x7F, 0xF3, 0xC0};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 15") SECTION("case 15")
@ -358,6 +423,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'\"', 0x7F, 0xF4, 0x7F}; uint8_t v[] = {'\"', 0x7F, 0xF4, 0x7F};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
SECTION("case 16") SECTION("case 16")
@ -365,6 +434,10 @@ TEST_CASE("deserialization")
uint8_t v[] = {'{', '\"', '\"', ':', '1', '1'}; uint8_t v[] = {'{', '\"', '\"', ':', '1', '1'};
CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&); CHECK_THROWS_AS(json::parse(std::begin(v), std::end(v)), json::parse_error&);
CHECK(not json::accept(std::begin(v), std::end(v))); CHECK(not json::accept(std::begin(v), std::end(v)));
json j_error;
CHECK_NOTHROW(j_error = json::parse(std::begin(v), std::end(v), nullptr, false));
CHECK(j_error.is_discarded());
} }
} }
} }

View file

@ -1126,6 +1126,25 @@ TEST_CASE("single MessagePack roundtrip")
// compare parsed JSON values // compare parsed JSON values
CHECK(j1 == j2); CHECK(j1 == j2);
SECTION("roundtrips")
{
SECTION("std::ostringstream")
{
std::ostringstream ss;
json::to_msgpack(j1, ss);
json j3 = json::from_msgpack(ss.str());
CHECK(j1 == j3);
}
SECTION("std::string")
{
std::string s;
json::to_msgpack(j1, s);
json j3 = json::from_msgpack(s);
CHECK(j1 == j3);
}
}
// check with different start index // check with different start index
packed.insert(packed.begin(), 5, 0xff); packed.insert(packed.begin(), 5, 0xff);
CHECK(j1 == json::from_msgpack(packed, 5)); CHECK(j1 == json::from_msgpack(packed, 5));
@ -1292,15 +1311,60 @@ TEST_CASE("MessagePack roundtrips", "[hide]")
std::ifstream f_json(filename); std::ifstream f_json(filename);
json j1 = json::parse(f_json); json j1 = json::parse(f_json);
// parse MessagePack file SECTION("std::vector<uint8_t>")
std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary); {
std::vector<uint8_t> packed((std::istreambuf_iterator<char>(f_msgpack)), // parse MessagePack file
std::istreambuf_iterator<char>()); std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
json j2; std::vector<uint8_t> packed(
CHECK_NOTHROW(j2 = json::from_msgpack(packed)); (std::istreambuf_iterator<char>(f_msgpack)),
std::istreambuf_iterator<char>());
json j2;
CHECK_NOTHROW(j2 = json::from_msgpack(packed));
// compare parsed JSON values // compare parsed JSON values
CHECK(j1 == j2); CHECK(j1 == j2);
}
SECTION("std::ifstream")
{
// parse MessagePack file
std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
json j2;
CHECK_NOTHROW(j2 = json::from_msgpack(f_msgpack));
// compare parsed JSON values
CHECK(j1 == j2);
}
SECTION("uint8_t* and size")
{
// parse MessagePack file
std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
std::vector<uint8_t> packed(
(std::istreambuf_iterator<char>(f_msgpack)),
std::istreambuf_iterator<char>());
json j2;
CHECK_NOTHROW(j2 = json::from_msgpack({packed.data(), packed.size()}));
// compare parsed JSON values
CHECK(j1 == j2);
}
SECTION("output to output adapters")
{
// parse MessagePack file
std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
std::vector<uint8_t> packed(
(std::istreambuf_iterator<char>(f_msgpack)),
std::istreambuf_iterator<char>());
SECTION("std::vector<uint8_t>")
{
std::vector<uint8_t> vec;
json::to_msgpack(j1, vec);
CHECK(vec == packed);
}
}
} }
} }
} }