Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
f2957dc3
Commit
f2957dc3
authored
Mar 22, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed #45 (added count function for keys in objects)
parent
cf829ac2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
94 additions
and
3 deletions
+94
-3
Makefile
Makefile
+1
-1
src/json.hpp
src/json.hpp
+7
-1
src/json.hpp.re2c
src/json.hpp.re2c
+7
-1
test/unit.cpp
test/unit.cpp
+79
-0
No files found.
Makefile
View file @
f2957dc3
...
...
@@ -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
...
...
src/json.hpp
View file @
f2957dc3
...
...
@@ -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
)
{
...
...
@@ -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 //
...
...
src/json.hpp.re2c
View file @
f2957dc3
...
...
@@ -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)
{
...
...
@@ -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 //
...
...
test/unit.cpp
View file @
f2957dc3
...
...
@@ -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
);
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment