Skip to content

Commit

Permalink
fixed #45 (added count function for keys in objects)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Mar 22, 2015
1 parent cf829ac commit f2957dc
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ RE2C = re2c
SED = gsed

# additional flags
FLAGS = -Wall -Wextra -pedantic -Weffc++ -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch -Wundef -Wno-unused -Wnon-virtual-dtor -Wreorder
FLAGS = -Wall -Wextra -pedantic -Weffc++ -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch -Wundef -Wno-unused -Wnon-virtual-dtor -Wreorder -Wdeprecated

all: json_unit

Expand Down
8 changes: 7 additions & 1 deletion src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,6 @@ class basic_json
return m_value.object->operator[](key);
}


/// find an element in an object
inline iterator find(typename object_t::key_type key)
{
Expand All @@ -1076,6 +1075,13 @@ class basic_json
return result;
}

/// returns the number of occurrences of a key in an object
inline size_type count(typename object_t::key_type key) const
{
// return 0 for all nonobject types
return (m_type == value_t::object) ? m_value.object->count(key) : 0;
}


///////////////
// iterators //
Expand Down
8 changes: 7 additions & 1 deletion src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,6 @@ class basic_json
return m_value.object->operator[](key);
}


/// find an element in an object
inline iterator find(typename object_t::key_type key)
{
Expand All @@ -1076,6 +1075,13 @@ class basic_json
return result;
}

/// returns the number of occurrences of a key in an object
inline size_type count(typename object_t::key_type key) const
{
// return 0 for all nonobject types
return (m_type == value_t::object) ? m_value.object->count(key) : 0;
}


///////////////
// iterators //
Expand Down
79 changes: 79 additions & 0 deletions test/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2441,6 +2441,85 @@ TEST_CASE("element access")
}
}
}

SECTION("count keys in an object")
{
SECTION("existing element")
{
for (auto key :
{"integer", "floating", "null", "string", "boolean", "object", "array"
})
{
CHECK(j.count(key) == 1);
CHECK(j_const.count(key) == 1);
}
}

SECTION("nonexisting element")
{
CHECK(j.count("foo") == 0);
CHECK(j_const.count("foo") == 0);
}

SECTION("all types")
{
SECTION("null")
{
json j_nonobject(json::value_t::null);
const json j_nonobject_const(j_nonobject);
CHECK(j_nonobject.count("foo") == 0);
CHECK(j_nonobject_const.count("foo") == 0);
}

SECTION("string")
{
json j_nonobject(json::value_t::string);
const json j_nonobject_const(j_nonobject);
CHECK(j_nonobject.count("foo") == 0);
CHECK(j_nonobject_const.count("foo") == 0);
}

SECTION("object")
{
json j_nonobject(json::value_t::object);
const json j_nonobject_const(j_nonobject);
CHECK(j_nonobject.count("foo") == 0);
CHECK(j_nonobject_const.count("foo") == 0);
}

SECTION("array")
{
json j_nonobject(json::value_t::array);
const json j_nonobject_const(j_nonobject);
CHECK(j_nonobject.count("foo") == 0);
CHECK(j_nonobject_const.count("foo") == 0);
}

SECTION("boolean")
{
json j_nonobject(json::value_t::boolean);
const json j_nonobject_const(j_nonobject);
CHECK(j_nonobject.count("foo") == 0);
CHECK(j_nonobject_const.count("foo") == 0);
}

SECTION("number (integer)")
{
json j_nonobject(json::value_t::number_integer);
const json j_nonobject_const(j_nonobject);
CHECK(j_nonobject.count("foo") == 0);
CHECK(j_nonobject_const.count("foo") == 0);
}

SECTION("number (floating-point)")
{
json j_nonobject(json::value_t::number_float);
const json j_nonobject_const(j_nonobject);
CHECK(j_nonobject.count("foo") == 0);
CHECK(j_nonobject_const.count("foo") == 0);
}
}
}
}
}

Expand Down

0 comments on commit f2957dc

Please sign in to comment.