has_shape() return true for NUL type only if element actually exists (fix #132) (#133)

* has_shape() return true for NUL type only if element actually exists
This commit is contained in:
Joshua Helm 2019-11-04 17:00:43 -05:00 committed by Andrew Twyman
parent 8ccf1f0c5e
commit e2e3a11e99
2 changed files with 8 additions and 1 deletions

View File

@ -775,8 +775,10 @@ bool Json::has_shape(const shape & types, string & err) const {
return false;
}
const auto& obj_items = object_items();
for (auto & item : types) {
if ((*this)[item.first].type() != item.second) {
const auto it = obj_items.find(item.first);
if (it == obj_items.cend() || it->second.type() != item.second) {
err = "bad type for " + item.first + " in " + dump();
return false;
}

View File

@ -245,6 +245,11 @@ JSON11_TEST_CASE(json11_test) {
std::string points_json = Json(points).dump();
std::cout << "points_json: " << points_json << "\n";
JSON11_TEST_ASSERT(points_json == "[[1, 2], [10, 20], [100, 200]]");
JSON11_TEST_ASSERT(((Json)(Json::object { { "foo", nullptr } })).has_shape({ { "foo", Json::NUL } }, err) == true);
JSON11_TEST_ASSERT(((Json)(Json::object { { "foo", 1234567 } })).has_shape({ { "foo", Json::NUL } }, err) == false);
JSON11_TEST_ASSERT(((Json)(Json::object { { "bar", 1234567 } })).has_shape({ { "foo", Json::NUL } }, err) == false);
}
#if JSON11_TEST_STANDALONE_MAIN