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
6b89785f
Commit
6b89785f
authored
Jan 07, 2017
by
Théo DELRIEU
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
replace constructor by from/to_json: boolean_t
parent
c833b22b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
56 deletions
+86
-56
json.hpp
src/json.hpp
+43
-28
json.hpp.re2c
src/json.hpp.re2c
+43
-28
No files found.
src/json.hpp
View file @
6b89785f
...
@@ -202,6 +202,21 @@ template <typename Json> std::string type_name(Json const &j)
...
@@ -202,6 +202,21 @@ template <typename Json> std::string type_name(Json const &j)
}
}
}
}
// This is an experiment. I need this to move constructors out of basic_json.
// I'm sure there is a better way, but this might need a big basic_json refactoring
template
<
value_t
>
struct
external_constructor
;
template
<>
struct
external_constructor
<
value_t
::
boolean
>
{
template
<
typename
Json
>
static
void
construct
(
Json
&
j
,
typename
Json
::
boolean_t
b
)
noexcept
{
j
.
m_type
=
value_t
::
boolean
;
j
.
m_value
=
b
;
j
.
assert_invariant
();
}
};
// very useful construct against boilerplate (more boilerplate needed than in
// very useful construct against boilerplate (more boilerplate needed than in
// C++17: http://en.cppreference.com/w/cpp/types/void_t)
// C++17: http://en.cppreference.com/w/cpp/types/void_t)
template
<
typename
...
>
struct
make_void
template
<
typename
...
>
struct
make_void
...
@@ -340,9 +355,11 @@ struct is_compatible_integer_type_impl<true, RealIntegerType, CompatibleNumberIn
...
@@ -340,9 +355,11 @@ struct is_compatible_integer_type_impl<true, RealIntegerType, CompatibleNumberIn
template
<
typename
RealIntegerType
,
typename
CompatibleNumberIntegerType
>
template
<
typename
RealIntegerType
,
typename
CompatibleNumberIntegerType
>
struct
is_compatible_integer_type
struct
is_compatible_integer_type
{
{
static
constexpr
auto
value
=
is_compatible_integer_type_impl
<
static
constexpr
auto
std
::
is_arithmetic
<
CompatibleNumberIntegerType
>::
value
,
RealIntegerType
,
value
=
is_compatible_integer_type_impl
<
CompatibleNumberIntegerType
>::
value
;
std
::
is_arithmetic
<
CompatibleNumberIntegerType
>::
value
and
not
std
::
is_same
<
bool
,
CompatibleNumberIntegerType
>::
value
,
RealIntegerType
,
CompatibleNumberIntegerType
>
::
value
;
};
};
template
<
typename
RealFloat
,
typename
CompatibleFloat
>
template
<
typename
RealFloat
,
typename
CompatibleFloat
>
...
@@ -360,7 +377,6 @@ struct is_compatible_basic_json_type
...
@@ -360,7 +377,6 @@ struct is_compatible_basic_json_type
is_unscoped_enum
<
T
>::
value
or
is_unscoped_enum
<
T
>::
value
or
std
::
is_same
<
T
,
BasicJson
>::
value
or
std
::
is_same
<
T
,
BasicJson
>::
value
or
std
::
is_constructible
<
typename
BasicJson
::
string_t
,
T
>::
value
or
std
::
is_constructible
<
typename
BasicJson
::
string_t
,
T
>::
value
or
std
::
is_same
<
typename
BasicJson
::
boolean_t
,
T
>::
value
or
is_compatible_array_type
<
BasicJson
,
T
>::
value
or
is_compatible_array_type
<
BasicJson
,
T
>::
value
or
is_compatible_object_type
<
typename
BasicJson
::
object_t
,
T
>::
value
or
is_compatible_object_type
<
typename
BasicJson
::
object_t
,
T
>::
value
or
is_compatible_float_type
<
typename
BasicJson
::
number_float_t
,
T
>::
value
or
is_compatible_float_type
<
typename
BasicJson
::
number_float_t
,
T
>::
value
or
...
@@ -433,6 +449,22 @@ struct has_to_json
...
@@ -433,6 +449,22 @@ struct has_to_json
void
to_json
();
void
to_json
();
void
from_json
();
void
from_json
();
// overloads for basic_json template parameters
template
<
typename
Json
>
void
to_json
(
Json
&
j
,
typename
Json
::
boolean_t
b
)
noexcept
{
external_constructor
<
value_t
::
boolean
>::
construct
(
j
,
b
);
}
template
<
typename
Json
>
void
from_json
(
Json
const
&
j
,
typename
Json
::
boolean_t
&
b
)
{
if
(
!
j
.
is_boolean
())
throw
std
::
domain_error
(
"type must be boolean, but is "
+
type_name
(
j
));
b
=
*
const_cast
<
Json
&>
(
j
).
template
get_ptr
<
typename
Json
::
boolean_t
*>
();
}
struct
to_json_fn
struct
to_json_fn
{
{
// is it really useful to mark those as constexpr?
// is it really useful to mark those as constexpr?
...
@@ -606,6 +638,7 @@ template <
...
@@ -606,6 +638,7 @@ template <
class
basic_json
class
basic_json
{
{
private
:
private
:
template
<::
nlohmann
::
value_t
>
friend
struct
detail
::
external_constructor
;
template
<
typename
Json
>
friend
std
::
string
detail
::
type_name
(
Json
const
&
);
template
<
typename
Json
>
friend
std
::
string
detail
::
type_name
(
Json
const
&
);
/// workaround type for MSVC
/// workaround type for MSVC
using
basic_json_t
=
basic_json
<
ObjectType
,
ArrayType
,
StringType
,
using
basic_json_t
=
basic_json
<
ObjectType
,
ArrayType
,
StringType
,
...
@@ -1732,26 +1765,6 @@ class basic_json
...
@@ -1732,26 +1765,6 @@ class basic_json
}
}
/*!
/*!
@brief create a boolean (explicit)
Creates a JSON boolean type from a given value.
@param[in] val a boolean value to store
@complexity Constant.
@liveexample{The example below demonstrates boolean
values.,basic_json__boolean_t}
@since version 1.0.0
*/
basic_json
(
boolean_t
val
)
noexcept
:
m_type
(
value_t
::
boolean
),
m_value
(
val
)
{
assert_invariant
();
}
/*!
@brief create an integer number (explicit)
@brief create an integer number (explicit)
Create an integer number JSON value with a given content.
Create an integer number JSON value with a given content.
...
@@ -3330,11 +3343,13 @@ class basic_json
...
@@ -3330,11 +3343,13 @@ class basic_json
@since version 1.0.0
@since version 1.0.0
*/
*/
template
<
typename
ValueType
,
template
<
typename
ValueType
,
enable_if_t
<
not
std
::
is_pointer
<
ValueType
>::
value
,
int
>
=
0
>
enable_if_t
<
not
std
::
is_pointer
<
ValueType
>::
value
and
detail
::
is_compatible_basic_json_type
<
uncvref_t
<
ValueType
>
,
basic_json_t
>::
value
,
int
>
=
0
>
auto
get
()
const
auto
get
()
const
->
decltype
(
this
->
get_impl
(
static_cast
<
ValueType
*>
(
nullptr
)))
->
decltype
(
this
->
get_impl
(
static_cast
<
ValueType
*>
(
nullptr
)))
{
{
return
get_impl
(
static_cast
<
ValueType
*>
(
nullptr
));
return
get_impl
(
static_cast
<
ValueType
*>
(
nullptr
));
}
}
template
<
template
<
...
...
src/json.hpp.re2c
View file @
6b89785f
...
@@ -202,6 +202,21 @@ template <typename Json> std::string type_name(Json const &j)
...
@@ -202,6 +202,21 @@ template <typename Json> std::string type_name(Json const &j)
}
}
}
}
// This is an experiment. I need this to move constructors out of basic_json.
// I'm sure there is a better way, but this might need a big basic_json refactoring
template <value_t> struct external_constructor;
template <>
struct external_constructor<value_t::boolean>
{
template <typename Json>
static void construct(Json &j, typename Json::boolean_t b) noexcept
{
j.m_type = value_t::boolean;
j.m_value = b;
j.assert_invariant();
}
};
// very useful construct against boilerplate (more boilerplate needed than in
// very useful construct against boilerplate (more boilerplate needed than in
// C++17: http://en.cppreference.com/w/cpp/types/void_t)
// C++17: http://en.cppreference.com/w/cpp/types/void_t)
template <typename...> struct make_void
template <typename...> struct make_void
...
@@ -340,9 +355,11 @@ struct is_compatible_integer_type_impl<true, RealIntegerType, CompatibleNumberIn
...
@@ -340,9 +355,11 @@ struct is_compatible_integer_type_impl<true, RealIntegerType, CompatibleNumberIn
template <typename RealIntegerType, typename CompatibleNumberIntegerType>
template <typename RealIntegerType, typename CompatibleNumberIntegerType>
struct is_compatible_integer_type
struct is_compatible_integer_type
{
{
static constexpr auto value = is_compatible_integer_type_impl <
static constexpr auto
std::is_arithmetic<CompatibleNumberIntegerType>::value, RealIntegerType,
value = is_compatible_integer_type_impl <
CompatibleNumberIntegerType >::value;
std::is_arithmetic<CompatibleNumberIntegerType>::value and
not std::is_same<bool, CompatibleNumberIntegerType>::value,
RealIntegerType, CompatibleNumberIntegerType > ::value;
};
};
template <typename RealFloat, typename CompatibleFloat>
template <typename RealFloat, typename CompatibleFloat>
...
@@ -360,7 +377,6 @@ struct is_compatible_basic_json_type
...
@@ -360,7 +377,6 @@ struct is_compatible_basic_json_type
is_unscoped_enum<T>::value or
is_unscoped_enum<T>::value or
std::is_same<T, BasicJson>::value or
std::is_same<T, BasicJson>::value or
std::is_constructible<typename BasicJson::string_t, T>::value or
std::is_constructible<typename BasicJson::string_t, T>::value or
std::is_same<typename BasicJson::boolean_t, T>::value or
is_compatible_array_type<BasicJson, T>::value or
is_compatible_array_type<BasicJson, T>::value or
is_compatible_object_type<typename BasicJson::object_t, T>::value or
is_compatible_object_type<typename BasicJson::object_t, T>::value or
is_compatible_float_type<typename BasicJson::number_float_t, T>::value or
is_compatible_float_type<typename BasicJson::number_float_t, T>::value or
...
@@ -433,6 +449,22 @@ struct has_to_json
...
@@ -433,6 +449,22 @@ struct has_to_json
void to_json();
void to_json();
void from_json();
void from_json();
// overloads for basic_json template parameters
template <typename Json>
void to_json(Json &j, typename Json::boolean_t b) noexcept
{
external_constructor<value_t::boolean>::construct(j, b);
}
template <typename Json>
void from_json(Json const& j, typename Json::boolean_t& b)
{
if (!j.is_boolean())
throw std::domain_error("type must be boolean, but is " + type_name(j));
b = *const_cast<Json&>(j).template get_ptr<typename Json::boolean_t*>();
}
struct to_json_fn
struct to_json_fn
{
{
// is it really useful to mark those as constexpr?
// is it really useful to mark those as constexpr?
...
@@ -607,6 +639,7 @@ template <
...
@@ -607,6 +639,7 @@ template <
class basic_json
class basic_json
{
{
private:
private:
template <::nlohmann::value_t> friend struct detail::external_constructor;
template <typename Json> friend std::string detail::type_name(Json const &);
template <typename Json> friend std::string detail::type_name(Json const &);
/// workaround type for MSVC
/// workaround type for MSVC
using basic_json_t = basic_json<ObjectType, ArrayType, StringType,
using basic_json_t = basic_json<ObjectType, ArrayType, StringType,
...
@@ -1733,26 +1766,6 @@ class basic_json
...
@@ -1733,26 +1766,6 @@ class basic_json
}
}
/*!
/*!
@brief create a boolean (explicit)
Creates a JSON boolean type from a given value.
@param[in] val a boolean value to store
@complexity Constant.
@liveexample{The example below demonstrates boolean
values.,basic_json__boolean_t}
@since version 1.0.0
*/
basic_json(boolean_t val) noexcept
: m_type(value_t::boolean), m_value(val)
{
assert_invariant();
}
/*!
@brief create an integer number (explicit)
@brief create an integer number (explicit)
Create an integer number JSON value with a given content.
Create an integer number JSON value with a given content.
...
@@ -3328,11 +3341,13 @@ class basic_json
...
@@ -3328,11 +3341,13 @@ class basic_json
@since version 1.0.0
@since version 1.0.0
*/
*/
template <typename ValueType,
template <typename ValueType,
enable_if_t<not std::is_pointer<ValueType>::value, int> = 0>
enable_if_t<not std::is_pointer<ValueType>::value and
detail::is_compatible_basic_json_type<
uncvref_t<ValueType>, basic_json_t>::value,
int> = 0>
auto get() const
auto get() const
-> decltype(this->get_impl(static_cast<ValueType*>(nullptr)))
-> decltype(this->get_impl(static_cast<ValueType *>(nullptr))) {
{
return get_impl(static_cast<ValueType *>(nullptr));
return get_impl(static_cast<ValueType*>(nullptr));
}
}
template <
template <
...
...
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