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
2bc685f6
Commit
2bc685f6
authored
Nov 09, 2016
by
Théo DELRIEU
Committed by
Théo DELRIEU
Jan 21, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
to_json and from_json takes both two arguments now
the first is the basic_json type, the second the user-defined type
parent
837b81d6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
43 deletions
+41
-43
json.hpp
src/json.hpp
+22
-27
unit-udt.cpp
test/src/unit-udt.cpp
+19
-16
No files found.
src/json.hpp
View file @
2bc685f6
...
...
@@ -155,22 +155,21 @@ void from_json();
struct
to_json_fn
{
template
<
typename
T
>
template
<
typename
Json
,
typename
T
>
constexpr
auto
operator
()(
T
&&
val
)
const
noexcept
(
noexcept
(
to_json
(
std
::
forward
<
T
>
(
val
))))
->
decltype
(
to_json
(
std
::
forward
<
T
>
(
val
)
))
operator
()(
Json
&&
j
,
T
&&
val
)
const
noexcept
(
noexcept
(
to_json
(
std
::
forward
<
Json
>
(
j
),
std
::
forward
<
T
>
(
val
))))
->
decltype
(
to_json
(
std
::
forward
<
Json
>
(
j
),
std
::
forward
<
T
>
(
val
)),
void
(
))
{
return
to_json
(
std
::
forward
<
T
>
(
val
));
return
to_json
(
std
::
forward
<
Json
>
(
j
),
std
::
forward
<
T
>
(
val
));
}
};
struct
from_json_fn
{
template
<
typename
T
,
typename
Json
>
template
<
typename
Json
,
typename
T
>
constexpr
auto
operator
()(
Json
&&
j
,
T
&
val
)
const
noexcept
(
noexcept
(
from_json
(
std
::
forward
<
Json
>
(
j
),
val
)))
->
decltype
(
from_json
(
std
::
forward
<
Json
>
(
j
),
val
))
->
decltype
(
from_json
(
std
::
forward
<
Json
>
(
j
),
val
)
,
void
()
)
{
return
from_json
(
std
::
forward
<
Json
>
(
j
),
val
);
}
...
...
@@ -219,28 +218,19 @@ inline namespace
template
<
typename
=
void
,
typename
=
void
>
struct
adl_serializer
{
template
<
typename
T
,
typename
Json
,
typename
=
enable_if_t
<
std
::
is_default_constructible
<
uncvref_t
<
T
>>::
value
>>
static
auto
from_json
(
Json
&&
j
)
->
uncvref_t
<
decltype
(
::
nlohmann
::
from_json
(
std
::
forward
<
Json
>
(
j
),
std
::
declval
<
T
&>
()),
std
::
declval
<
T
>
())
>
{
uncvref_t
<
T
>
ret
;
::
nlohmann
::
from_json
(
std
::
forward
<
Json
>
(
j
),
ret
);
return
ret
;
}
template
<
typename
T
,
typename
Json
>
static
auto
from_json
(
Json
&&
j
,
T
&
val
)
->
decltype
(
::
nlohmann
::
from_json
(
std
::
forward
<
Json
>
(
j
),
val
))
template
<
typename
Json
,
typename
T
>
static
auto
from_json
(
Json
&&
j
,
T
&
val
)
->
decltype
(
::
nlohmann
::
from_json
(
std
::
forward
<
Json
>
(
j
),
val
),
void
())
{
::
nlohmann
::
from_json
(
std
::
forward
<
Json
>
(
j
),
val
);
}
template
<
typename
T
>
static
auto
to_json
(
T
&&
val
)
->
decltype
(
::
nlohmann
::
to_json
(
std
::
forward
<
T
>
(
val
)
))
template
<
typename
Json
,
typename
T
>
static
auto
to_json
(
Json
&
j
,
T
&&
val
)
->
decltype
(
::
nlohmann
::
to_json
(
j
,
std
::
forward
<
T
>
(
val
)),
void
(
))
{
return
::
nlohmann
::
to_json
(
std
::
forward
<
T
>
(
val
));
::
nlohmann
::
to_json
(
j
,
std
::
forward
<
T
>
(
val
));
}
};
/*!
@brief a class to store JSON values
...
...
@@ -1399,9 +1389,11 @@ class basic_json
}
// constructor chosen when JSONSerializer::to_json exists for type T
template
<
typename
T
,
typename
=
decltype
(
JSONSerializer
<
uncvref_t
<
T
>>::
to_json
(
std
::
declval
<
uncvref_t
<
T
>>
()))
>
template
<
typename
T
,
typename
=
decltype
(
JSONSerializer
<
uncvref_t
<
T
>>::
to_json
(
std
::
declval
<
basic_json
&>
(),
std
::
declval
<
uncvref_t
<
T
>>
()))
>
explicit
basic_json
(
T
&&
val
)
:
basic_json
(
JSONSerializer
<
uncvref_t
<
T
>>::
to_json
(
std
::
forward
<
T
>
(
val
)))
{}
{
JSONSerializer
<
uncvref_t
<
T
>>::
to_json
(
*
this
,
std
::
forward
<
T
>
(
val
));
}
/*!
@brief create a string (explicit)
...
...
@@ -3072,11 +3064,14 @@ class basic_json
return
get_impl
(
static_cast
<
ValueType
*>
(
nullptr
));
}
template
<
typename
ValueType
,
typename
=
enable_if_t
<
std
::
is_default_constructible
<
uncvref_t
<
ValueType
>>::
value
,
float
>
>
auto
get
()
const
->
remove_reference_t
<
decltype
(
JSONSerializer
<
uncvref_t
<
ValueType
>>::
from_json
(
*
this
,
std
::
declval
<
ValueType
&>
()),
std
::
declval
<
ValueType
>
())
>
template
<
typename
ValueType
,
typename
=
decltype
(
JSONSerializer
<
uncvref_t
<
ValueType
>>::
from_json
(
std
::
declval
<
basic_json
>
(),
std
::
declval
<
ValueType
&>
()))
>
auto
get
()
const
->
uncvref_t
<
ValueType
>
{
uncvref_t
<
ValueType
>
ret
;
JSONSerializer
<
uncvref_t
<
ValueType
>>::
from_json
(
*
this
,
ret
);
using
type
=
uncvref_t
<
ValueType
>
;
static_assert
(
std
::
is_default_constructible
<
type
>::
value
&&
std
::
is_copy_constructible
<
type
>::
value
,
"user-defined types must be DefaultConstructible and CopyConstructible when used with get"
);
type
ret
;
JSONSerializer
<
type
>::
from_json
(
*
this
,
ret
);
return
ret
;
}
...
...
test/src/unit-udt.cpp
View file @
2bc685f6
...
...
@@ -70,32 +70,31 @@ private:
// free to/from_json functions
json
to_json
(
empty_type
)
void
to_json
(
json
&
j
,
empty_type
)
{
return
json
::
object
();
j
=
json
::
object
();
}
json
to_json
(
pod_type
const
&
p
)
void
to_json
(
json
&
j
,
pod_type
const
&
p
)
{
return
{{
"a"
,
p
.
a
},
{
"b"
,
p
.
b
},
{
"c"
,
p
.
c
}};
j
=
json
{{
"a"
,
p
.
a
},
{
"b"
,
p
.
b
},
{
"c"
,
p
.
c
}};
}
json
to_json
(
bit_more_complex_type
const
&
p
)
void
to_json
(
json
&
j
,
bit_more_complex_type
const
&
p
)
{
using
nlohmann
::
to_json
;
return
json
{{
"a"
,
to_json
(
p
.
a
)},
{
"b"
,
to_json
(
p
.
b
)},
{
"c"
,
p
.
c
}};
j
=
json
{{
"a"
,
json
(
p
.
a
)},
{
"b"
,
json
(
p
.
b
)},
{
"c"
,
p
.
c
}};
}
template
<
typename
T
>
json
to_json
(
optional_type
<
T
>
const
&
opt
)
void
to_json
(
json
&
j
,
optional_type
<
T
>
const
&
opt
)
{
using
nlohmann
::
to_json
;
if
(
!
opt
)
return
nullptr
;
return
json
(
*
opt
);
j
=
nullptr
;
else
j
=
json
(
*
opt
);
}
void
from_json
(
json
const
&
j
,
empty_type
&
t
)
void
from_json
(
json
const
&
j
,
empty_type
&
t
)
{
assert
(
j
.
empty
());
t
=
empty_type
{};
...
...
@@ -292,7 +291,8 @@ TEST_CASE("to_json free function", "[udt]")
auto
const
e
=
udt
::
pod_type
{
42
,
42
,
42
};
auto
const
expected
=
json
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}};
auto
const
j
=
nlohmann
::
to_json
(
e
);
json
j
;
nlohmann
::
to_json
(
j
,
e
);
CHECK
(
j
==
expected
);
}
...
...
@@ -303,7 +303,8 @@ TEST_CASE("to_json free function", "[udt]")
auto
const
expected
=
json
{{
"a"
,
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}}},
{
"b"
,
{{
"a"
,
41
},
{
"b"
,
41
},
{
"c"
,
41
}}},
{
"c"
,
"forty"
}};
auto
const
j
=
nlohmann
::
to_json
(
e
);
json
j
;
nlohmann
::
to_json
(
j
,
e
);
CHECK
(
j
==
expected
);
}
...
...
@@ -314,7 +315,8 @@ TEST_CASE("to_json free function", "[udt]")
udt
::
optional_type
<
udt
::
pod_type
>
o
;
json
expected
;
auto
const
j
=
nlohmann
::
to_json
(
o
);
json
j
;
nlohmann
::
to_json
(
j
,
o
);
CHECK
(
expected
==
j
);
}
...
...
@@ -323,7 +325,8 @@ TEST_CASE("to_json free function", "[udt]")
udt
::
optional_type
<
udt
::
pod_type
>
o
{{
42
,
42
,
42
}};
auto
const
expected
=
json
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}};
auto
const
j
=
nlohmann
::
to_json
(
o
);
json
j
;
nlohmann
::
to_json
(
j
,
o
);
CHECK
(
expected
==
j
);
}
}
...
...
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