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
e5906048
Unverified
Commit
e5906048
authored
Jul 28, 2020
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🐛
fix a bug due to missing overloads in ordered_map container
parent
1b8efed0
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
213 additions
and
6 deletions
+213
-6
include/nlohmann/ordered_map.hpp
include/nlohmann/ordered_map.hpp
+34
-3
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+34
-3
test/CMakeLists.txt
test/CMakeLists.txt
+1
-0
test/src/unit-ordered_map.cpp
test/src/unit-ordered_map.cpp
+125
-0
test/src/unit-regression.cpp
test/src/unit-regression.cpp
+19
-0
No files found.
include/nlohmann/ordered_map.hpp
View file @
e5906048
...
...
@@ -30,7 +30,7 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
ordered_map
(
std
::
initializer_list
<
T
>
init
,
const
Allocator
&
alloc
=
Allocator
()
)
:
Container
{
init
,
alloc
}
{}
std
::
pair
<
iterator
,
bool
>
emplace
(
key_type
&
&
key
,
T
&&
t
)
std
::
pair
<
iterator
,
bool
>
emplace
(
const
key_type
&
key
,
T
&&
t
)
{
for
(
auto
it
=
this
->
begin
();
it
!=
this
->
end
();
++
it
)
{
...
...
@@ -43,9 +43,40 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
return
{
--
this
->
end
(),
true
};
}
T
&
operator
[](
Key
&
&
key
)
T
&
operator
[](
const
Key
&
key
)
{
return
emplace
(
std
::
move
(
key
),
T
{}).
first
->
second
;
return
emplace
(
key
,
T
{}).
first
->
second
;
}
const
T
&
operator
[](
const
Key
&
key
)
const
{
return
at
(
key
);
}
T
&
at
(
const
Key
&
key
)
{
for
(
auto
it
=
this
->
begin
();
it
!=
this
->
end
();
++
it
)
{
if
(
it
->
first
==
key
)
{
return
it
->
second
;
}
}
throw
std
::
out_of_range
(
"key not found"
);
}
const
T
&
at
(
const
Key
&
key
)
const
{
for
(
auto
it
=
this
->
begin
();
it
!=
this
->
end
();
++
it
)
{
if
(
it
->
first
==
key
)
{
return
it
->
second
;
}
}
throw
std
::
out_of_range
(
"key not found"
);
}
size_type
erase
(
const
Key
&
key
)
...
...
single_include/nlohmann/json.hpp
View file @
e5906048
...
...
@@ -16420,7 +16420,7 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
ordered_map
(
std
::
initializer_list
<
T
>
init
,
const
Allocator
&
alloc
=
Allocator
()
)
:
Container
{
init
,
alloc
}
{}
std
::
pair
<
iterator
,
bool
>
emplace
(
key_type
&
&
key
,
T
&&
t
)
std
::
pair
<
iterator
,
bool
>
emplace
(
const
key_type
&
key
,
T
&&
t
)
{
for
(
auto
it
=
this
->
begin
();
it
!=
this
->
end
();
++
it
)
{
...
...
@@ -16433,9 +16433,40 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
return
{
--
this
->
end
(),
true
};
}
T
&
operator
[](
Key
&
&
key
)
T
&
operator
[](
const
Key
&
key
)
{
return
emplace
(
std
::
move
(
key
),
T
{}).
first
->
second
;
return
emplace
(
key
,
T
{}).
first
->
second
;
}
const
T
&
operator
[](
const
Key
&
key
)
const
{
return
at
(
key
);
}
T
&
at
(
const
Key
&
key
)
{
for
(
auto
it
=
this
->
begin
();
it
!=
this
->
end
();
++
it
)
{
if
(
it
->
first
==
key
)
{
return
it
->
second
;
}
}
throw
std
::
out_of_range
(
"key not found"
);
}
const
T
&
at
(
const
Key
&
key
)
const
{
for
(
auto
it
=
this
->
begin
();
it
!=
this
->
end
();
++
it
)
{
if
(
it
->
first
==
key
)
{
return
it
->
second
;
}
}
throw
std
::
out_of_range
(
"key not found"
);
}
size_type
erase
(
const
Key
&
key
)
...
...
test/CMakeLists.txt
View file @
e5906048
...
...
@@ -126,6 +126,7 @@ set(files
src/unit-msgpack.cpp
src/unit-noexcept.cpp
src/unit-ordered_json.cpp
src/unit-ordered_map.cpp
src/unit-pointer_access.cpp
src/unit-readme.cpp
src/unit-reference_access.cpp
...
...
test/src/unit-ordered_map.cpp
0 → 100644
View file @
e5906048
/*
__ _____ _____ _____
__| | __| | | | JSON for Modern C++ (test suite)
| | |__ | | | | | | version 3.9.0
|_____|_____|_____|_|___| https://github.com/nlohmann/json
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT
Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "doctest_compatibility.h"
#include <nlohmann/json.hpp>
using
nlohmann
::
ordered_map
;
TEST_CASE
(
"ordered_map"
)
{
SECTION
(
"constructor"
)
{
SECTION
(
"constructor from iterator range"
)
{
std
::
map
<
std
::
string
,
std
::
string
>
m
{{
"eins"
,
"one"
},
{
"zwei"
,
"two"
},
{
"drei"
,
"three"
}};
ordered_map
<
std
::
string
,
std
::
string
>
om
(
m
.
begin
(),
m
.
end
());
CHECK
(
om
.
size
()
==
3
);
}
SECTION
(
"copy assignment"
)
{
std
::
map
<
std
::
string
,
std
::
string
>
m
{{
"eins"
,
"one"
},
{
"zwei"
,
"two"
},
{
"drei"
,
"three"
}};
ordered_map
<
std
::
string
,
std
::
string
>
om
(
m
.
begin
(),
m
.
end
());
const
auto
com
=
om
;
CHECK
(
com
.
size
()
==
3
);
}
}
SECTION
(
"at"
)
{
std
::
map
<
std
::
string
,
std
::
string
>
m
{{
"eins"
,
"one"
},
{
"zwei"
,
"two"
},
{
"drei"
,
"three"
}};
ordered_map
<
std
::
string
,
std
::
string
>
om
(
m
.
begin
(),
m
.
end
());
const
auto
com
=
om
;
SECTION
(
"with Key&&"
)
{
CHECK
(
om
.
at
(
std
::
string
(
"eins"
))
==
std
::
string
(
"one"
));
CHECK
(
com
.
at
(
std
::
string
(
"eins"
))
==
std
::
string
(
"one"
));
CHECK_THROWS_AS
(
om
.
at
(
std
::
string
(
"vier"
)),
std
::
out_of_range
);
CHECK_THROWS_AS
(
com
.
at
(
std
::
string
(
"vier"
)),
std
::
out_of_range
);
}
SECTION
(
"with const Key&&"
)
{
const
std
::
string
eins
=
"eins"
;
const
std
::
string
vier
=
"vier"
;
CHECK
(
om
.
at
(
eins
)
==
std
::
string
(
"one"
));
CHECK
(
com
.
at
(
eins
)
==
std
::
string
(
"one"
));
CHECK_THROWS_AS
(
om
.
at
(
vier
),
std
::
out_of_range
);
CHECK_THROWS_AS
(
com
.
at
(
vier
),
std
::
out_of_range
);
}
SECTION
(
"with string literal"
)
{
CHECK
(
om
.
at
(
"eins"
)
==
std
::
string
(
"one"
));
CHECK
(
com
.
at
(
"eins"
)
==
std
::
string
(
"one"
));
CHECK_THROWS_AS
(
om
.
at
(
"vier"
),
std
::
out_of_range
);
CHECK_THROWS_AS
(
com
.
at
(
"vier"
),
std
::
out_of_range
);
}
}
SECTION
(
"operator[]"
)
{
std
::
map
<
std
::
string
,
std
::
string
>
m
{{
"eins"
,
"one"
},
{
"zwei"
,
"two"
},
{
"drei"
,
"three"
}};
ordered_map
<
std
::
string
,
std
::
string
>
om
(
m
.
begin
(),
m
.
end
());
const
auto
com
=
om
;
SECTION
(
"with Key&&"
)
{
CHECK
(
om
[
std
::
string
(
"eins"
)]
==
std
::
string
(
"one"
));
CHECK
(
com
[
std
::
string
(
"eins"
)]
==
std
::
string
(
"one"
));
CHECK
(
om
[
std
::
string
(
"vier"
)]
==
std
::
string
(
""
));
CHECK
(
om
.
size
()
==
4
);
}
SECTION
(
"with const Key&&"
)
{
const
std
::
string
eins
=
"eins"
;
const
std
::
string
vier
=
"vier"
;
CHECK
(
om
[
eins
]
==
std
::
string
(
"one"
));
CHECK
(
com
[
eins
]
==
std
::
string
(
"one"
));
CHECK
(
om
[
vier
]
==
std
::
string
(
""
));
CHECK
(
om
.
size
()
==
4
);
}
SECTION
(
"with string literal"
)
{
CHECK
(
om
[
"eins"
]
==
std
::
string
(
"one"
));
CHECK
(
com
[
"eins"
]
==
std
::
string
(
"one"
));
CHECK
(
om
[
"vier"
]
==
std
::
string
(
""
));
CHECK
(
om
.
size
()
==
4
);
}
}
}
test/src/unit-regression.cpp
View file @
e5906048
...
...
@@ -1976,6 +1976,25 @@ TEST_CASE("regression tests")
json
result
=
json
::
from_cbor
(
data
,
true
,
false
);
CHECK
(
result
.
is_discarded
());
}
SECTION
(
"issue #2315 - json.update and vector<pair>does not work with ordered_json"
)
{
nlohmann
::
ordered_json
jsonAnimals
=
{{
"animal"
,
"dog"
}};
nlohmann
::
ordered_json
jsonCat
=
{{
"animal"
,
"cat"
}};
jsonAnimals
.
update
(
jsonCat
);
CHECK
(
jsonAnimals
[
"animal"
]
==
"cat"
);
std
::
vector
<
std
::
pair
<
std
::
string
,
int64_t
>>
intData
=
{
std
::
make_pair
(
"aaaa"
,
11
),
std
::
make_pair
(
"bbb"
,
222
)
};
nlohmann
::
ordered_json
jsonObj
;
for
(
const
auto
&
data
:
intData
)
{
jsonObj
[
data
.
first
]
=
data
.
second
;
}
CHECK
(
jsonObj
[
"aaaa"
]
==
11
);
CHECK
(
jsonObj
[
"bbb"
]
==
222
);
}
}
#if !defined(JSON_NOEXCEPTION)
...
...
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