diff --git a/CMakeLists.txt b/CMakeLists.txt index c4f5ee0097d..9e71962bc77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -256,6 +256,9 @@ if(GINKGO_BUILD_BENCHMARKS) find_package(gflags 2.2.2 QUIET) find_package(nlohmann_json 3.9.1 QUIET) endif() +if(GINKGO_BUILD_CONFIG_PARSER) + find_package(nlohmann_json 3.9.1 QUIET) +endif() # System provided, third party libraries (not bundled!) set(GINKGO_HAVE_HWLOC 0) diff --git a/extension/property_tree/include/property_tree/json_parser.hpp b/extension/property_tree/include/property_tree/json_parser.hpp index bc8460ff8df..da2083ba794 100644 --- a/extension/property_tree/include/property_tree/json_parser.hpp +++ b/extension/property_tree/include/property_tree/json_parser.hpp @@ -40,8 +40,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -#include -#include +#include #include @@ -51,29 +50,31 @@ namespace gko { namespace extension { -void json_parser(gko::config::pnode& ptree, rapidjson::Value& dom) +inline void json_parser(gko::config::pnode& ptree, const nlohmann::json& dom) { - if (dom.IsArray()) { - auto array = dom.GetArray(); - int num = array.Size(); + if (dom.is_array()) { + int num = dom.size(); ptree.get_array().resize(num); for (int i = 0; i < num; i++) { - json_parser(ptree.at(i), array[i]); + json_parser(ptree.at(i), dom[i]); } - } else if (dom.IsObject()) { + } else if (dom.is_object()) { auto& list = ptree.get_map(); - for (auto& m : dom.GetObject()) { - json_parser(list[m.name.GetString()], dom[m.name.GetString()]); + for (auto& m : dom.items()) { + json_parser(list[m.key()], m.value()); } } else { - if (dom.IsInt64()) { - ptree = gko::config::pnode{dom.GetInt64()}; - } else if (dom.IsBool()) { - ptree = gko::config::pnode{dom.GetBool()}; - } else if (dom.IsDouble()) { - ptree = gko::config::pnode{dom.GetDouble()}; + if (dom.is_number_integer()) { + ptree = gko::config::pnode{dom.template get()}; + } else if (dom.is_boolean()) { + ptree = gko::config::pnode{dom.template get()}; + } else if (dom.is_number_float()) { + ptree = gko::config::pnode{dom.template get()}; + } else if (dom.is_string()) { + ptree = gko::config::pnode{ + std::string(dom.template get())}; } else { - ptree = gko::config::pnode{std::string(dom.GetString())}; + ptree = gko::config::pnode{}; } } } diff --git a/extension/property_tree/test/CMakeLists.txt b/extension/property_tree/test/CMakeLists.txt index 5b4b4fb0014..b461ca8dc1d 100644 --- a/extension/property_tree/test/CMakeLists.txt +++ b/extension/property_tree/test/CMakeLists.txt @@ -24,4 +24,4 @@ function(gkoext_pt_create_test test_name) endfunction(gkoext_pt_create_test) -gkoext_pt_create_test(json_parser rapidjson ginkgo) \ No newline at end of file +gkoext_pt_create_test(json_parser nlohmann_json::nlohmann_json ginkgo) \ No newline at end of file diff --git a/extension/property_tree/test/json_parser.cpp b/extension/property_tree/test/json_parser.cpp index 9a7d9cc8511..49b05085e11 100644 --- a/extension/property_tree/test/json_parser.cpp +++ b/extension/property_tree/test/json_parser.cpp @@ -34,7 +34,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -#include +#include #include @@ -50,9 +50,7 @@ using namespace gko::extension; TEST(JsonParser, ReadObject) { const char json[] = R"({"base": "ReferenceExecutor"})"; - rapidjson::StringStream s(json); - rapidjson::Document d; - d.ParseStream(s); + auto d = nlohmann::json::parse(json); gko::config::pnode ptree; json_parser(ptree, d); @@ -65,21 +63,20 @@ TEST(JsonParser, ReadInput2) { const char json[] = R"({"base": "Csr", - "dim": [3, 4], + "dim": [3, 4.5], "exec": {"base": "ReferenceExecutor"}})"; std::istringstream iss(R"({ base: "Csr" dim: [ 3 - 4 + 4.5 ] exec: { base: "ReferenceExecutor" } -})"); - rapidjson::StringStream s(json); - rapidjson::Document d; - d.ParseStream(s); +} +)"); + auto d = nlohmann::json::parse(json); gko::config::pnode ptree; json_parser(ptree, d); @@ -100,10 +97,9 @@ TEST(JsonParser, ReadInput3) { name: "B" } -])"); - rapidjson::StringStream s(json); - rapidjson::Document d; - d.ParseStream(s); +] +)"); + auto d = nlohmann::json::parse(json); gko::config::pnode ptree; json_parser(ptree, d); diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt index 749d24b87bd..08f10c95362 100644 --- a/third_party/CMakeLists.txt +++ b/third_party/CMakeLists.txt @@ -16,6 +16,12 @@ if(GINKGO_BUILD_BENCHMARKS) endif() endif() +if(GINKGO_BUILD_CONFIG_PARSER) + if (NOT nlohmann_json_FOUND) + add_subdirectory(nlohmann_json) + endif() +endif() + if (GINKGO_TEST_NONDEFAULT_STREAM) add_subdirectory(identify_stream_usage) endif() diff --git a/third_party/nlohmann_json/CMakeLists.txt b/third_party/nlohmann_json/CMakeLists.txt index 6f413e458b9..b76d5d8b0b8 100644 --- a/third_party/nlohmann_json/CMakeLists.txt +++ b/third_party/nlohmann_json/CMakeLists.txt @@ -6,5 +6,6 @@ FetchContent_Declare( GIT_TAG v3.9.1 ) set(JSON_BuildTests OFF CACHE INTERNAL "") -set(JSON_Install OFF CACHE INTERNAL "") +set(JSON_Install ON CACHE INTERNAL "") +# default cmake installation is in CMAKE_INSTALL_PREFIX/share/cmake FetchContent_MakeAvailable(nlohmann_json)