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
1c8d5dc2
Commit
1c8d5dc2
authored
Jul 12, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added another insert function
parent
092bf39f
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
129 additions
and
0 deletions
+129
-0
doc/examples/insert__ilist.cpp
doc/examples/insert__ilist.cpp
+16
-0
doc/examples/insert__ilist.link
doc/examples/insert__ilist.link
+1
-0
doc/examples/insert__ilist.output
doc/examples/insert__ilist.output
+2
-0
src/json.hpp
src/json.hpp
+39
-0
src/json.hpp.re2c
src/json.hpp.re2c
+39
-0
test/unit.cpp
test/unit.cpp
+32
-0
No files found.
doc/examples/insert__ilist.cpp
0 → 100644
View file @
1c8d5dc2
#include <json.hpp>
using
namespace
nlohmann
;
int
main
()
{
// create a JSON array
json
v
=
{
1
,
2
,
3
,
4
};
// insert range from v2 before the end of array v
auto
new_pos
=
v
.
insert
(
v
.
end
(),
{
7
,
8
,
9
});
// output new array and result of insert call
std
::
cout
<<
*
new_pos
<<
'\n'
;
std
::
cout
<<
v
<<
'\n'
;
}
doc/examples/insert__ilist.link
0 → 100644
View file @
1c8d5dc2
<a target="_blank" href="http://melpon.org/wandbox/permlink/ciOQKs1KoHMSAWN3"><b>online</b></a>
\ No newline at end of file
doc/examples/insert__ilist.output
0 → 100644
View file @
1c8d5dc2
7
[1,2,3,4,7,8,9]
src/json.hpp
View file @
1c8d5dc2
...
@@ -3876,6 +3876,45 @@ class basic_json
...
@@ -3876,6 +3876,45 @@ class basic_json
return
result
;
return
result
;
}
}
/*!
@brief inserts elements
Inserts elements from initializer list @a ilist before iterator @a pos.
@param[in] pos iterator before which the content will be inserted; may be
the end() iterator
@param[in] ilist initializer list to insert the values from
@throw std::domain_error if called on JSON values other than arrays
@throw std::domain_error if @a pos is not an iterator of *this
@return iterator pointing to the first element inserted, or @a pos if
`ilist` is empty
@complexity Linear in `ilist.size()` plus linear in the distance between @a
pos and end of the container.
@liveexample{The example shows how insert is used.,insert__ilist}
*/
iterator
insert
(
const_iterator
pos
,
std
::
initializer_list
<
basic_json
>
ilist
)
{
// insert only works for arrays
if
(
m_type
!=
value_t
::
array
)
{
throw
std
::
domain_error
(
"cannot use insert() with "
+
type_name
());
}
// check if iterator pos fits to this JSON value
if
(
pos
.
m_object
!=
this
)
{
throw
std
::
domain_error
(
"iterator does not fit current value"
);
}
// insert to array and return iterator
iterator
result
(
this
);
result
.
m_it
.
array_iterator
=
m_value
.
array
->
insert
(
pos
.
m_it
.
array_iterator
,
ilist
);
return
result
;
}
/*!
/*!
@brief exchanges the values
@brief exchanges the values
...
...
src/json.hpp.re2c
View file @
1c8d5dc2
...
@@ -3876,6 +3876,45 @@ class basic_json
...
@@ -3876,6 +3876,45 @@ class basic_json
return result;
return result;
}
}
/*!
@brief inserts elements
Inserts elements from initializer list @a ilist before iterator @a pos.
@param[in] pos iterator before which the content will be inserted; may be
the end() iterator
@param[in] ilist initializer list to insert the values from
@throw std::domain_error if called on JSON values other than arrays
@throw std::domain_error if @a pos is not an iterator of *this
@return iterator pointing to the first element inserted, or @a pos if
`ilist` is empty
@complexity Linear in `ilist.size()` plus linear in the distance between @a
pos and end of the container.
@liveexample{The example shows how insert is used.,insert__ilist}
*/
iterator insert(const_iterator pos, std::initializer_list<basic_json> ilist)
{
// insert only works for arrays
if (m_type != value_t::array)
{
throw std::domain_error("cannot use insert() with " + type_name());
}
// check if iterator pos fits to this JSON value
if (pos.m_object != this)
{
throw std::domain_error("iterator does not fit current value");
}
// insert to array and return iterator
iterator result(this);
result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, ilist);
return result;
}
/*!
/*!
@brief exchanges the values
@brief exchanges the values
...
...
test/unit.cpp
View file @
1c8d5dc2
...
@@ -6743,6 +6743,36 @@ TEST_CASE("modifiers")
...
@@ -6743,6 +6743,36 @@ TEST_CASE("modifiers")
}
}
}
}
SECTION
(
"initializer list at position"
)
{
SECTION
(
"insert before begin()"
)
{
auto
it
=
j_array
.
insert
(
j_array
.
begin
(),
{
7
,
8
,
9
});
CHECK
(
j_array
.
size
()
==
7
);
CHECK
(
*
it
==
json
(
7
));
CHECK
(
j_array
.
begin
()
==
it
);
CHECK
(
j_array
==
json
({
7
,
8
,
9
,
1
,
2
,
3
,
4
}));
}
SECTION
(
"insert in the middle"
)
{
auto
it
=
j_array
.
insert
(
j_array
.
begin
()
+
2
,
{
7
,
8
,
9
});
CHECK
(
j_array
.
size
()
==
7
);
CHECK
(
*
it
==
json
(
7
));
CHECK
((
it
-
j_array
.
begin
())
==
2
);
CHECK
(
j_array
==
json
({
1
,
2
,
7
,
8
,
9
,
3
,
4
}));
}
SECTION
(
"insert before end()"
)
{
auto
it
=
j_array
.
insert
(
j_array
.
end
(),
{
7
,
8
,
9
});
CHECK
(
j_array
.
size
()
==
7
);
CHECK
(
*
it
==
json
(
7
));
CHECK
((
j_array
.
end
()
-
it
)
==
3
);
CHECK
(
j_array
==
json
({
1
,
2
,
3
,
4
,
7
,
8
,
9
}));
}
}
SECTION
(
"invalid iterator"
)
SECTION
(
"invalid iterator"
)
{
{
// pass iterator to a different array
// pass iterator to a different array
...
@@ -6753,6 +6783,7 @@ TEST_CASE("modifiers")
...
@@ -6753,6 +6783,7 @@ TEST_CASE("modifiers")
CHECK_THROWS_AS
(
j_array
.
insert
(
j_another_array
.
end
(),
10
,
11
),
std
::
domain_error
);
CHECK_THROWS_AS
(
j_array
.
insert
(
j_another_array
.
end
(),
10
,
11
),
std
::
domain_error
);
CHECK_THROWS_AS
(
j_array
.
insert
(
j_another_array
.
end
(),
j_yet_another_array
.
begin
(),
CHECK_THROWS_AS
(
j_array
.
insert
(
j_another_array
.
end
(),
j_yet_another_array
.
begin
(),
j_yet_another_array
.
end
()),
std
::
domain_error
);
j_yet_another_array
.
end
()),
std
::
domain_error
);
CHECK_THROWS_AS
(
j_array
.
insert
(
j_another_array
.
end
(),
{
1
,
2
,
3
,
4
}),
std
::
domain_error
);
}
}
SECTION
(
"non-array type"
)
SECTION
(
"non-array type"
)
...
@@ -6765,6 +6796,7 @@ TEST_CASE("modifiers")
...
@@ -6765,6 +6796,7 @@ TEST_CASE("modifiers")
CHECK_THROWS_AS
(
j_nonarray
.
insert
(
j_nonarray
.
end
(),
10
,
11
),
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonarray
.
insert
(
j_nonarray
.
end
(),
10
,
11
),
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonarray
.
insert
(
j_nonarray
.
end
(),
j_yet_another_array
.
begin
(),
CHECK_THROWS_AS
(
j_nonarray
.
insert
(
j_nonarray
.
end
(),
j_yet_another_array
.
begin
(),
j_yet_another_array
.
end
()),
std
::
domain_error
);
j_yet_another_array
.
end
()),
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonarray
.
insert
(
j_nonarray
.
end
(),
{
1
,
2
,
3
,
4
}),
std
::
domain_error
);
}
}
}
}
...
...
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