# HG changeset patch # User David Demelier # Date 1525447920 -7200 # Node ID 3d8fae8e44477ffd2725c0eb8ac480c4f38d38d3 # Parent 7e40ed196bb0c062b500d541d6ef7959816ebf5c json_util: rename parser to document diff -r 7e40ed196bb0 -r 3d8fae8e4447 cpp/json_util/json_util.hpp --- a/cpp/json_util/json_util.hpp Mon Apr 30 17:26:41 2018 +0200 +++ b/cpp/json_util/json_util.hpp Fri May 04 17:32:00 2018 +0200 @@ -258,35 +258,27 @@ * This class helps destructuring insecure JSON input by returning optional * values if they are not present or invalid. */ -class parser { -private: - nlohmann::json& json_; - +class document : public nlohmann::json { public: /** - * Construct the parser. - * - * \param document the document + * Inherited constructor. */ - inline parser(nlohmann::json& document) noexcept - : json_(document) - { - } + using nlohmann::json::json; /** * Get a value from the document object. * * \param key the property key - * \return the value or boost::none if not found + * \return the value or boost::none if not found or not convertible */ template inline boost::optional get(const std::string& key) const noexcept { static_assert(parser_type_traits::value, "type not supported"); - const auto it = json_.find(key); + const auto it = find(key); - if (it == json_.end()) + if (it == end()) return boost::none; return parser_type_traits::get(*it); @@ -307,9 +299,9 @@ { static_assert(parser_type_traits::value, "type not supported"); - const auto it = json_.find(key); + const auto it = find(key); - if (it == json_.end()) + if (it == end()) return boost::optional(std::forward(def)); return parser_type_traits::get(*it); diff -r 7e40ed196bb0 -r 3d8fae8e4447 cpp/json_util/test/main.cpp --- a/cpp/json_util/test/main.cpp Mon Apr 30 17:26:41 2018 +0200 +++ b/cpp/json_util/test/main.cpp Fri May 04 17:32:00 2018 +0200 @@ -26,14 +26,12 @@ class test_fixture { protected: - nlohmann::json json_{ + document document_{ { "boolean", true }, { "int", -1234 }, { "uint", 1234U }, { "string", "str" } }; - - parser parser_{json_}; }; BOOST_FIXTURE_TEST_SUITE(test_fixture_suite, test_fixture) @@ -42,46 +40,46 @@ BOOST_AUTO_TEST_CASE(boolean) { - const auto v = parser_.get("boolean"); + const auto v = document_.get("boolean"); BOOST_TEST(v); BOOST_TEST(*v); - BOOST_TEST(!parser_.get("int")); - BOOST_TEST(!parser_.get("int")); - BOOST_TEST(!parser_.get("uint")); - BOOST_TEST(!parser_.get("string")); + BOOST_TEST(!document_.get("int")); + BOOST_TEST(!document_.get("int")); + BOOST_TEST(!document_.get("uint")); + BOOST_TEST(!document_.get("string")); } BOOST_AUTO_TEST_CASE(integer) { - const auto v = parser_.get("int"); + const auto v = document_.get("int"); BOOST_TEST(v); BOOST_TEST(*v == -1234); - BOOST_TEST(!parser_.get("bool")); - BOOST_TEST(!parser_.get("string")); + BOOST_TEST(!document_.get("bool")); + BOOST_TEST(!document_.get("string")); } BOOST_AUTO_TEST_CASE(unsigned_integer) { - const auto v = parser_.get("uint"); + const auto v = document_.get("uint"); BOOST_TEST(v); BOOST_TEST(*v == 1234U); - BOOST_TEST(!parser_.get("bool")); - BOOST_TEST(!parser_.get("int")); - BOOST_TEST(!parser_.get("string")); + BOOST_TEST(!document_.get("bool")); + BOOST_TEST(!document_.get("int")); + BOOST_TEST(!document_.get("string")); } BOOST_AUTO_TEST_CASE(string) { - const auto v = parser_.get("string"); + const auto v = document_.get("string"); BOOST_TEST(v); BOOST_TEST(*v == "str"); - BOOST_TEST(!parser_.get("bool")); - BOOST_TEST(!parser_.get("int")); - BOOST_TEST(!parser_.get("uint")); + BOOST_TEST(!document_.get("bool")); + BOOST_TEST(!document_.get("int")); + BOOST_TEST(!document_.get("uint")); } BOOST_AUTO_TEST_SUITE_END() @@ -90,23 +88,23 @@ BOOST_AUTO_TEST_CASE(boolean) { - BOOST_TEST(*parser_.optional("undefined", true)); - BOOST_TEST(!*parser_.optional("undefined", false)); + BOOST_TEST(*document_.optional("undefined", true)); + BOOST_TEST(!*document_.optional("undefined", false)); } BOOST_AUTO_TEST_CASE(integer) { - BOOST_TEST(*parser_.optional("undefined", 1234) == 1234); + BOOST_TEST(*document_.optional("undefined", 1234) == 1234); } BOOST_AUTO_TEST_CASE(unsigned_integer) { - BOOST_TEST(*parser_.optional("undefined", 1234U) == 1234U); + BOOST_TEST(*document_.optional("undefined", 1234U) == 1234U); } BOOST_AUTO_TEST_CASE(string) { - BOOST_TEST(*parser_.optional("undefined", "foo") == "foo"); + BOOST_TEST(*document_.optional("undefined", "foo") == "foo"); } BOOST_AUTO_TEST_SUITE_END() @@ -115,29 +113,29 @@ BOOST_AUTO_TEST_CASE(boolean) { - BOOST_TEST(!parser_.optional("int", true)); - BOOST_TEST(!parser_.optional("uint", true)); - BOOST_TEST(!parser_.optional("string", true)); + BOOST_TEST(!document_.optional("int", true)); + BOOST_TEST(!document_.optional("uint", true)); + BOOST_TEST(!document_.optional("string", true)); } BOOST_AUTO_TEST_CASE(integer) { - BOOST_TEST(!parser_.optional("boolean", 1234)); - BOOST_TEST(!parser_.optional("string", 1234)); + BOOST_TEST(!document_.optional("boolean", 1234)); + BOOST_TEST(!document_.optional("string", 1234)); } BOOST_AUTO_TEST_CASE(unsigned_integer) { - BOOST_TEST(!parser_.optional("boolean", 1234)); - BOOST_TEST(!parser_.optional("int", 1234)); - BOOST_TEST(!parser_.optional("string", 1234)); + BOOST_TEST(!document_.optional("boolean", 1234)); + BOOST_TEST(!document_.optional("int", 1234)); + BOOST_TEST(!document_.optional("string", 1234)); } BOOST_AUTO_TEST_CASE(string) { - BOOST_TEST(!parser_.optional("boolean", "foo")); - BOOST_TEST(!parser_.optional("int", "foo")); - BOOST_TEST(!parser_.optional("uint", "foo")); + BOOST_TEST(!document_.optional("boolean", "foo")); + BOOST_TEST(!document_.optional("int", "foo")); + BOOST_TEST(!document_.optional("uint", "foo")); } BOOST_AUTO_TEST_SUITE_END()