mirror of
https://github.com/nlohmann/json.git
synced 2026-09-06 08:17:59 +00:00
Fall back to plain-object encoding when _ArrayType_ is not a string
write_bjdata_ndarray() looked up _ArrayType_ by calling get<string_t>() directly, which throws type_error.302 when the annotation is not a string (e.g. a number, null, boolean, array, or object). Per the documented BJData ndarray contract, an object only qualifies for the compact ndarray encoding if _ArrayType_ names a known type; anything else must fall back to plain-object encoding, the same way an unknown type-name string already does. Add an is_string() check before the get<string_t>() call so a non-string _ArrayType_ takes the existing "unrecognized type name" fallback path instead of throwing. Fixes #5398. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -1672,6 +1672,16 @@ class binary_writer
|
||||
};
|
||||
|
||||
string_t key = "_ArrayType_";
|
||||
// the type name is looked up as a string below; a non-string
|
||||
// annotation (e.g. a number, null, or an array) cannot name a known
|
||||
// dtype, so it is treated the same as an unrecognized type name and
|
||||
// falls back to a plain object encoding instead of throwing
|
||||
// type_error.302 out of get<string_t>()
|
||||
if (!value.at(key).is_string())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// use get<string_t>() instead of static_cast<string_t> to avoid an
|
||||
// ambiguous conversion under explicit instantiation on C++17 (see #4825)
|
||||
auto it = bjdtype.find(value.at(key).template get<string_t>());
|
||||
|
||||
@@ -18680,6 +18680,16 @@ class binary_writer
|
||||
};
|
||||
|
||||
string_t key = "_ArrayType_";
|
||||
// the type name is looked up as a string below; a non-string
|
||||
// annotation (e.g. a number, null, or an array) cannot name a known
|
||||
// dtype, so it is treated the same as an unrecognized type name and
|
||||
// falls back to a plain object encoding instead of throwing
|
||||
// type_error.302 out of get<string_t>()
|
||||
if (!value.at(key).is_string())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// use get<string_t>() instead of static_cast<string_t> to avoid an
|
||||
// ambiguous conversion under explicit instantiation on C++17 (see #4825)
|
||||
auto it = bjdtype.find(value.at(key).template get<string_t>());
|
||||
|
||||
@@ -2746,6 +2746,39 @@ TEST_CASE("BJData")
|
||||
CHECK(json::from_bjdata(json::to_bjdata(j_size), true, true) == j_size);
|
||||
}
|
||||
|
||||
SECTION("ndarray whose _ArrayType_ is not a string stays as object")
|
||||
{
|
||||
// the type name is looked up as a string below the annotation
|
||||
// check; a non-string _ArrayType_ cannot name a known dtype,
|
||||
// so calling get<string_t>() on it would throw type_error.302
|
||||
// instead of falling back like an unrecognized type name
|
||||
// already does (see GitHub issue #5398)
|
||||
json const j_number = json({{"_ArrayType_", 1}, {"_ArraySize_", {2}}, {"_ArrayData_", {1, 2}}});
|
||||
const auto out_number = json::to_bjdata(j_number);
|
||||
CHECK(out_number.at(0) == '{');
|
||||
CHECK(json::from_bjdata(out_number) == j_number);
|
||||
|
||||
json const j_null = json({{"_ArrayType_", nullptr}, {"_ArraySize_", {2}}, {"_ArrayData_", {1, 2}}});
|
||||
const auto out_null = json::to_bjdata(j_null);
|
||||
CHECK(out_null.at(0) == '{');
|
||||
CHECK(json::from_bjdata(out_null) == j_null);
|
||||
|
||||
json const j_bool = json({{"_ArrayType_", true}, {"_ArraySize_", {2}}, {"_ArrayData_", {1, 2}}});
|
||||
const auto out_bool = json::to_bjdata(j_bool);
|
||||
CHECK(out_bool.at(0) == '{');
|
||||
CHECK(json::from_bjdata(out_bool) == j_bool);
|
||||
|
||||
json const j_array = json({{"_ArrayType_", {"uint8"}}, {"_ArraySize_", {2}}, {"_ArrayData_", {1, 2}}});
|
||||
const auto out_array = json::to_bjdata(j_array);
|
||||
CHECK(out_array.at(0) == '{');
|
||||
CHECK(json::from_bjdata(out_array) == j_array);
|
||||
|
||||
json const j_object = json({{"_ArrayType_", {{"a", 1}}}, {"_ArraySize_", {2}}, {"_ArrayData_", {1, 2}}});
|
||||
const auto out_object = json::to_bjdata(j_object);
|
||||
CHECK(out_object.at(0) == '{');
|
||||
CHECK(json::from_bjdata(out_object) == j_object);
|
||||
}
|
||||
|
||||
SECTION("ndarray whose dimensions overflow stays as object")
|
||||
{
|
||||
// the product of the dimensions wraps around std::size_t to 0
|
||||
|
||||
Reference in New Issue
Block a user