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
4d3053cd
Commit
4d3053cd
authored
Jan 25, 2017
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
💄
some cleanup
parent
94d9b7bc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
38 deletions
+50
-38
src/json.hpp
src/json.hpp
+25
-19
src/json.hpp.re2c
src/json.hpp.re2c
+25
-19
No files found.
src/json.hpp
View file @
4d3053cd
...
...
@@ -204,14 +204,14 @@ using enable_if_t = typename std::enable_if<B, T>::type;
template
<
typename
T
>
using
uncvref_t
=
typename
std
::
remove_cv
<
typename
std
::
remove_reference
<
T
>::
type
>::
type
;
//
Taken from http://stackoverflow.com/questions/26936640/how-to-implement-is-enum-class-type-trait
//
taken from http://stackoverflow.com/a/26936864/266378
template
<
typename
T
>
using
is_unscoped_enum
=
std
::
integral_constant
<
bool
,
std
::
is_convertible
<
T
,
int
>::
value
and
std
::
is_enum
<
T
>::
value
>
;
/*
Implementation of
2
C++17 constructs: conjunction, negation. This is needed
Implementation of
two
C++17 constructs: conjunction, negation. This is needed
to avoid evaluating all the traits in a condition
For example: not std::is_same<void, T>::value and has_value_type<T>::value
...
...
@@ -393,6 +393,7 @@ NLOHMANN_JSON_HAS_HELPER(iterator);
#undef NLOHMANN_JSON_HAS_HELPER
template
<
bool
B
,
class
RealType
,
class
CompatibleObjectType
>
struct
is_compatible_object_type_impl
:
std
::
false_type
{};
...
...
@@ -468,7 +469,7 @@ struct is_compatible_integer_type
};
//
This trait checks
if JSONSerializer<T>::from_json(json const&, udt&) exists
//
trait checking
if JSONSerializer<T>::from_json(json const&, udt&) exists
template
<
typename
BasicJsonType
,
typename
T
>
struct
has_from_json
{
...
...
@@ -530,8 +531,7 @@ void to_json(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
template
<
typename
BasicJsonType
,
typename
CompatibleString
,
enable_if_t
<
std
::
is_constructible
<
typename
BasicJsonType
::
string_t
,
CompatibleString
>
::
value
,
int
>
=
0
>
CompatibleString
>
::
value
,
int
>
=
0
>
void
to_json
(
BasicJsonType
&
j
,
const
CompatibleString
&
s
)
{
external_constructor
<
value_t
::
string
>::
construct
(
j
,
s
);
...
...
@@ -547,8 +547,7 @@ void to_json(BasicJsonType& j, FloatType val) noexcept
template
<
typename
BasicJsonType
,
typename
CompatibleNumberUnsignedType
,
enable_if_t
<
is_compatible_integer_type
<
typename
BasicJsonType
::
number_unsigned_t
,
CompatibleNumberUnsignedType
>
::
value
,
int
>
=
0
>
CompatibleNumberUnsignedType
>
::
value
,
int
>
=
0
>
void
to_json
(
BasicJsonType
&
j
,
CompatibleNumberUnsignedType
val
)
noexcept
{
external_constructor
<
value_t
::
number_unsigned
>::
construct
(
j
,
static_cast
<
typename
BasicJsonType
::
number_unsigned_t
>
(
val
));
...
...
@@ -557,8 +556,7 @@ void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
template
<
typename
BasicJsonType
,
typename
CompatibleNumberIntegerType
,
enable_if_t
<
is_compatible_integer_type
<
typename
BasicJsonType
::
number_integer_t
,
CompatibleNumberIntegerType
>
::
value
,
int
>
=
0
>
CompatibleNumberIntegerType
>
::
value
,
int
>
=
0
>
void
to_json
(
BasicJsonType
&
j
,
CompatibleNumberIntegerType
val
)
noexcept
{
external_constructor
<
value_t
::
number_integer
>::
construct
(
j
,
static_cast
<
typename
BasicJsonType
::
number_integer_t
>
(
val
));
...
...
@@ -690,7 +688,6 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
}
// forward_list doesn't have an insert method
// TODO find a way to avoid including forward_list
template
<
typename
BasicJsonType
,
typename
T
,
typename
Allocator
>
void
from_json
(
const
BasicJsonType
&
j
,
std
::
forward_list
<
T
,
Allocator
>&
l
)
{
...
...
@@ -781,14 +778,15 @@ void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
using
std
::
begin
;
using
std
::
end
;
// we could avoid the assignment, but this might require a for loop, which
// might be less efficient than the container constructor for some containers (would it?)
// might be less efficient than the container constructor for some
// containers (would it?)
obj
=
CompatibleObjectType
(
begin
(
*
inner_object
),
end
(
*
inner_object
));
}
// 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..
//
in case of a custom BooleanType which is not
an arithmetic type?
// 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. in case of a custom BooleanType which is not
// an arithmetic type?
template
<
typename
BasicJsonType
,
typename
ArithmeticType
,
enable_if_t
<
std
::
is_arithmetic
<
ArithmeticType
>
::
value
and
...
...
@@ -802,19 +800,29 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
switch
(
static_cast
<
value_t
>
(
j
))
{
case
value_t
:
:
number_unsigned
:
{
val
=
static_cast
<
ArithmeticType
>
(
*
j
.
template
get_ptr
<
const
typename
BasicJsonType
::
number_unsigned_t
*
>());
break
;
}
case
value_t
:
:
number_integer
:
{
val
=
static_cast
<
ArithmeticType
>
(
*
j
.
template
get_ptr
<
const
typename
BasicJsonType
::
number_integer_t
*
>());
break
;
}
case
value_t
:
:
number_float
:
{
val
=
static_cast
<
ArithmeticType
>
(
*
j
.
template
get_ptr
<
const
typename
BasicJsonType
::
number_float_t
*
>());
break
;
}
case
value_t
:
:
boolean
:
{
val
=
static_cast
<
ArithmeticType
>
(
*
j
.
template
get_ptr
<
const
typename
BasicJsonType
::
boolean_t
*
>());
break
;
}
default:
{
JSON_THROW
(
std
::
domain_error
(
"type must be number, but is "
+
j
.
type_name
()));
}
}
}
...
...
@@ -3160,8 +3168,7 @@ class basic_json
detail
::
enable_if_t
<
not
std
::
is_same
<
basic_json_t
,
U
>::
value
and
detail
::
has_from_json
<
basic_json_t
,
U
>::
value
and
not
detail
::
has_non_default_from_json
<
basic_json_t
,
U
>::
value
,
not
detail
::
has_non_default_from_json
<
basic_json_t
,
U
>::
value
,
int
>
=
0
>
U
get
()
const
noexcept
(
noexcept
(
JSONSerializer
<
U
>::
from_json
(
std
::
declval
<
const
basic_json_t
&>
(),
std
::
declval
<
U
&>
())))
...
...
@@ -3196,8 +3203,7 @@ class basic_json
typename
U
=
detail
::
uncvref_t
<
T
>,
detail
::
enable_if_t
<
not
std
::
is_same
<
basic_json_t
,
U
>::
value
and
detail
::
has_non_default_from_json
<
basic_json_t
,
U
>::
value
,
int
>
=
0
>
U
>::
value
,
int
>
=
0
>
U
get
()
const
noexcept
(
noexcept
(
JSONSerializer
<
T
>::
from_json
(
std
::
declval
<
const
basic_json_t
&>
())))
{
static_assert
(
not
std
::
is_reference
<
T
>::
value
,
"get cannot be used with reference types, you might want to use get_ref"
);
...
...
src/json.hpp.re2c
View file @
4d3053cd
...
...
@@ -204,14 +204,14 @@ using enable_if_t = typename std::enable_if<B, T>::type;
template<typename T>
using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
//
Taken from http://stackoverflow.com/questions/26936640/how-to-implement-is-enum-class-type-trait
//
taken from http://stackoverflow.com/a/26936864/266378
template<typename T>
using is_unscoped_enum =
std::integral_constant<bool, std::is_convertible<T, int>::value and
std::is_enum<T>::value>;
/*
Implementation of
2
C++17 constructs: conjunction, negation. This is needed
Implementation of
two
C++17 constructs: conjunction, negation. This is needed
to avoid evaluating all the traits in a condition
For example: not std::is_same<void, T>::value and has_value_type<T>::value
...
...
@@ -393,6 +393,7 @@ NLOHMANN_JSON_HAS_HELPER(iterator);
#undef NLOHMANN_JSON_HAS_HELPER
template<bool B, class RealType, class CompatibleObjectType>
struct is_compatible_object_type_impl : std::false_type {};
...
...
@@ -468,7 +469,7 @@ struct is_compatible_integer_type
};
//
This trait checks
if JSONSerializer<T>::from_json(json const&, udt&) exists
//
trait checking
if JSONSerializer<T>::from_json(json const&, udt&) exists
template<typename BasicJsonType, typename T>
struct has_from_json
{
...
...
@@ -530,8 +531,7 @@ void to_json(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
template<typename BasicJsonType, typename CompatibleString,
enable_if_t<std::is_constructible<typename BasicJsonType::string_t,
CompatibleString>::value,
int> = 0>
CompatibleString>::value, int> = 0>
void to_json(BasicJsonType& j, const CompatibleString& s)
{
external_constructor<value_t::string>::construct(j, s);
...
...
@@ -547,8 +547,7 @@ void to_json(BasicJsonType& j, FloatType val) noexcept
template <
typename BasicJsonType, typename CompatibleNumberUnsignedType,
enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t,
CompatibleNumberUnsignedType>::value,
int> = 0 >
CompatibleNumberUnsignedType>::value, int> = 0 >
void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
{
external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
...
...
@@ -557,8 +556,7 @@ void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
template <
typename BasicJsonType, typename CompatibleNumberIntegerType,
enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t,
CompatibleNumberIntegerType>::value,
int> = 0 >
CompatibleNumberIntegerType>::value, int> = 0 >
void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
{
external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
...
...
@@ -690,7 +688,6 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
}
// forward_list doesn't have an insert method
// TODO find a way to avoid including forward_list
template<typename BasicJsonType, typename T, typename Allocator>
void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
{
...
...
@@ -781,14 +778,15 @@ void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
using std::begin;
using std::end;
// we could avoid the assignment, but this might require a for loop, which
// might be less efficient than the container constructor for some containers (would it?)
// might be less efficient than the container constructor for some
// containers (would it?)
obj = CompatibleObjectType(begin(*inner_object), end(*inner_object));
}
// 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..
//
in case of a custom BooleanType which is not
an arithmetic type?
// 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. in case of a custom BooleanType which is not
// an arithmetic type?
template<typename BasicJsonType, typename ArithmeticType,
enable_if_t <
std::is_arithmetic<ArithmeticType>::value and
...
...
@@ -802,19 +800,29 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
switch (static_cast<value_t>(j))
{
case value_t::number_unsigned:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
break;
}
case value_t::number_integer:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
break;
}
case value_t::number_float:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
break;
}
case value_t::boolean:
{
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
break;
}
default:
{
JSON_THROW(std::domain_error("type must be number, but is " + j.type_name()));
}
}
}
...
...
@@ -3160,8 +3168,7 @@ class basic_json
detail::enable_if_t <
not std::is_same<basic_json_t, U>::value and
detail::has_from_json<basic_json_t, U>::value and
not detail::has_non_default_from_json<basic_json_t,
U>::value,
not detail::has_non_default_from_json<basic_json_t, U>::value,
int > = 0 >
U get() const noexcept(noexcept(JSONSerializer<U>::from_json(
std::declval<const basic_json_t&>(), std::declval<U&>())))
...
...
@@ -3196,8 +3203,7 @@ class basic_json
typename U = detail::uncvref_t<T>,
detail::enable_if_t<not std::is_same<basic_json_t, U>::value and
detail::has_non_default_from_json<basic_json_t,
U>::value,
int> = 0 >
U>::value, int> = 0 >
U get() const noexcept(noexcept(JSONSerializer<T>::from_json(std::declval<const basic_json_t&>())))
{
static_assert(not std::is_reference<T>::value, "get cannot be used with reference types, you might want to use get_ref");
...
...
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