Skip to content

Commit

Permalink
change to nlohmann_json
Browse files Browse the repository at this point in the history
  • Loading branch information
yhmtsai committed Nov 27, 2023
1 parent 290b99e commit 0672805
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 33 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,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)
Expand Down
35 changes: 18 additions & 17 deletions extension/property_tree/include/property_tree/json_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <type_traits>


#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
#include <nlohmann/json.hpp>


#include <ginkgo/core/config/property_tree.hpp>
Expand All @@ -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<long long int>()};
} else if (dom.is_boolean()) {
ptree = gko::config::pnode{dom.template get<bool>()};
} else if (dom.is_number_float()) {
ptree = gko::config::pnode{dom.template get<double>()};
} else if (dom.is_string()) {
ptree = gko::config::pnode{
std::string(dom.template get<std::string>())};
} else {
ptree = gko::config::pnode{std::string(dom.GetString())};
ptree = gko::config::pnode{};
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion extension/property_tree/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ function(gkoext_pt_create_test test_name)
endfunction(gkoext_pt_create_test)


gkoext_pt_create_test(json_parser rapidjson ginkgo)
gkoext_pt_create_test(json_parser nlohmann_json::nlohmann_json ginkgo)
24 changes: 10 additions & 14 deletions extension/property_tree/test/json_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#include <gtest/gtest.h>
#include <rapidjson/document.h>
#include <nlohmann/json.hpp>


#include <ginkgo/core/config/property_tree.hpp>
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion third_party/nlohmann_json/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 0672805

Please sign in to comment.