Skip to content

Commit

Permalink
fixed #47 (added erase function to remove key form object)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Mar 24, 2015
1 parent 43417c3 commit 6fc52f3
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,18 @@ class basic_json
return m_value.object->operator[](key);
}

/// remove element from an object given a key
inline size_type erase(const typename object_t::key_type& key)
{
// at only works for objects
if (m_type != value_t::object)
{
throw std::runtime_error("cannot use at with " + type_name());
}

return m_value.object->erase(key);
}

/// find an element in an object
inline iterator find(typename object_t::key_type key)
{
Expand Down
12 changes: 12 additions & 0 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,18 @@ class basic_json
return m_value.object->operator[](key);
}

/// remove element from an object given a key
inline size_type erase(const typename object_t::key_type& key)
{
// at only works for objects
if (m_type != value_t::object)
{
throw std::runtime_error("cannot use at with " + type_name());
}

return m_value.object->erase(key);
}

/// find an element in an object
inline iterator find(typename object_t::key_type key)
{
Expand Down
80 changes: 80 additions & 0 deletions test/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2367,6 +2367,86 @@ TEST_CASE("element access")
}
}

SECTION("remove specified element")
{
SECTION("remove element")
{
CHECK(j.find("integer") != j.end());
CHECK(j.erase("integer") == 1);
CHECK(j.find("integer") == j.end());
CHECK(j.erase("integer") == 0);

CHECK(j.find("boolean") != j.end());
CHECK(j.erase("boolean") == 1);
CHECK(j.find("boolean") == j.end());
CHECK(j.erase("boolean") == 0);

CHECK(j.find("null") != j.end());
CHECK(j.erase("null") == 1);
CHECK(j.find("null") == j.end());
CHECK(j.erase("null") == 0);

CHECK(j.find("string") != j.end());
CHECK(j.erase("string") == 1);
CHECK(j.find("string") == j.end());
CHECK(j.erase("string") == 0);

CHECK(j.find("floating") != j.end());
CHECK(j.erase("floating") == 1);
CHECK(j.find("floating") == j.end());
CHECK(j.erase("floating") == 0);

CHECK(j.find("object") != j.end());
CHECK(j.erase("object") == 1);
CHECK(j.find("object") == j.end());
CHECK(j.erase("object") == 0);

CHECK(j.find("array") != j.end());
CHECK(j.erase("array") == 1);
CHECK(j.find("array") == j.end());
CHECK(j.erase("array") == 0);
}

SECTION("access on non-object type")
{
SECTION("null")
{
json j_nonobject(json::value_t::null);
CHECK_THROWS_AS(j_nonobject.erase("foo"), std::runtime_error);
}

SECTION("boolean")
{
json j_nonobject(json::value_t::boolean);
CHECK_THROWS_AS(j_nonobject.erase("foo"), std::runtime_error);
}

SECTION("string")
{
json j_nonobject(json::value_t::string);
CHECK_THROWS_AS(j_nonobject.erase("foo"), std::runtime_error);
}

SECTION("array")
{
json j_nonobject(json::value_t::array);
CHECK_THROWS_AS(j_nonobject.erase("foo"), std::runtime_error);
}

SECTION("number (integer)")
{
json j_nonobject(json::value_t::number_integer);
CHECK_THROWS_AS(j_nonobject.erase("foo"), std::runtime_error);
}

SECTION("number (floating-point)")
{
json j_nonobject(json::value_t::number_float);
CHECK_THROWS_AS(j_nonobject.erase("foo"), std::runtime_error);
}
}
}

SECTION("find an element in an object")
{
SECTION("existing element")
Expand Down

1 comment on commit 6fc52f3

@gnzlbg
Copy link

@gnzlbg gnzlbg commented on 6fc52f3 Mar 24, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

Please sign in to comment.