Skip to content

Commit

Permalink
Merge branch 'develop' into move-construction
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Jul 30, 2017
2 parents 93bb712 + 96dd4ff commit aad5521
Show file tree
Hide file tree
Showing 10 changed files with 970 additions and 995 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ cmake_minimum_required(VERSION 3.0)
# define the project
project(nlohmann_json VERSION 2.1.1 LANGUAGES CXX)

enable_testing()

option(JSON_BuildTests "Build the unit tests" ON)

# define project variables
Expand All @@ -26,6 +24,7 @@ target_include_directories(${JSON_TARGET_NAME} INTERFACE

# create and configure the unit test target
if(JSON_BuildTests)
enable_testing()
add_subdirectory(test)
endif()

Expand Down
Binary file removed doc/images/scanner.png
Binary file not shown.
1,653 changes: 705 additions & 948 deletions src/json.hpp

Large diffs are not rendered by default.

81 changes: 72 additions & 9 deletions test/src/unit-cbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,25 @@ TEST_CASE("single CBOR roundtrip")
// compare parsed JSON values
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
packed.insert(packed.begin(), 5, 0xff);
CHECK(j1 == json::from_cbor(packed, 5));
Expand Down Expand Up @@ -1519,16 +1538,60 @@ TEST_CASE("CBOR roundtrips", "[hide]")
std::ifstream f_json(filename);
json j1 = json::parse(f_json);

// 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));
SECTION("std::vector<uint8_t>")
{
// 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));

// compare parsed JSON values
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);
}

// 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);
}
}
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions test/src/unit-class_const_iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,32 +182,32 @@ TEST_CASE("const_iterator class")
{
json j(json::value_t::null);
json::const_iterator it = j.cbegin();
CHECK_THROWS_AS(it->type_name(), json::invalid_iterator&);
CHECK_THROWS_WITH(it->type_name(), "[json.exception.invalid_iterator.214] cannot get value");
CHECK_THROWS_AS(std::string(it->type_name()), json::invalid_iterator&);
CHECK_THROWS_WITH(std::string(it->type_name()), "[json.exception.invalid_iterator.214] cannot get value");
}

SECTION("number")
{
json j(17);
json::const_iterator it = j.cbegin();
CHECK(it->type_name() == "number");
CHECK(std::string(it->type_name()) == "number");
it = j.cend();
CHECK_THROWS_AS(it->type_name(), json::invalid_iterator&);
CHECK_THROWS_WITH(it->type_name(), "[json.exception.invalid_iterator.214] cannot get value");
CHECK_THROWS_AS(std::string(it->type_name()), json::invalid_iterator&);
CHECK_THROWS_WITH(std::string(it->type_name()), "[json.exception.invalid_iterator.214] cannot get value");
}

SECTION("object")
{
json j({{"foo", "bar"}});
json::const_iterator it = j.cbegin();
CHECK(it->type_name() == "string");
CHECK(std::string(it->type_name()) == "string");
}

SECTION("array")
{
json j({1, 2, 3, 4});
json::const_iterator it = j.cbegin();
CHECK(it->type_name() == "number");
CHECK(std::string(it->type_name()) == "number");
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions test/src/unit-class_iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,32 +166,32 @@ TEST_CASE("iterator class")
{
json j(json::value_t::null);
json::iterator it = j.begin();
CHECK_THROWS_AS(it->type_name(), json::invalid_iterator&);
CHECK_THROWS_WITH(it->type_name(), "[json.exception.invalid_iterator.214] cannot get value");
CHECK_THROWS_AS(std::string(it->type_name()), json::invalid_iterator&);
CHECK_THROWS_WITH(std::string(it->type_name()), "[json.exception.invalid_iterator.214] cannot get value");
}

SECTION("number")
{
json j(17);
json::iterator it = j.begin();
CHECK(it->type_name() == "number");
CHECK(std::string(it->type_name()) == "number");
it = j.end();
CHECK_THROWS_AS(it->type_name(), json::invalid_iterator&);
CHECK_THROWS_WITH(it->type_name(), "[json.exception.invalid_iterator.214] cannot get value");
CHECK_THROWS_AS(std::string(it->type_name()), json::invalid_iterator&);
CHECK_THROWS_WITH(std::string(it->type_name()), "[json.exception.invalid_iterator.214] cannot get value");
}

SECTION("object")
{
json j({{"foo", "bar"}});
json::iterator it = j.begin();
CHECK(it->type_name() == "string");
CHECK(std::string(it->type_name()) == "string");
}

SECTION("array")
{
json j({1, 2, 3, 4});
json::iterator it = j.begin();
CHECK(it->type_name() == "number");
CHECK(std::string(it->type_name()) == "number");
}
}
}
Expand Down
25 changes: 22 additions & 3 deletions test/src/unit-class_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,31 @@ json parser_helper(const std::string& s)
{
json 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;
}

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")
Expand Down Expand Up @@ -590,8 +609,8 @@ TEST_CASE("parser class")

SECTION("overflow")
{
// overflows during parsing yield an exception, but is accepted anyway
CHECK(accept_helper("1.18973e+4932"));
// overflows during parsing
CHECK(not accept_helper("1.18973e+4932"));
}

SECTION("invalid numbers")
Expand Down
20 changes: 10 additions & 10 deletions test/src/unit-convenience.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
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);
CHECK(ss.str() == escaped);
}
Expand All @@ -45,15 +45,15 @@ 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_unsigned).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");
CHECK(json(json::value_t::discarded).type_name() == "discarded");
CHECK(std::string(json(json::value_t::null).type_name()) == "null");
CHECK(std::string(json(json::value_t::object).type_name()) == "object");
CHECK(std::string(json(json::value_t::array).type_name()) == "array");
CHECK(std::string(json(json::value_t::number_integer).type_name()) == "number");
CHECK(std::string(json(json::value_t::number_unsigned).type_name()) == "number");
CHECK(std::string(json(json::value_t::number_float).type_name()) == "number");
CHECK(std::string(json(json::value_t::boolean).type_name()) == "boolean");
CHECK(std::string(json(json::value_t::string).type_name()) == "string");
CHECK(std::string(json(json::value_t::discarded).type_name()) == "discarded");
}

SECTION("string escape")
Expand Down
Loading

0 comments on commit aad5521

Please sign in to comment.