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
9355f058
Unverified
Commit
9355f058
authored
Mar 12, 2017
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🔨
cleaned up array from_json methods #473
Removed some code that is not needed any more. Thus, streamlining the array from_json methods.
parent
87eafd8d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
44 deletions
+16
-44
json.hpp
src/json.hpp
+8
-22
json.hpp.re2c
src/json.hpp.re2c
+8
-22
No files found.
src/json.hpp
View file @
9355f058
...
@@ -700,22 +700,15 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
...
@@ -700,22 +700,15 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
}
}
// forward_list doesn't have an insert method
// forward_list doesn't have an insert method
template
<
typename
BasicJsonType
,
typename
T
,
typename
Allocator
>
template
<
typename
BasicJsonType
,
typename
T
,
typename
Allocator
,
enable_if_t
<
std
::
is_convertible
<
BasicJsonType
,
T
>::
value
,
int
>
=
0
>
void
from_json
(
const
BasicJsonType
&
j
,
std
::
forward_list
<
T
,
Allocator
>&
l
)
void
from_json
(
const
BasicJsonType
&
j
,
std
::
forward_list
<
T
,
Allocator
>&
l
)
{
{
// do not perform the check when user wants to retrieve jsons
if
(
not
j
.
is_array
())
// (except when it's null.. ?)
if
(
j
.
is_null
())
{
{
JSON_THROW
(
std
::
domain_error
(
"type must be array, but is "
+
j
.
type_name
()));
JSON_THROW
(
std
::
domain_error
(
"type must be array, but is "
+
j
.
type_name
()));
}
}
if
(
not
std
::
is_same
<
T
,
BasicJsonType
>::
value
)
{
if
(
not
j
.
is_array
())
{
JSON_THROW
(
std
::
domain_error
(
"type must be array, but is "
+
j
.
type_name
()));
}
}
for
(
auto
it
=
j
.
rbegin
(),
end
=
j
.
rend
();
it
!=
end
;
++
it
)
for
(
auto
it
=
j
.
rbegin
(),
end
=
j
.
rend
();
it
!=
end
;
++
it
)
{
{
l
.
push_front
(
it
->
template
get
<
T
>
());
l
.
push_front
(
it
->
template
get
<
T
>
());
...
@@ -747,8 +740,8 @@ auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, prio
...
@@ -747,8 +740,8 @@ auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, prio
using
std
::
end
;
using
std
::
end
;
arr
.
reserve
(
j
.
size
());
arr
.
reserve
(
j
.
size
());
std
::
transform
(
std
::
transform
(
j
.
begin
(),
j
.
end
(),
j
.
begin
(),
j
.
end
(),
std
::
inserter
(
arr
,
end
(
arr
)),
[](
const
BasicJsonType
&
i
)
std
::
inserter
(
arr
,
end
(
arr
)),
[](
const
BasicJsonType
&
i
)
{
{
// get<BasicJsonType>() returns *this, this won't call a from_json
// get<BasicJsonType>() returns *this, this won't call a from_json
// method when value_type is BasicJsonType
// method when value_type is BasicJsonType
...
@@ -758,22 +751,15 @@ auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, prio
...
@@ -758,22 +751,15 @@ auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, prio
template
<
typename
BasicJsonType
,
typename
CompatibleArrayType
,
template
<
typename
BasicJsonType
,
typename
CompatibleArrayType
,
enable_if_t
<
is_compatible_array_type
<
BasicJsonType
,
CompatibleArrayType
>::
value
and
enable_if_t
<
is_compatible_array_type
<
BasicJsonType
,
CompatibleArrayType
>::
value
and
std
::
is_convertible
<
BasicJsonType
,
typename
CompatibleArrayType
::
value_type
>::
value
and
not
std
::
is_same
<
typename
BasicJsonType
::
array_t
,
CompatibleArrayType
>::
value
,
int
>
=
0
>
not
std
::
is_same
<
typename
BasicJsonType
::
array_t
,
CompatibleArrayType
>::
value
,
int
>
=
0
>
void
from_json
(
const
BasicJsonType
&
j
,
CompatibleArrayType
&
arr
)
void
from_json
(
const
BasicJsonType
&
j
,
CompatibleArrayType
&
arr
)
{
{
if
(
j
.
is_null
())
if
(
not
j
.
is_array
())
{
{
JSON_THROW
(
std
::
domain_error
(
"type must be array, but is "
+
j
.
type_name
()));
JSON_THROW
(
std
::
domain_error
(
"type must be array, but is "
+
j
.
type_name
()));
}
}
// when T == BasicJsonType, do not check if value_t is correct
if
(
not
std
::
is_same
<
typename
CompatibleArrayType
::
value_type
,
BasicJsonType
>::
value
)
{
if
(
not
j
.
is_array
())
{
JSON_THROW
(
std
::
domain_error
(
"type must be array, but is "
+
j
.
type_name
()));
}
}
from_json_array_impl
(
j
,
arr
,
priority_tag
<
1
>
{});
from_json_array_impl
(
j
,
arr
,
priority_tag
<
1
>
{});
}
}
...
...
src/json.hpp.re2c
View file @
9355f058
...
@@ -700,22 +700,15 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
...
@@ -700,22 +700,15 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
}
}
// forward_list doesn't have an insert method
// forward_list doesn't have an insert method
template<typename BasicJsonType, typename T, typename Allocator>
template<typename BasicJsonType, typename T, typename Allocator,
enable_if_t<std::is_convertible<BasicJsonType, T>::value, int> = 0>
void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
{
{
// do not perform the check when user wants to retrieve jsons
if (not j.is_array())
// (except when it's null.. ?)
if (j.is_null())
{
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
}
}
if (not std::is_same<T, BasicJsonType>::value)
{
if (not j.is_array())
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
}
}
for (auto it = j.rbegin(), end = j.rend(); it != end; ++it)
for (auto it = j.rbegin(), end = j.rend(); it != end; ++it)
{
{
l.push_front(it->template get<T>());
l.push_front(it->template get<T>());
...
@@ -747,8 +740,8 @@ auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, prio
...
@@ -747,8 +740,8 @@ auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, prio
using std::end;
using std::end;
arr.reserve(j.size());
arr.reserve(j.size());
std::transform(
std::transform(
j.begin(), j.end(),
j.begin(), j.end(),
std::inserter(arr, end(arr)), [](const BasicJsonType & i)
std::inserter(arr, end(arr)), [](const BasicJsonType & i)
{
{
// get<BasicJsonType>() returns *this, this won't call a from_json
// get<BasicJsonType>() returns *this, this won't call a from_json
// method when value_type is BasicJsonType
// method when value_type is BasicJsonType
...
@@ -758,22 +751,15 @@ auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, prio
...
@@ -758,22 +751,15 @@ auto from_json_array_impl(const BasicJsonType& j, CompatibleArrayType& arr, prio
template<typename BasicJsonType, typename CompatibleArrayType,
template<typename BasicJsonType, typename CompatibleArrayType,
enable_if_t<is_compatible_array_type<BasicJsonType, CompatibleArrayType>::value and
enable_if_t<is_compatible_array_type<BasicJsonType, CompatibleArrayType>::value and
std::is_convertible<BasicJsonType, typename CompatibleArrayType::value_type>::value and
not std::is_same<typename BasicJsonType::array_t, CompatibleArrayType>::value, int> = 0>
not std::is_same<typename BasicJsonType::array_t, CompatibleArrayType>::value, int> = 0>
void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
{
{
if (
j.is_null
())
if (
not j.is_array
())
{
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
}
}
// when T == BasicJsonType, do not check if value_t is correct
if (not std::is_same<typename CompatibleArrayType::value_type, BasicJsonType>::value)
{
if (not j.is_array())
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
}
}
from_json_array_impl(j, arr, priority_tag<1> {});
from_json_array_impl(j, arr, priority_tag<1> {});
}
}
...
...
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