Add Boost.JSON support

This commit is contained in:
Matt Young 2021-09-10 08:55:03 +02:00
parent 14325788f8
commit f5cf601efa
5 changed files with 102 additions and 29 deletions

View File

@ -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)

View File

@ -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

View File

@ -2,14 +2,14 @@
#include <iostream>
#include <nlohmann/json.hpp>
#include <boost/json.hpp>
#include <valijson/utils/file_utils.hpp>
#include <valijson/exceptions.hpp>
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

View File

@ -20,9 +20,11 @@
#include <valijson/utils/picojson_utils.hpp>
#include <valijson/utils/rapidjson_utils.hpp>
#ifdef VALIJSON_BUILD_PROPERTY_TREE_ADAPTER
#ifdef VALIJSON_BUILD_BOOST_ADAPTERS
#include <valijson/adapters/property_tree_adapter.hpp>
#include <valijson/utils/property_tree_utils.hpp>
#include <valijson/adapters/boost_json_adapter.hpp>
#include <valijson/utils/boost_json_utils.hpp>
#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::UTF8<>,
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)
{

View File

@ -1,5 +1,7 @@
#include <gtest/gtest.h>
#include <boost/json/src.hpp> // Needs to be included exactly once in the code to use header-only version of Boost.JSON
#include <valijson/adapters/boost_json_adapter.hpp>
class TestBoostJsonAdapter : public testing::Test