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
bbe40641
Commit
bbe40641
authored
Jan 07, 2017
by
Théo DELRIEU
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
replace constructor by from/to_json: string_t
parent
6b89785f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
100 additions
and
184 deletions
+100
-184
json.hpp
src/json.hpp
+50
-92
json.hpp.re2c
src/json.hpp.re2c
+50
-92
No files found.
src/json.hpp
View file @
bbe40641
...
...
@@ -217,6 +217,18 @@ struct external_constructor<value_t::boolean>
j
.
assert_invariant
();
}
};
template
<>
struct
external_constructor
<
value_t
::
string
>
{
template
<
typename
Json
>
static
void
construct
(
Json
&
j
,
const
typename
Json
::
string_t
&
s
)
{
j
.
m_type
=
value_t
::
string
;
j
.
m_value
=
s
;
j
.
assert_invariant
();
}
};
// very useful construct against boilerplate (more boilerplate needed than in
// C++17: http://en.cppreference.com/w/cpp/types/void_t)
template
<
typename
...
>
struct
make_void
...
...
@@ -328,12 +340,14 @@ struct is_compatible_array_type
{
// the check for CompatibleArrayType = void is done in
// `is_compatible_object_type`, but we need the conjunction here as well
static
auto
constexpr
value
=
is_compatible_array_type_impl
<
conjunction
<
negation
<
is_compatible_object_type
<
typename
BasicJson
::
object_t
,
CompatibleArrayType
>>
,
has_value_type
<
CompatibleArrayType
>
,
has_iterator
<
CompatibleArrayType
>>::
value
,
BasicJson
,
CompatibleArrayType
>::
value
;
static
auto
constexpr
value
=
is_compatible_array_type_impl
<
conjunction
<
negation
<
is_compatible_object_type
<
typename
BasicJson
::
object_t
,
CompatibleArrayType
>>
,
negation
<
std
::
is_constructible
<
typename
BasicJson
::
string_t
,
CompatibleArrayType
>>
,
has_value_type
<
CompatibleArrayType
>
,
has_iterator
<
CompatibleArrayType
>>::
value
,
BasicJson
,
CompatibleArrayType
>::
value
;
};
template
<
bool
,
typename
,
typename
>
...
...
@@ -376,7 +390,6 @@ struct is_compatible_basic_json_type
static
auto
constexpr
value
=
is_unscoped_enum
<
T
>::
value
or
std
::
is_same
<
T
,
BasicJson
>::
value
or
std
::
is_constructible
<
typename
BasicJson
::
string_t
,
T
>::
value
or
is_compatible_array_type
<
BasicJson
,
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
...
...
@@ -457,6 +470,15 @@ void to_json(Json &j, typename Json::boolean_t b) noexcept
external_constructor
<
value_t
::
boolean
>::
construct
(
j
,
b
);
}
template
<
typename
Json
,
typename
CompatibleString
,
enable_if_t
<
std
::
is_constructible
<
typename
Json
::
string_t
,
CompatibleString
>::
value
,
int
>
=
0
>
void
to_json
(
Json
&
j
,
const
CompatibleString
&
s
)
{
external_constructor
<
value_t
::
string
>::
construct
(
j
,
s
);
}
template
<
typename
Json
>
void
from_json
(
Json
const
&
j
,
typename
Json
::
boolean_t
&
b
)
{
...
...
@@ -465,6 +487,14 @@ void from_json(Json const& j, typename Json::boolean_t& b)
b
=
*
const_cast
<
Json
&>
(
j
).
template
get_ptr
<
typename
Json
::
boolean_t
*>
();
}
template
<
typename
Json
>
void
from_json
(
Json
const
&
j
,
typename
Json
::
string_t
&
s
)
{
if
(
!
j
.
is_string
())
throw
std
::
domain_error
(
"type must be string, but is "
+
type_name
(
j
));
s
=
*
const_cast
<
Json
&>
(
j
).
template
get_ptr
<
typename
Json
::
string_t
*>
();
}
struct
to_json_fn
{
// is it really useful to mark those as constexpr?
...
...
@@ -1681,90 +1711,6 @@ class basic_json
}
/*!
@brief create a string (explicit)
Create an string JSON value with a given content.
@param[in] val a value for the string
@complexity Linear in the size of the passed @a val.
@throw std::bad_alloc if allocation for string value fails
@liveexample{The following code shows the constructor with an @ref
string_t parameter.,basic_json__string_t}
@sa @ref basic_json(const typename string_t::value_type*) -- create a
string value from a character pointer
@sa @ref basic_json(const CompatibleStringType&) -- create a string value
from a compatible string container
@since version 1.0.0
*/
basic_json
(
const
string_t
&
val
)
:
m_type
(
value_t
::
string
),
m_value
(
val
)
{
assert_invariant
();
}
/*!
@brief create a string (explicit)
Create a string JSON value with a given content.
@param[in] val a literal value for the string
@complexity Linear in the size of the passed @a val.
@throw std::bad_alloc if allocation for string value fails
@liveexample{The following code shows the constructor with string literal
parameter.,basic_json__string_t_value_type}
@sa @ref basic_json(const string_t&) -- create a string value
@sa @ref basic_json(const CompatibleStringType&) -- create a string value
from a compatible string container
@since version 1.0.0
*/
basic_json
(
const
typename
string_t
::
value_type
*
val
)
:
basic_json
(
string_t
(
val
))
{
assert_invariant
();
}
/*!
@brief create a string (implicit)
Create a string JSON value with a given content.
@param[in] val a value for the string
@tparam CompatibleStringType an string type which is compatible to @ref
string_t, for instance `std::string`.
@complexity Linear in the size of the passed @a val.
@throw std::bad_alloc if allocation for string value fails
@liveexample{The following code shows the construction of a string value
from a compatible type.,basic_json__CompatibleStringType}
@sa @ref basic_json(const string_t&) -- create a string value
@sa @ref basic_json(const typename string_t::value_type*) -- create a
string value from a character pointer
@since version 1.0.0
*/
template
<
class
CompatibleStringType
,
typename
std
::
enable_if
<
std
::
is_constructible
<
string_t
,
CompatibleStringType
>::
value
,
int
>::
type
=
0
>
basic_json
(
const
CompatibleStringType
&
val
)
:
basic_json
(
string_t
(
val
))
{
assert_invariant
();
}
/*!
@brief create an integer number (explicit)
Create an integer number JSON value with a given content.
...
...
@@ -11505,9 +11451,21 @@ basic_json_parser_66:
}
private
:
friend
bool
operator
==
(
json_pointer
const
&
lhs
,
json_pointer
const
&
rhs
)
noexcept
{
return
lhs
.
reference_tokens
==
rhs
.
reference_tokens
;
}
friend
bool
operator
!=
(
json_pointer
const
&
lhs
,
json_pointer
const
&
rhs
)
noexcept
{
return
!
(
lhs
==
rhs
);
}
/// the reference tokens
std
::
vector
<
std
::
string
>
reference_tokens
{};
};
};
//////////////////////////
// JSON Pointer support //
...
...
src/json.hpp.re2c
View file @
bbe40641
...
...
@@ -217,6 +217,18 @@ struct external_constructor<value_t::boolean>
j.assert_invariant();
}
};
template <>
struct external_constructor<value_t::string>
{
template <typename Json>
static void construct(Json &j, const typename Json::string_t& s)
{
j.m_type = value_t::string;
j.m_value = s;
j.assert_invariant();
}
};
// very useful construct against boilerplate (more boilerplate needed than in
// C++17: http://en.cppreference.com/w/cpp/types/void_t)
template <typename...> struct make_void
...
...
@@ -328,12 +340,14 @@ struct is_compatible_array_type
{
// the check for CompatibleArrayType = void is done in
// `is_compatible_object_type`, but we need the conjunction here as well
static auto constexpr value = is_compatible_array_type_impl <
conjunction<negation<is_compatible_object_type<
typename BasicJson::object_t, CompatibleArrayType>>,
has_value_type<CompatibleArrayType>,
has_iterator<CompatibleArrayType>>::value,
BasicJson, CompatibleArrayType >::value;
static auto constexpr value = is_compatible_array_type_impl<
conjunction<negation<is_compatible_object_type<
typename BasicJson::object_t, CompatibleArrayType>>,
negation<std::is_constructible<typename BasicJson::string_t,
CompatibleArrayType>>,
has_value_type<CompatibleArrayType>,
has_iterator<CompatibleArrayType>>::value,
BasicJson, CompatibleArrayType>::value;
};
template <bool, typename, typename>
...
...
@@ -376,7 +390,6 @@ struct is_compatible_basic_json_type
static auto constexpr value =
is_unscoped_enum<T>::value or
std::is_same<T, BasicJson>::value or
std::is_constructible<typename BasicJson::string_t, T>::value or
is_compatible_array_type<BasicJson, 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
...
...
@@ -457,6 +470,15 @@ void to_json(Json &j, typename Json::boolean_t b) noexcept
external_constructor<value_t::boolean>::construct(j, b);
}
template <typename Json, typename CompatibleString,
enable_if_t<std::is_constructible<typename Json::string_t,
CompatibleString>::value,
int> = 0>
void to_json(Json &j, const CompatibleString &s)
{
external_constructor<value_t::string>::construct(j, s);
}
template <typename Json>
void from_json(Json const& j, typename Json::boolean_t& b)
{
...
...
@@ -465,6 +487,14 @@ void from_json(Json const& j, typename Json::boolean_t& b)
b = *const_cast<Json&>(j).template get_ptr<typename Json::boolean_t*>();
}
template <typename Json>
void from_json(Json const& j, typename Json::string_t& s)
{
if (!j.is_string())
throw std::domain_error("type must be string, but is " + type_name(j));
s = *const_cast<Json&>(j).template get_ptr<typename Json::string_t*>();
}
struct to_json_fn
{
// is it really useful to mark those as constexpr?
...
...
@@ -1682,90 +1712,6 @@ class basic_json
}
/*!
@brief create a string (explicit)
Create an string JSON value with a given content.
@param[in] val a value for the string
@complexity Linear in the size of the passed @a val.
@throw std::bad_alloc if allocation for string value fails
@liveexample{The following code shows the constructor with an @ref
string_t parameter.,basic_json__string_t}
@sa @ref basic_json(const typename string_t::value_type*) -- create a
string value from a character pointer
@sa @ref basic_json(const CompatibleStringType&) -- create a string value
from a compatible string container
@since version 1.0.0
*/
basic_json(const string_t& val)
: m_type(value_t::string), m_value(val)
{
assert_invariant();
}
/*!
@brief create a string (explicit)
Create a string JSON value with a given content.
@param[in] val a literal value for the string
@complexity Linear in the size of the passed @a val.
@throw std::bad_alloc if allocation for string value fails
@liveexample{The following code shows the constructor with string literal
parameter.,basic_json__string_t_value_type}
@sa @ref basic_json(const string_t&) -- create a string value
@sa @ref basic_json(const CompatibleStringType&) -- create a string value
from a compatible string container
@since version 1.0.0
*/
basic_json(const typename string_t::value_type* val)
: basic_json(string_t(val))
{
assert_invariant();
}
/*!
@brief create a string (implicit)
Create a string JSON value with a given content.
@param[in] val a value for the string
@tparam CompatibleStringType an string type which is compatible to @ref
string_t, for instance `std::string`.
@complexity Linear in the size of the passed @a val.
@throw std::bad_alloc if allocation for string value fails
@liveexample{The following code shows the construction of a string value
from a compatible type.,basic_json__CompatibleStringType}
@sa @ref basic_json(const string_t&) -- create a string value
@sa @ref basic_json(const typename string_t::value_type*) -- create a
string value from a character pointer
@since version 1.0.0
*/
template<class CompatibleStringType, typename std::enable_if<
std::is_constructible<string_t, CompatibleStringType>::value, int>::type = 0>
basic_json(const CompatibleStringType& val)
: basic_json(string_t(val))
{
assert_invariant();
}
/*!
@brief create an integer number (explicit)
Create an integer number JSON value with a given content.
...
...
@@ -11131,9 +11077,21 @@ class basic_json
}
private:
friend bool operator==(json_pointer const &lhs,
json_pointer const &rhs) noexcept
{
return lhs.reference_tokens == rhs.reference_tokens;
}
friend bool operator!=(json_pointer const &lhs,
json_pointer const &rhs) noexcept
{
return !(lhs == rhs);
}
/// the reference tokens
std::vector<std::string> reference_tokens {};
};
};
//////////////////////////
// JSON Pointer support //
...
...
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