Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
json
Commits
6d427acd
Commit
6d427acd
authored
Jan 08, 2017
by
Théo DELRIEU
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
replace constructor by from/to_json: unscoped enum types
this also means that one can do: j.get<unscoped_enum>();
parent
f0089833
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
21 deletions
+41
-21
json.hpp
src/json.hpp
+16
-10
json.hpp.re2c
src/json.hpp.re2c
+16
-10
unit-regression.cpp
test/src/unit-regression.cpp
+9
-1
No files found.
src/json.hpp
View file @
6d427acd
...
...
@@ -432,7 +432,6 @@ template <typename T, typename BasicJson>
struct
is_compatible_basic_json_type
{
static
auto
constexpr
value
=
is_unscoped_enum
<
T
>::
value
or
std
::
is_same
<
T
,
BasicJson
>::
value
or
is_compatible_array_type
<
BasicJson
,
T
>::
value
or
is_compatible_object_type
<
typename
BasicJson
::
object_t
,
T
>::
value
;
...
...
@@ -569,6 +568,13 @@ void to_json(Json &j, CompatibleNumberIntegerType val) noexcept
external_constructor
<
value_t
::
number_integer
>::
construct
(
j
,
val
);
}
template
<
typename
Json
,
typename
UnscopedEnumType
,
enable_if_t
<
is_unscoped_enum
<
UnscopedEnumType
>::
value
,
int
>
=
0
>
void
to_json
(
Json
&
j
,
UnscopedEnumType
e
)
{
external_constructor
<
value_t
::
number_integer
>::
construct
(
j
,
e
);
}
template
<
typename
Json
>
void
from_json
(
Json
const
&
j
,
typename
Json
::
boolean_t
&
b
)
{
...
...
@@ -603,6 +609,15 @@ void from_json(Json const& j, typename Json::number_integer_t& val)
get_arithmetic_value
(
j
,
val
);
}
template
<
typename
Json
,
typename
UnscopedEnumType
,
enable_if_t
<
is_unscoped_enum
<
UnscopedEnumType
>::
value
,
int
>
=
0
>
void
from_json
(
Json
const
&
j
,
UnscopedEnumType
&
e
)
{
typename
std
::
underlying_type
<
UnscopedEnumType
>::
type
val
=
e
;
get_arithmetic_value
(
j
,
val
);
e
=
static_cast
<
UnscopedEnumType
>
(
val
);
}
// overload for arithmetic types, not chosen for basic_json template arguments (BooleanType, etc..)
//
// note: Is it really necessary to provide explicit overloads for boolean_t etc..
...
...
@@ -1848,15 +1863,6 @@ class basic_json
JSONSerializer
<
uncvref_t
<
T
>>::
to_json
(
*
this
,
std
::
forward
<
T
>
(
val
));
}
// Constructor for unscoped enums (not enum classes)
template
<
typename
T
,
enable_if_t
<
is_unscoped_enum
<
T
>::
value
,
int
>
=
0
>
basic_json
(
T
val
)
noexcept
:
m_type
(
value_t
::
number_integer
),
m_value
(
static_cast
<
number_integer_t
>
(
val
))
{
assert_invariant
();
}
/*!
@brief create a container (array or object) from an initializer list
...
...
src/json.hpp.re2c
View file @
6d427acd
...
...
@@ -432,7 +432,6 @@ template <typename T, typename BasicJson>
struct is_compatible_basic_json_type
{
static auto constexpr value =
is_unscoped_enum<T>::value or
std::is_same<T, BasicJson>::value or
is_compatible_array_type<BasicJson, T>::value or
is_compatible_object_type<typename BasicJson::object_t, T>::value;
...
...
@@ -569,6 +568,13 @@ void to_json(Json &j, CompatibleNumberIntegerType val) noexcept
external_constructor<value_t::number_integer>::construct(j, val);
}
template <typename Json, typename UnscopedEnumType,
enable_if_t<is_unscoped_enum<UnscopedEnumType>::value, int> = 0>
void to_json(Json &j, UnscopedEnumType e)
{
external_constructor<value_t::number_integer>::construct(j, e);
}
template <typename Json>
void from_json(Json const& j, typename Json::boolean_t& b)
{
...
...
@@ -603,6 +609,15 @@ void from_json(Json const& j, typename Json::number_integer_t& val)
get_arithmetic_value(j, val);
}
template <typename Json, typename UnscopedEnumType,
enable_if_t<is_unscoped_enum<UnscopedEnumType>::value, int> = 0>
void from_json(Json const &j, UnscopedEnumType& e)
{
typename std::underlying_type<UnscopedEnumType>::type val = e;
get_arithmetic_value(j, val);
e = static_cast<UnscopedEnumType>(val);
}
// overload for arithmetic types, not chosen for basic_json template arguments (BooleanType, etc..)
//
// note: Is it really necessary to provide explicit overloads for boolean_t etc..
...
...
@@ -1849,15 +1864,6 @@ class basic_json
JSONSerializer<uncvref_t<T>>::to_json(*this, std::forward<T>(val));
}
// Constructor for unscoped enums (not enum classes)
template <typename T, enable_if_t<is_unscoped_enum<T>::value, int> = 0>
basic_json(T val) noexcept
: m_type(value_t::number_integer),
m_value(static_cast<number_integer_t>(val))
{
assert_invariant();
}
/*!
@brief create a container (array or object) from an initializer list
...
...
test/src/unit-regression.cpp
View file @
6d427acd
...
...
@@ -63,10 +63,18 @@ TEST_CASE("regression tests")
SECTION
(
"pull request #71 - handle enum type"
)
{
enum
{
t
=
0
};
enum
{
t
=
0
,
u
=
1
};
json
j
=
json
::
array
();
j
.
push_back
(
t
);
// maybe this is not the place to test this?
json
j2
=
u
;
auto
anon_enum_value
=
j2
.
get
<
decltype
(
u
)
>
();
CHECK
(
u
==
anon_enum_value
);
static_assert
(
std
::
is_same
<
decltype
(
anon_enum_value
),
decltype
(
u
)
>::
value
,
""
);
j
.
push_back
(
json
::
object
(
{
{
"game_type"
,
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