From edbd47317d8b099390e215888e819d25db853c4b Mon Sep 17 00:00:00 2001 From: Huangzizhou Date: Tue, 28 Nov 2023 13:36:30 -0500 Subject: [PATCH] add logic for json --- src/polysolve/JSONUtils.hpp | 4 ++-- tests/test_json.cpp | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/polysolve/JSONUtils.hpp b/src/polysolve/JSONUtils.hpp index 848cda3..9693ebf 100644 --- a/src/polysolve/JSONUtils.hpp +++ b/src/polysolve/JSONUtils.hpp @@ -38,13 +38,13 @@ namespace nlohmann if (j.is_number()) { - matrix.resize(1, 1); + matrix.resize((nrows == -1) ? 1 : nrows, (ncols == -1) ? 1 : ncols); matrix(0, 0) = j.get(); } else if (j.is_array()) { if (j.size() == 0) // empty array - matrix.resize(0, 0); + matrix.setZero(std::max(0, nrows), std::max(0, ncols)); else if (j.at(0).is_number()) // 1D array { assert(nrows == -1 || ncols == -1); // at least one dimension can be resized diff --git a/tests/test_json.cpp b/tests/test_json.cpp index 99c06cf..2756e14 100644 --- a/tests/test_json.cpp +++ b/tests/test_json.cpp @@ -59,9 +59,13 @@ TEST_CASE("json_to_eigen", "[json]") REQUIRE(c.cols() == 2); // empty json array + a = input[4]; + REQUIRE(a.size() == 0); + b = input[4]; + REQUIRE(b.size() == 0); c = input[4]; REQUIRE(c.size() == 0); - + // single number c = input[5]; REQUIRE(c(0) == 1);