Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
json
Commits
84195daf
Commit
84195daf
authored
Apr 09, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added erase function for arrays
parent
9ea3dd9b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
121 additions
and
2 deletions
+121
-2
json.hpp
src/json.hpp
+18
-1
json.hpp.re2c
src/json.hpp.re2c
+18
-1
unit.cpp
test/unit.cpp
+85
-0
No files found.
src/json.hpp
View file @
84195daf
...
...
@@ -1190,7 +1190,7 @@ class basic_json
/// remove element from an object given a key
inline
size_type
erase
(
const
typename
object_t
::
key_type
&
key
)
{
//
at
only works for objects
//
this erase
only works for objects
if
(
m_type
!=
value_t
::
object
)
{
throw
std
::
runtime_error
(
"cannot use erase with "
+
type_name
());
...
...
@@ -1199,6 +1199,23 @@ class basic_json
return
m_value
.
object
->
erase
(
key
);
}
/// remove element from an array given an index
inline
void
erase
(
const
size_type
pos
)
{
// this erase only works for arrays
if
(
m_type
!=
value_t
::
array
)
{
throw
std
::
runtime_error
(
"cannot use erase with "
+
type_name
());
}
if
(
pos
>=
size
())
{
throw
std
::
out_of_range
(
"index out of range"
);
}
m_value
.
array
->
erase
(
m_value
.
array
->
begin
()
+
static_cast
<
difference_type
>
(
pos
));
}
/// find an element in an object
inline
iterator
find
(
typename
object_t
::
key_type
key
)
{
...
...
src/json.hpp.re2c
View file @
84195daf
...
...
@@ -1190,7 +1190,7 @@ class basic_json
/// remove element from an object given a key
inline size_type erase(const typename object_t::key_type& key)
{
//
at
only works for objects
//
this erase
only works for objects
if (m_type != value_t::object)
{
throw std::runtime_error("cannot use erase with " + type_name());
...
...
@@ -1199,6 +1199,23 @@ class basic_json
return m_value.object->erase(key);
}
/// remove element from an array given an index
inline void erase(const size_type pos)
{
// this erase only works for arrays
if (m_type != value_t::array)
{
throw std::runtime_error("cannot use erase with " + type_name());
}
if (pos >= size())
{
throw std::out_of_range("index out of range");
}
m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(pos));
}
/// find an element in an object
inline iterator find(typename object_t::key_type key)
{
...
...
test/unit.cpp
View file @
84195daf
...
...
@@ -2291,6 +2291,91 @@ TEST_CASE("element access")
}
}
}
SECTION
(
"remove specified element"
)
{
SECTION
(
"remove element"
)
{
{
json
jarray
=
{
1
,
true
,
nullptr
,
"string"
,
42.23
,
json
::
object
(),
{
1
,
2
,
3
}};
jarray
.
erase
(
0
);
CHECK
(
jarray
==
json
({
true
,
nullptr
,
"string"
,
42.23
,
json
::
object
(),
{
1
,
2
,
3
}}));
}
{
json
jarray
=
{
1
,
true
,
nullptr
,
"string"
,
42.23
,
json
::
object
(),
{
1
,
2
,
3
}};
jarray
.
erase
(
1
);
CHECK
(
jarray
==
json
({
1
,
nullptr
,
"string"
,
42.23
,
json
::
object
(),
{
1
,
2
,
3
}}));
}
{
json
jarray
=
{
1
,
true
,
nullptr
,
"string"
,
42.23
,
json
::
object
(),
{
1
,
2
,
3
}};
jarray
.
erase
(
2
);
CHECK
(
jarray
==
json
({
1
,
true
,
"string"
,
42.23
,
json
::
object
(),
{
1
,
2
,
3
}}));
}
{
json
jarray
=
{
1
,
true
,
nullptr
,
"string"
,
42.23
,
json
::
object
(),
{
1
,
2
,
3
}};
jarray
.
erase
(
3
);
CHECK
(
jarray
==
json
({
1
,
true
,
nullptr
,
42.23
,
json
::
object
(),
{
1
,
2
,
3
}}));
}
{
json
jarray
=
{
1
,
true
,
nullptr
,
"string"
,
42.23
,
json
::
object
(),
{
1
,
2
,
3
}};
jarray
.
erase
(
4
);
CHECK
(
jarray
==
json
({
1
,
true
,
nullptr
,
"string"
,
json
::
object
(),
{
1
,
2
,
3
}}));
}
{
json
jarray
=
{
1
,
true
,
nullptr
,
"string"
,
42.23
,
json
::
object
(),
{
1
,
2
,
3
}};
jarray
.
erase
(
5
);
CHECK
(
jarray
==
json
({
1
,
true
,
nullptr
,
"string"
,
42.23
,
{
1
,
2
,
3
}}));
}
{
json
jarray
=
{
1
,
true
,
nullptr
,
"string"
,
42.23
,
json
::
object
(),
{
1
,
2
,
3
}};
jarray
.
erase
(
6
);
CHECK
(
jarray
==
json
({
1
,
true
,
nullptr
,
"string"
,
42.23
,
json
::
object
()}));
}
{
json
jarray
=
{
1
,
true
,
nullptr
,
"string"
,
42.23
,
json
::
object
(),
{
1
,
2
,
3
}};
CHECK_THROWS_AS
(
jarray
.
erase
(
7
),
std
::
out_of_range
);
}
}
SECTION
(
"access on non-object type"
)
{
SECTION
(
"null"
)
{
json
j_nonobject
(
json
::
value_t
::
null
);
CHECK_THROWS_AS
(
j_nonobject
.
erase
(
0
),
std
::
runtime_error
);
}
SECTION
(
"boolean"
)
{
json
j_nonobject
(
json
::
value_t
::
boolean
);
CHECK_THROWS_AS
(
j_nonobject
.
erase
(
0
),
std
::
runtime_error
);
}
SECTION
(
"string"
)
{
json
j_nonobject
(
json
::
value_t
::
string
);
CHECK_THROWS_AS
(
j_nonobject
.
erase
(
0
),
std
::
runtime_error
);
}
SECTION
(
"object"
)
{
json
j_nonobject
(
json
::
value_t
::
object
);
CHECK_THROWS_AS
(
j_nonobject
.
erase
(
0
),
std
::
runtime_error
);
}
SECTION
(
"number (integer)"
)
{
json
j_nonobject
(
json
::
value_t
::
number_integer
);
CHECK_THROWS_AS
(
j_nonobject
.
erase
(
0
),
std
::
runtime_error
);
}
SECTION
(
"number (floating-point)"
)
{
json
j_nonobject
(
json
::
value_t
::
number_float
);
CHECK_THROWS_AS
(
j_nonobject
.
erase
(
0
),
std
::
runtime_error
);
}
}
}
}
SECTION
(
"object"
)
...
...
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