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
149ded85
Unverified
Commit
149ded85
authored
Jul 15, 2021
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
♻
simplify destroy() function for primitive types
parent
db980739
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
70 deletions
+74
-70
include/nlohmann/json.hpp
include/nlohmann/json.hpp
+37
-35
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+37
-35
No files found.
include/nlohmann/json.hpp
View file @
149ded85
...
...
@@ -1133,51 +1133,53 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
void
destroy
(
value_t
t
)
noexcept
{
// flatten the current json_value to a heap-allocated stack
std
::
vector
<
basic_json
>
stack
;
// move the top-level items to stack
if
(
t
==
value_t
::
array
)
{
stack
.
reserve
(
array
->
size
());
std
::
move
(
array
->
begin
(),
array
->
end
(),
std
::
back_inserter
(
stack
));
}
else
if
(
t
==
value_t
::
object
)
{
stack
.
reserve
(
object
->
size
());
for
(
auto
&&
it
:
*
object
)
{
stack
.
push_back
(
std
::
move
(
it
.
second
));
}
}
while
(
!
stack
.
empty
())
if
(
t
==
value_t
::
array
||
t
==
value_t
::
object
)
{
// move the last item to local variable to be processed
basic_json
current_item
(
std
::
move
(
stack
.
back
()));
stack
.
pop_back
();
// flatten the current json_value to a heap-allocated stack
std
::
vector
<
basic_json
>
stack
;
// if current_item is array/object, move
// its children to the stack to be processed later
if
(
current_item
.
is_array
())
// move the top-level items to stack
if
(
t
==
value_t
::
array
)
{
std
::
move
(
current_item
.
m_value
.
array
->
begin
(),
current_item
.
m_value
.
array
->
end
(),
std
::
back_inserter
(
stack
));
current_item
.
m_value
.
array
->
clear
();
stack
.
reserve
(
array
->
size
());
std
::
move
(
array
->
begin
(),
array
->
end
(),
std
::
back_inserter
(
stack
));
}
else
if
(
current_item
.
is_object
())
else
{
for
(
auto
&&
it
:
*
current_item
.
m_value
.
object
)
stack
.
reserve
(
object
->
size
());
for
(
auto
&&
it
:
*
object
)
{
stack
.
push_back
(
std
::
move
(
it
.
second
));
}
current_item
.
m_value
.
object
->
clear
();
}
// it's now safe that current_item get destructed
// since it doesn't have any children
while
(
!
stack
.
empty
())
{
// move the last item to local variable to be processed
basic_json
current_item
(
std
::
move
(
stack
.
back
()));
stack
.
pop_back
();
// if current_item is array/object, move
// its children to the stack to be processed later
if
(
current_item
.
is_array
())
{
std
::
move
(
current_item
.
m_value
.
array
->
begin
(),
current_item
.
m_value
.
array
->
end
(),
std
::
back_inserter
(
stack
));
current_item
.
m_value
.
array
->
clear
();
}
else
if
(
current_item
.
is_object
())
{
for
(
auto
&&
it
:
*
current_item
.
m_value
.
object
)
{
stack
.
push_back
(
std
::
move
(
it
.
second
));
}
current_item
.
m_value
.
object
->
clear
();
}
// it's now safe that current_item get destructed
// since it doesn't have any children
}
}
switch
(
t
)
...
...
single_include/nlohmann/json.hpp
View file @
149ded85
...
...
@@ -18168,51 +18168,53 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
void
destroy
(
value_t
t
)
noexcept
{
// flatten the current json_value to a heap-allocated stack
std
::
vector
<
basic_json
>
stack
;
// move the top-level items to stack
if
(
t
==
value_t
::
array
)
{
stack
.
reserve
(
array
->
size
());
std
::
move
(
array
->
begin
(),
array
->
end
(),
std
::
back_inserter
(
stack
));
}
else
if
(
t
==
value_t
::
object
)
{
stack
.
reserve
(
object
->
size
());
for
(
auto
&&
it
:
*
object
)
{
stack
.
push_back
(
std
::
move
(
it
.
second
));
}
}
while
(
!
stack
.
empty
())
if
(
t
==
value_t
::
array
||
t
==
value_t
::
object
)
{
// move the last item to local variable to be processed
basic_json
current_item
(
std
::
move
(
stack
.
back
()));
stack
.
pop_back
();
// flatten the current json_value to a heap-allocated stack
std
::
vector
<
basic_json
>
stack
;
// if current_item is array/object, move
// its children to the stack to be processed later
if
(
current_item
.
is_array
())
// move the top-level items to stack
if
(
t
==
value_t
::
array
)
{
std
::
move
(
current_item
.
m_value
.
array
->
begin
(),
current_item
.
m_value
.
array
->
end
(),
std
::
back_inserter
(
stack
));
current_item
.
m_value
.
array
->
clear
();
stack
.
reserve
(
array
->
size
());
std
::
move
(
array
->
begin
(),
array
->
end
(),
std
::
back_inserter
(
stack
));
}
else
if
(
current_item
.
is_object
())
else
{
for
(
auto
&&
it
:
*
current_item
.
m_value
.
object
)
stack
.
reserve
(
object
->
size
());
for
(
auto
&&
it
:
*
object
)
{
stack
.
push_back
(
std
::
move
(
it
.
second
));
}
current_item
.
m_value
.
object
->
clear
();
}
// it's now safe that current_item get destructed
// since it doesn't have any children
while
(
!
stack
.
empty
())
{
// move the last item to local variable to be processed
basic_json
current_item
(
std
::
move
(
stack
.
back
()));
stack
.
pop_back
();
// if current_item is array/object, move
// its children to the stack to be processed later
if
(
current_item
.
is_array
())
{
std
::
move
(
current_item
.
m_value
.
array
->
begin
(),
current_item
.
m_value
.
array
->
end
(),
std
::
back_inserter
(
stack
));
current_item
.
m_value
.
array
->
clear
();
}
else
if
(
current_item
.
is_object
())
{
for
(
auto
&&
it
:
*
current_item
.
m_value
.
object
)
{
stack
.
push_back
(
std
::
move
(
it
.
second
));
}
current_item
.
m_value
.
object
->
clear
();
}
// it's now safe that current_item get destructed
// since it doesn't have any children
}
}
switch
(
t
)
...
...
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