diff --git a/CMakeLists.txt b/CMakeLists.txt index 74f4013..e2fb0ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -112,6 +112,7 @@ if(valijson_BUILD_TESTS) if(Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) + list(APPEND TEST_SOURCES tests/test_boost_json_adapter.cpp) list(APPEND TEST_SOURCES tests/test_property_tree_adapter.cpp) endif() @@ -153,7 +154,7 @@ if(valijson_BUILD_TESTS) set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) - target_compile_definitions(test_suite PRIVATE "VALIJSON_BUILD_PROPERTY_TREE_ADAPTER") + target_compile_definitions(test_suite PRIVATE "VALIJSON_BUILD_BOOST_ADAPTERS") endif() if(Poco_FOUND) diff --git a/include/valijson/adapters/boost_json_adapter.hpp b/include/valijson/adapters/boost_json_adapter.hpp index 232609c..4c1ee55 100644 --- a/include/valijson/adapters/boost_json_adapter.hpp +++ b/include/valijson/adapters/boost_json_adapter.hpp @@ -400,7 +400,7 @@ public: bool getString(std::string &result) const { if (m_value.is_string()) { - result = m_value.get_string(); + result = m_value.get_string().c_str(); return true; } @@ -497,8 +497,7 @@ public: * @brief Class for iterating over values held in a JSON array. * * This class provides a JSON array iterator that dereferences as an instance of - * BoostJsonAdapter representing a value stored in the array. It has been - * implemented using the boost iterator_facade template. + * BoostJsonAdapter representing a value stored in the array. * * @see BoostJsonArray */ @@ -590,7 +589,6 @@ private: * * This class provides a JSON object iterator that dereferences as an instance * of BoostJsonObjectMember representing one of the members of the object. - * It has been implemented using the boost iterator_facade template. * * @see BoostJsonObject * @see BoostJsonObjectMember diff --git a/include/valijson/utils/boost_json_utils.hpp b/include/valijson/utils/boost_json_utils.hpp index b3cbe20..1168903 100644 --- a/include/valijson/utils/boost_json_utils.hpp +++ b/include/valijson/utils/boost_json_utils.hpp @@ -2,14 +2,14 @@ #include -#include +#include #include #include namespace valijson { namespace utils { -inline bool loadDocument(const std::string &path, nlohmann::json &document) +inline bool loadDocument(const std::string &path, boost::json::value &document) { // Load schema JSON from file std::string file; @@ -22,16 +22,17 @@ inline bool loadDocument(const std::string &path, nlohmann::json &document) // Parse schema #if VALIJSON_USE_EXCEPTION try { - document = nlohmann::json::parse(file); - } catch (std::invalid_argument const& exception) { - std::cerr << "nlohmann::json failed to parse the document\n" - << "Parse error:" << exception.what() << "\n"; - return false; - } -#else - document = nlohmann::json::parse(file, nullptr, false); - if (document.is_discarded()) { - std::cerr << "nlohmann::json failed to parse the document."; +#endif + boost::json::error_code errorCode; + boost::json::string_view stringView{file}; + document = boost::json::parse(stringView, errorCode); + if (errorCode) { + std::cerr << "Boost.JSON parsing error: " << errorCode.message(); + return false; + } +#if VALIJSON_USE_EXCEPTION + } catch (std::exception const & exception) { + std::cerr << "Boost.JSON parsing exception: " << exception.what(); return false; } #endif diff --git a/tests/test_adapter_comparison.cpp b/tests/test_adapter_comparison.cpp index b269626..b531efd 100644 --- a/tests/test_adapter_comparison.cpp +++ b/tests/test_adapter_comparison.cpp @@ -20,9 +20,11 @@ #include #include -#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER +#ifdef VALIJSON_BUILD_BOOST_ADAPTERS #include #include +#include +#include #endif #ifdef VALIJSON_BUILD_QT_ADAPTER @@ -157,7 +159,7 @@ TEST_F(TestAdapterComparison, JsonCppVsPicoJson) valijson::adapters::PicoJsonAdapter>(); } -#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER +#ifdef VALIJSON_BUILD_BOOST_ADAPTERS TEST_F(TestAdapterComparison, JsonCppVsPropertyTree) { @@ -166,6 +168,13 @@ TEST_F(TestAdapterComparison, JsonCppVsPropertyTree) valijson::adapters::PropertyTreeAdapter>(); } +TEST_F(TestAdapterComparison, JsonCppVsBoostJson) +{ + testComparison< + valijson::adapters::JsonCppAdapter, + valijson::adapters::BoostJsonAdapter>(); +} + #endif TEST_F(TestAdapterComparison, JsonCppVsRapidJson) @@ -184,12 +193,12 @@ TEST_F(TestAdapterComparison, JsonCppVsRapidJsonCrtAlloc) rapidjson::CrtAllocator> > >(); } + +#ifdef VALIJSON_BUILD_BOOST_ADAPTERS // // PropertyTreeAdapter vs X // ------------------------------------------------------------------------------------------------ -#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER - TEST_F(TestAdapterComparison, PropertyTreeVsPicoJson) { testComparison< @@ -220,6 +229,40 @@ TEST_F(TestAdapterComparison, PropertyTreeVsRapidJsonCrtAlloc) rapidjson::CrtAllocator> > >(); } +// +// BoostJsonAdapter vs X +// ------------------------------------------------------------------------------------------------ + +TEST_F(TestAdapterComparison, BoostJsonVsPicoJson) +{ + testComparison< + valijson::adapters::BoostJsonAdapter, + valijson::adapters::PicoJsonAdapter>(); +} + +TEST_F(TestAdapterComparison, BoostJsonVsBoostJson) +{ + testComparison< + valijson::adapters::BoostJsonAdapter, + valijson::adapters::BoostJsonAdapter>(); +} + +TEST_F(TestAdapterComparison, BoostJsonVsRapidJson) +{ + testComparison< + valijson::adapters::BoostJsonAdapter, + valijson::adapters::RapidJsonAdapter>(); +} + +TEST_F(TestAdapterComparison, BoostJsonVsRapidJsonCrtAlloc) +{ + testComparison< + valijson::adapters::BoostJsonAdapter, + valijson::adapters::GenericRapidJsonAdapter< + rapidjson::GenericValue, + rapidjson::CrtAllocator> > >(); +} + #endif // @@ -322,7 +365,7 @@ TEST_F(TestAdapterComparison, Json11VsPicoJson) valijson::adapters::PicoJsonAdapter>(); } -#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER +#ifdef VALIJSON_BUILD_BOOST_ADAPTERS TEST_F(TestAdapterComparison, Json11VsPropertyTree) { @@ -331,7 +374,14 @@ TEST_F(TestAdapterComparison, Json11VsPropertyTree) valijson::adapters::PropertyTreeAdapter>(); } -#endif // VALIJSON_BUILD_PROPERTY_TREE_ADAPTER +TEST_F(TestAdapterComparison, Json11VsBoostJson) +{ + testComparison< + valijson::adapters::Json11Adapter, + valijson::adapters::BoostJsonAdapter>(); +} + +#endif // VALIJSON_BUILD_BOOST_ADAPTERS // // NlohmannJsonAdapter vs X @@ -381,7 +431,7 @@ TEST_F(TestAdapterComparison, NlohmannJsonVsPicoJson) valijson::adapters::PicoJsonAdapter>(); } -#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER +#ifdef VALIJSON_BUILD_BOOST_ADAPTERS TEST_F(TestAdapterComparison, NlohmannJsonVsPropertyTree) { @@ -390,7 +440,14 @@ TEST_F(TestAdapterComparison, NlohmannJsonVsPropertyTree) valijson::adapters::PropertyTreeAdapter>(); } -#endif // VALIJSON_BUILD_PROPERTY_TREE_ADAPTER +TEST_F(TestAdapterComparison, NlohmannJsonVsBoostJson) +{ + testComparison< + valijson::adapters::NlohmannJsonAdapter, + valijson::adapters::BoostJsonAdapter>(); +} + +#endif // VALIJSON_BUILD_BOOST_ADAPTERS // // QtJsonAdapter vs X @@ -434,7 +491,7 @@ TEST_F(TestAdapterComparison, QtJsonVsPicoJson) valijson::adapters::PicoJsonAdapter>(); } -#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER +#ifdef VALIJSON_BUILD_BOOST_ADAPTERS TEST_F(TestAdapterComparison, QtJsonVsPropertyTree) { @@ -443,7 +500,14 @@ TEST_F(TestAdapterComparison, QtJsonVsPropertyTree) valijson::adapters::PropertyTreeAdapter>(); } -#endif // VALIJSON_BUILD_PROPERTY_TREE_ADAPTER +TEST_F(TestAdapterComparison, QtJsonVsBoostJson) +{ + testComparison< + valijson::adapters::QtJsonAdapter, + valijson::adapters::BoostJsonAdapter>(); +} + +#endif // VALIJSON_BUILD_BOOST_ADAPTERS TEST_F(TestAdapterComparison, QtJsonVsJson11) { @@ -504,7 +568,7 @@ TEST_F(TestAdapterComparison, PocoJsonVsPicoJson) valijson::adapters::PicoJsonAdapter>(); } -#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER +#ifdef VALIJSON_BUILD_BOOST_ADAPTERS TEST_F(TestAdapterComparison, PocoJsonVsPropertyTree) { @@ -513,7 +577,14 @@ TEST_F(TestAdapterComparison, PocoJsonVsPropertyTree) valijson::adapters::PropertyTreeAdapter>(); } -#endif // VALIJSON_BUILD_PROPERTY_TREE_ADAPTER +TEST_F(TestAdapterComparison, PocoJsonVsBoostJson) +{ + testComparison< + valijson::adapters::PocoJsonAdapter, + valijson::adapters::BoostJsonAdapter>(); +} + +#endif // VALIJSON_BUILD_BOOST_ADAPTERS TEST_F(TestAdapterComparison, PocoJsonVsJson11) { diff --git a/tests/test_boost_json_adapter.cpp b/tests/test_boost_json_adapter.cpp index bbc197e..bd88060 100644 --- a/tests/test_boost_json_adapter.cpp +++ b/tests/test_boost_json_adapter.cpp @@ -1,5 +1,7 @@ #include +#include // Needs to be included exactly once in the code to use header-only version of Boost.JSON + #include class TestBoostJsonAdapter : public testing::Test