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
7bcaba0c
Unverified
Commit
7bcaba0c
authored
Nov 05, 2019
by
Niels Lohmann
Committed by
GitHub
Nov 05, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1821 from AnthonyVH/develop
Fix for #1647
parents
1ca6f290
c4923e3d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
93 additions
and
53 deletions
+93
-53
include/nlohmann/detail/macro_scope.hpp
include/nlohmann/detail/macro_scope.hpp
+24
-24
include/nlohmann/json.hpp
include/nlohmann/json.hpp
+3
-3
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+26
-26
test/src/unit-regression.cpp
test/src/unit-regression.cpp
+40
-0
No files found.
include/nlohmann/detail/macro_scope.hpp
View file @
7bcaba0c
...
...
@@ -78,30 +78,30 @@
@def NLOHMANN_JSON_SERIALIZE_ENUM
@since version 3.4.0
*/
#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
template<typename BasicJsonType> \
inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
{ \
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
auto it = std::find_if(std::begin(m), std::end(m), \
[e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
{ \
return ej_pair.first == e; \
}); \
j = ((it != std::end(m)) ? it : std::begin(m))->second; \
} \
template<typename BasicJsonType> \
inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
{ \
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
auto it = std::find_if(std::begin(m), std::end(m), \
[j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
{ \
return ej_pair.second == j; \
}); \
e = ((it != std::end(m)) ? it : std::begin(m))->first; \
#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...)
\
template<typename BasicJsonType>
\
inline void to_json(BasicJsonType& j, const ENUM_TYPE& e)
\
{
\
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!");
\
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__;
\
auto it = std::find_if(std::begin(m), std::end(m),
\
[e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool
\
{
\
return ej_pair.first == e;
\
});
\
j = ((it != std::end(m)) ? it : std::begin(m))->second;
\
}
\
template<typename BasicJsonType>
\
inline void from_json(const BasicJsonType& j, ENUM_TYPE& e)
\
{
\
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!");
\
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__;
\
auto it = std::find_if(std::begin(m), std::end(m),
\
[
&
j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
{
\
return ej_pair.second == j;
\
});
\
e = ((it != std::end(m)) ? it : std::begin(m))->first;
\
}
// Ugly macros to avoid uglier copy-paste when specializing basic_json. They
...
...
include/nlohmann/json.hpp
View file @
7bcaba0c
...
...
@@ -2633,11 +2633,11 @@ class basic_json
detail
::
has_non_default_from_json
<
basic_json_t
,
ValueType
>::
value
,
int
>
=
0
>
ValueType
get
()
const
noexcept
(
noexcept
(
JSONSerializer
<
ValueType
CV
>::
from_json
(
std
::
declval
<
const
basic_json_t
&>
())))
JSONSerializer
<
ValueType
>::
from_json
(
std
::
declval
<
const
basic_json_t
&>
())))
{
static_assert
(
not
std
::
is_reference
<
ValueTypeCV
>::
value
,
"get() cannot be used with reference types, you might want to use get_ref()"
);
return
JSONSerializer
<
ValueType
CV
>::
from_json
(
*
this
);
return
JSONSerializer
<
ValueType
>::
from_json
(
*
this
);
}
/*!
...
...
@@ -8062,7 +8062,7 @@ struct hash<nlohmann::json>
/// @note: do not remove the space after '<',
/// see https://github.com/nlohmann/json/pull/679
template
<
>
struct
less
<
::
nlohmann
::
detail
::
value_t
>
struct
less
<::
nlohmann
::
detail
::
value_t
>
{
/*!
@brief compare two value_t enum values
...
...
single_include/nlohmann/json.hpp
View file @
7bcaba0c
...
...
@@ -1782,30 +1782,30 @@ JSON_HEDLEY_DIAGNOSTIC_POP
@def NLOHMANN_JSON_SERIALIZE_ENUM
@since version 3.4.0
*/
#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
template<typename BasicJsonType> \
inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
{ \
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
auto it = std::find_if(std::begin(m), std::end(m), \
[e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
{ \
return ej_pair.first == e; \
}); \
j = ((it != std::end(m)) ? it : std::begin(m))->second; \
} \
template<typename BasicJsonType> \
inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
{ \
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
auto it = std::find_if(std::begin(m), std::end(m), \
[j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
{ \
return ej_pair.second == j; \
}); \
e = ((it != std::end(m)) ? it : std::begin(m))->first; \
#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...)
\
template<typename BasicJsonType>
\
inline void to_json(BasicJsonType& j, const ENUM_TYPE& e)
\
{
\
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!");
\
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__;
\
auto it = std::find_if(std::begin(m), std::end(m),
\
[e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool
\
{
\
return ej_pair.first == e;
\
});
\
j = ((it != std::end(m)) ? it : std::begin(m))->second;
\
}
\
template<typename BasicJsonType>
\
inline void from_json(const BasicJsonType& j, ENUM_TYPE& e)
\
{
\
static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!");
\
static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__;
\
auto it = std::find_if(std::begin(m), std::end(m),
\
[
&
j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
{
\
return ej_pair.second == j;
\
});
\
e = ((it != std::end(m)) ? it : std::begin(m))->first;
\
}
// Ugly macros to avoid uglier copy-paste when specializing basic_json. They
...
...
@@ -17176,11 +17176,11 @@ class basic_json
detail::has_non_default_from_json<basic_json_t, ValueType>::value,
int> = 0>
ValueType get() const noexcept(noexcept(
JSONSerializer
<
ValueType
CV
>::
from_json
(
std
::
declval
<
const
basic_json_t
&>
())))
JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
{
static_assert(not std::is_reference<ValueTypeCV>::value,
"get() cannot be used with reference types, you might want to use get_ref()");
return
JSONSerializer
<
ValueType
CV
>::
from_json
(
*
this
);
return JSONSerializer<ValueType>::from_json(*this);
}
/*!
...
...
test/src/unit-regression.cpp
View file @
7bcaba0c
...
...
@@ -159,6 +159,39 @@ bool operator==(Data const& lhs, Data const& rhs)
using
float_json
=
nlohmann
::
basic_json
<
std
::
map
,
std
::
vector
,
std
::
string
,
bool
,
std
::
int64_t
,
std
::
uint64_t
,
float
>
;
/////////////////////////////////////////////////////////////////////
// for #1647
/////////////////////////////////////////////////////////////////////
namespace
{
struct
NonDefaultFromJsonStruct
{
};
inline
bool
operator
==
(
NonDefaultFromJsonStruct
const
&
lhs
,
NonDefaultFromJsonStruct
const
&
rhs
)
{
return
true
;
}
enum
class
for_1647
{
one
,
two
};
NLOHMANN_JSON_SERIALIZE_ENUM
(
for_1647
,
{
{
for_1647
::
one
,
"one"
},
{
for_1647
::
two
,
"two"
},
})
}
namespace
nlohmann
{
template
<
>
struct
adl_serializer
<
NonDefaultFromJsonStruct
>
{
static
NonDefaultFromJsonStruct
from_json
(
json
const
&
j
)
{
return
{};
}
};
}
/////////////////////////////////////////////////////////////////////
// for #1805
/////////////////////////////////////////////////////////////////////
...
...
@@ -1836,6 +1869,13 @@ TEST_CASE("regression tests")
CHECK
(
j
.
contains
(
jptr1
));
CHECK
(
j
.
contains
(
jptr2
));
}
SECTION
(
"issue #1647 - compile error when deserializing enum if both non-default from_json and non-member operator== exists for other type"
)
{
auto
val
=
nlohmann
::
json
(
"one"
).
get
<
for_1647
>
();
CHECK
(
val
==
for_1647
::
one
);
}
SECTION
(
"issue #1805 - A pair<T1, T2> is json constructible only if T1 and T2 are json constructible"
)
{
static_assert
(
!
std
::
is_constructible
<
json
,
std
::
pair
<
std
::
string
,
NotSerializableData
>>::
value
,
""
);
...
...
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