From 7fc8e4e0d3df6a2ca21e334cadab8c6658515998 Mon Sep 17 00:00:00 2001 From: Qianqian Fang Date: Tue, 17 May 2022 23:52:40 -0400 Subject: [PATCH] use a loop to test 0 ndarray dimension --- include/nlohmann/detail/input/binary_reader.hpp | 11 ++++++++++- single_include/nlohmann/json.hpp | 11 ++++++++++- tests/src/unit-bjdata.cpp | 2 ++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 89ae352f1..58c012ecb 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -2113,13 +2113,22 @@ class binary_reader { return false; } - if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1) || std::count(dim.begin(), dim.end(), 0) > 0) // return normal array size if 1D row vector or total length is 0 + if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector { result = dim.at(dim.size() - 1); return true; } if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format { + for (auto i : dim) + { + if ( i == 0 ) + { + result = 0; + return true; + } + } + string_t key = "_ArraySize_"; if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size()))) { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2b628cbcf..9895aa32b 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10587,13 +10587,22 @@ class binary_reader { return false; } - if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1) || std::count(dim.begin(), dim.end(), 0) > 0) // return normal array size if 1D row vector or total length is 0 + if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector { result = dim.at(dim.size() - 1); return true; } if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format { + for (auto i : dim) + { + if ( i == 0 ) + { + result = 0; + return true; + } + } + string_t key = "_ArraySize_"; if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size()))) { diff --git a/tests/src/unit-bjdata.cpp b/tests/src/unit-bjdata.cpp index 8bebca416..1a34ef840 100644 --- a/tests/src/unit-bjdata.cpp +++ b/tests/src/unit-bjdata.cpp @@ -2363,6 +2363,7 @@ TEST_CASE("BJData") { // create vector with two elements of the same type std::vector v_0 = {'[', '$', 'i', '#', '[', ']'}; + std::vector v_E = {'[', '$', 'i', '#', '[', 'i', 2, 'i', 0, ']'}; std::vector v_i = {'[', '$', 'i', '#', '[', 'i', 1, 'i', 2, ']', 0x7F, 0x7F}; std::vector v_U = {'[', '$', 'U', '#', '[', 'i', 1, 'i', 2, ']', 0xFF, 0xFF}; std::vector v_I = {'[', '$', 'I', '#', '[', 'i', 1, 'i', 2, ']', 0xFF, 0x7F, 0xFF, 0x7F}; @@ -2377,6 +2378,7 @@ TEST_CASE("BJData") // check if vector is parsed correctly CHECK(json::from_bjdata(v_0) == json::array()); + CHECK(json::from_bjdata(v_E) == json::array()); CHECK(json::from_bjdata(v_i) == json({127, 127})); CHECK(json::from_bjdata(v_U) == json({255, 255})); CHECK(json::from_bjdata(v_I) == json({32767, 32767}));