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
0efaf891
Unverified
Commit
0efaf891
authored
May 28, 2018
by
Niels Lohmann
Committed by
GitHub
May 28, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1089 from theodelrieu/feature/map_conversion
Provide a from_json overload for std::map
parents
db03d093
c5e63fd6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
0 deletions
+61
-0
include/nlohmann/detail/conversions/from_json.hpp
include/nlohmann/detail/conversions/from_json.hpp
+20
-0
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+20
-0
test/src/unit-conversions.cpp
test/src/unit-conversions.cpp
+21
-0
No files found.
include/nlohmann/detail/conversions/from_json.hpp
View file @
0efaf891
...
...
@@ -5,6 +5,7 @@
#include <ciso646> // and, not
#include <forward_list> // forward_list
#include <iterator> // inserter, front_inserter, end
#include <map> // map
#include <string> // string
#include <tuple> // tuple, make_tuple
#include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
...
...
@@ -277,6 +278,25 @@ void from_json(const BasicJsonType& j, std::tuple<Args...>& t)
from_json_tuple_impl
(
j
,
t
,
index_sequence_for
<
Args
...
>
{});
}
template
<
typename
BasicJsonType
,
typename
Key
,
typename
Value
,
typename
=
enable_if_t
<
not
std
::
is_constructible
<
typename
BasicJsonType
::
string_t
,
Key
>
::
value
>>
void
from_json
(
const
BasicJsonType
&
j
,
std
::
map
<
Key
,
Value
>&
m
)
{
if
(
JSON_UNLIKELY
(
not
j
.
is_array
()))
{
JSON_THROW
(
type_error
::
create
(
302
,
"type must be array, but is "
+
std
::
string
(
j
.
type_name
())));
}
for
(
const
auto
&
p
:
j
)
{
if
(
JSON_UNLIKELY
(
not
p
.
is_array
()))
{
JSON_THROW
(
type_error
::
create
(
302
,
"type must be array, but is "
+
std
::
string
(
p
.
type_name
())));
}
m
.
emplace
(
p
.
at
(
0
).
template
get
<
Key
>(),
p
.
at
(
1
).
template
get
<
Value
>());
}
}
struct
from_json_fn
{
private:
...
...
single_include/nlohmann/json.hpp
View file @
0efaf891
...
...
@@ -909,6 +909,7 @@ inline bool operator<(const value_t lhs, const value_t rhs) noexcept
#include <ciso646> // and, not
#include <forward_list> // forward_list
#include <iterator> // inserter, front_inserter, end
#include <map> // map
#include <string> // string
#include <tuple> // tuple, make_tuple
#include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
...
...
@@ -1185,6 +1186,25 @@ void from_json(const BasicJsonType& j, std::tuple<Args...>& t)
from_json_tuple_impl
(
j
,
t
,
index_sequence_for
<
Args
...
>
{});
}
template
<
typename
BasicJsonType
,
typename
Key
,
typename
Value
,
typename
=
enable_if_t
<
not
std
::
is_constructible
<
typename
BasicJsonType
::
string_t
,
Key
>
::
value
>>
void
from_json
(
const
BasicJsonType
&
j
,
std
::
map
<
Key
,
Value
>&
m
)
{
if
(
JSON_UNLIKELY
(
not
j
.
is_array
()))
{
JSON_THROW
(
type_error
::
create
(
302
,
"type must be array, but is "
+
std
::
string
(
j
.
type_name
())));
}
for
(
const
auto
&
p
:
j
)
{
if
(
JSON_UNLIKELY
(
not
p
.
is_array
()))
{
JSON_THROW
(
type_error
::
create
(
302
,
"type must be array, but is "
+
std
::
string
(
p
.
type_name
())));
}
m
.
emplace
(
p
.
at
(
0
).
template
get
<
Key
>(),
p
.
at
(
1
).
template
get
<
Value
>());
}
}
struct
from_json_fn
{
private:
...
...
test/src/unit-conversions.cpp
View file @
0efaf891
...
...
@@ -1079,6 +1079,26 @@ TEST_CASE("value conversion")
j5
.
get
<
std
::
unordered_set
<
std
::
string
>>
();
}
SECTION
(
"std::map (array of pairs)"
)
{
std
::
map
<
int
,
int
>
m
{{
0
,
1
},
{
1
,
2
},
{
2
,
3
}};
json
j6
=
m
;
auto
m2
=
j6
.
get
<
std
::
map
<
int
,
int
>>
();
CHECK
(
m
==
m2
);
json
j7
=
{
0
,
1
,
2
,
3
};
CHECK_THROWS_AS
((
j7
.
get
<
std
::
map
<
int
,
int
>>
()),
json
::
type_error
&
);
CHECK_THROWS_WITH
((
j7
.
get
<
std
::
map
<
int
,
int
>>
()),
"[json.exception.type_error.302] type must be array, but is number"
);
SECTION
(
"superfluous entries"
)
{
json
j8
=
{{
0
,
1
,
2
},
{
1
,
2
,
3
},
{
2
,
3
,
4
}};
m2
=
j8
.
get
<
std
::
map
<
int
,
int
>>
();
CHECK
(
m
==
m2
);
}
}
SECTION
(
"exception in case of a non-object type"
)
{
CHECK_THROWS_AS
((
json
().
get
<
std
::
list
<
int
>>
()),
json
::
type_error
&
);
...
...
@@ -1094,6 +1114,7 @@ TEST_CASE("value conversion")
CHECK_THROWS_WITH
((
json
().
get
<
std
::
vector
<
json
>>
()),
"[json.exception.type_error.302] type must be array, but is null"
);
CHECK_THROWS_WITH
((
json
().
get
<
std
::
list
<
json
>>
()),
"[json.exception.type_error.302] type must be array, but is null"
);
CHECK_THROWS_WITH
((
json
().
get
<
std
::
valarray
<
int
>>
()),
"[json.exception.type_error.302] type must be array, but is null"
);
CHECK_THROWS_WITH
((
json
().
get
<
std
::
map
<
int
,
int
>>
()),
"[json.exception.type_error.302] type must be array, but is null"
);
}
}
}
...
...
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