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
60e6f822
Commit
60e6f822
authored
Nov 29, 2016
by
Théo DELRIEU
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add support for non-default-constructible udt
parent
e5999c6c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
107 additions
and
407 deletions
+107
-407
json.hpp
src/json.hpp
+54
-17
unit-udt.cpp
test/src/unit-udt.cpp
+53
-390
No files found.
src/json.hpp
View file @
60e6f822
...
@@ -290,14 +290,32 @@ struct is_compatible_basic_json_type
...
@@ -290,14 +290,32 @@ struct is_compatible_basic_json_type
};
};
// This trait checks if JSONSerializer<T>::from_json exists
// This trait checks if JSONSerializer<T>::from_json
(json const&, udt&)
exists
template
<
template
<
typename
,
typename
>
class
JSONSerializer
,
typename
Json
,
template
<
template
<
typename
,
typename
>
class
JSONSerializer
,
typename
Json
,
typename
T
>
typename
T
>
struct
has_from_json
struct
has_from_json
{
{
private
:
private
:
template
<
typename
U
,
typename
=
decltype
(
uncvref_t
<
U
>::
from_json
(
// also check the return type of from_json
std
::
declval
<
Json
>
(),
std
::
declval
<
T
&>
()))
>
template
<
typename
U
,
typename
=
enable_if_t
<
std
::
is_same
<
void
,
decltype
(
uncvref_t
<
U
>::
from_json
(
std
::
declval
<
Json
>
(),
std
::
declval
<
T
&>
()))
>::
value
>>
static
int
detect
(
U
&&
);
static
void
detect
(...);
public
:
static
constexpr
bool
value
=
std
::
is_integral
<
decltype
(
detect
(
std
::
declval
<
JSONSerializer
<
T
,
void
>>
()))
>::
value
;
};
// This trait checks if JSONSerializer<T>::from_json(json const&) exists
// this overload is used for non-default-constructible user-defined-types
template
<
template
<
typename
,
typename
>
class
JSONSerializer
,
typename
Json
,
typename
T
>
struct
has_non_default_from_json
{
private
:
template
<
typename
U
,
typename
=
enable_if_t
<
std
::
is_same
<
T
,
decltype
(
uncvref_t
<
U
>::
from_json
(
std
::
declval
<
Json
>
()))
>::
value
>>
static
int
detect
(
U
&&
);
static
int
detect
(
U
&&
);
static
void
detect
(...);
static
void
detect
(...);
...
@@ -326,8 +344,8 @@ public:
...
@@ -326,8 +344,8 @@ public:
// those declarations are needed to workaround a MSVC bug related to ADL
// those declarations are needed to workaround a MSVC bug related to ADL
// (taken from MSVC-Ranges implementation)
// (taken from MSVC-Ranges implementation)
void
to_json
();
//
void to_json();
void
from_json
();
//
void from_json();
struct
to_json_fn
struct
to_json_fn
{
{
...
@@ -2467,6 +2485,18 @@ class basic_json
...
@@ -2467,6 +2485,18 @@ class basic_json
return
*
this
;
return
*
this
;
}
}
// this overload is needed, since constructor for udt is explicit
template
<
typename
T
,
enable_if_t
<
not
detail
::
is_compatible_basic_json_type
<
uncvref_t
<
T
>
,
basic_json_t
>::
value
and
detail
::
has_to_json
<
JSONSerializer
,
basic_json_t
,
uncvref_t
<
T
>>::
value
>>
reference
&
operator
=
(
T
&&
val
)
noexcept
(
std
::
is_nothrow_constructible
<
basic_json_t
,
uncvref_t
<
T
>>::
value
and
std
::
is_nothrow_move_assignable
<
uncvref_t
<
T
>>::
value
)
{
static_assert
(
sizeof
(
T
)
==
0
,
""
);
// I'm not sure this a is good practice...
return
*
this
=
basic_json_t
{
std
::
forward
<
T
>
(
val
)};
}
/*!
/*!
@brief destructor
@brief destructor
...
@@ -2482,38 +2512,32 @@ class basic_json
...
@@ -2482,38 +2512,32 @@ class basic_json
@since version 1.0.0
@since version 1.0.0
*/
*/
~
basic_json
()
~
basic_json
()
{
{
assert_invariant
();
assert_invariant
();
switch
(
m_type
)
switch
(
m_type
)
{
{
case
value_t
:
:
object
:
{
case
value_t
:
:
object
:
{
AllocatorType
<
object_t
>
alloc
;
AllocatorType
<
object_t
>
alloc
;
alloc
.
destroy
(
m_value
.
object
);
alloc
.
destroy
(
m_value
.
object
);
alloc
.
deallocate
(
m_value
.
object
,
1
);
alloc
.
deallocate
(
m_value
.
object
,
1
);
break
;
break
;
}
}
case
value_t
:
:
array
:
case
value_t
:
:
array
:
{
{
AllocatorType
<
array_t
>
alloc
;
AllocatorType
<
array_t
>
alloc
;
alloc
.
destroy
(
m_value
.
array
);
alloc
.
destroy
(
m_value
.
array
);
alloc
.
deallocate
(
m_value
.
array
,
1
);
alloc
.
deallocate
(
m_value
.
array
,
1
);
break
;
break
;
}
}
case
value_t
:
:
string
:
case
value_t
:
:
string
:
{
{
AllocatorType
<
string_t
>
alloc
;
AllocatorType
<
string_t
>
alloc
;
alloc
.
destroy
(
m_value
.
string
);
alloc
.
destroy
(
m_value
.
string
);
alloc
.
deallocate
(
m_value
.
string
,
1
);
alloc
.
deallocate
(
m_value
.
string
,
1
);
break
;
break
;
}
}
default
:
default
:
{
{
// all other types need no specific destructor
// all other types need no specific destructor
break
;
break
;
}
}
...
@@ -3273,6 +3297,19 @@ class basic_json
...
@@ -3273,6 +3297,19 @@ class basic_json
return
ret
;
return
ret
;
}
}
// This overload is chosen for non-default constructible user-defined-types
template
<
typename
T
,
enable_if_t
<
not
detail
::
is_compatible_basic_json_type
<
T
,
basic_json_t
>::
value
and
detail
::
has_non_default_from_json
<
JSONSerializer
,
basic_json_t
,
T
>::
value
,
short
>
=
0
>
T
get
()
const
{
return
JSONSerializer
<
T
>::
from_json
(
*
this
);
}
/*!
/*!
@brief get a pointer value (explicit)
@brief get a pointer value (explicit)
...
...
test/src/unit-udt.cpp
View file @
60e6f822
...
@@ -26,443 +26,105 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
...
@@ -26,443 +26,105 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
*/
*/
#include <array>
#include <string>
#include <string>
#include <memory>
#include <memory>
#include "catch.hpp"
#include "catch.hpp"
#include "json.hpp"
#include "json.hpp"
using
nlohmann
::
json
;
namespace
udt
namespace
udt
{
{
struct
empty_type
{};
struct
age
struct
pod_type
{
int
a
;
char
b
;
short
c
;
};
struct
bit_more_complex_type
{
pod_type
a
;
pod_type
b
;
std
::
string
c
;
};
// best optional implementation ever
template
<
typename
T
>
class
optional_type
{
public
:
optional_type
()
=
default
;
explicit
optional_type
(
T
val
)
:
_val
(
std
::
make_shared
<
T
>
(
std
::
move
(
val
)))
{}
explicit
operator
bool
()
const
noexcept
{
return
_val
!=
nullptr
;
}
T
const
&
operator
*
()
const
{
return
*
_val
;
}
optional_type
&
operator
=
(
T
const
&
t
)
{
_val
=
std
::
make_shared
<
T
>
(
t
);
return
*
this
;
}
private
:
std
::
shared_ptr
<
T
>
_val
;
};
// free to/from_json functions
void
to_json
(
json
&
j
,
empty_type
)
{
j
=
json
::
object
();
}
void
to_json
(
json
&
j
,
pod_type
const
&
p
)
{
j
=
json
{{
"a"
,
p
.
a
},
{
"b"
,
p
.
b
},
{
"c"
,
p
.
c
}};
}
void
to_json
(
json
&
j
,
bit_more_complex_type
const
&
p
)
{
j
=
json
{{
"a"
,
json
(
p
.
a
)},
{
"b"
,
json
(
p
.
b
)},
{
"c"
,
p
.
c
}};
}
template
<
typename
T
>
void
to_json
(
json
&
j
,
optional_type
<
T
>
const
&
opt
)
{
if
(
!
opt
)
j
=
nullptr
;
else
j
=
json
(
*
opt
);
}
void
from_json
(
json
const
&
j
,
empty_type
&
t
)
{
assert
(
j
.
empty
());
t
=
empty_type
{};
}
void
from_json
(
json
const
&
j
,
pod_type
&
t
)
{
t
=
{
j
[
"a"
].
get
<
int
>
(),
j
[
"b"
].
get
<
char
>
(),
j
[
"c"
].
get
<
short
>
()};
}
void
from_json
(
json
const
&
j
,
bit_more_complex_type
&
t
)
{
// relying on json_traits struct here..
t
=
{
j
[
"a"
].
get
<
udt
::
pod_type
>
(),
j
[
"b"
].
get
<
udt
::
pod_type
>
(),
j
[
"c"
].
get
<
std
::
string
>
()};
}
template
<
typename
T
>
void
from_json
(
json
const
&
j
,
optional_type
<
T
>&
t
)
{
if
(
j
.
is_null
())
t
=
optional_type
<
T
>
{};
else
t
=
j
.
get
<
T
>
();
}
inline
bool
operator
==
(
pod_type
const
&
lhs
,
pod_type
const
&
rhs
)
noexcept
{
return
std
::
tie
(
lhs
.
a
,
lhs
.
b
,
lhs
.
c
)
==
std
::
tie
(
rhs
.
a
,
rhs
.
b
,
rhs
.
c
);
}
inline
bool
operator
==
(
bit_more_complex_type
const
&
lhs
,
bit_more_complex_type
const
&
rhs
)
noexcept
{
return
std
::
tie
(
lhs
.
a
,
lhs
.
b
,
lhs
.
c
)
==
std
::
tie
(
rhs
.
a
,
rhs
.
b
,
rhs
.
c
);
}
template
<
typename
T
>
inline
bool
operator
==
(
optional_type
<
T
>
const
&
lhs
,
optional_type
<
T
>
const
&
rhs
)
{
if
(
!
lhs
&&
!
rhs
)
return
true
;
if
(
!
lhs
||
!
rhs
)
return
false
;
return
*
lhs
==
*
rhs
;
}
}
TEST_CASE
(
"constructors for user-defined types"
,
"[udt]"
)
{
SECTION
(
"empty type"
)
{
udt
::
empty_type
const
e
{};
auto
const
j
=
json
{
e
};
auto
k
=
json
::
object
();
CHECK
(
j
==
k
);
}
SECTION
(
"pod type"
)
{
auto
const
e
=
udt
::
pod_type
{
42
,
42
,
42
};
auto
j
=
json
{
e
};
auto
k
=
json
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}};
CHECK
(
j
==
k
);
}
SECTION
(
"bit more complex type"
)
{
auto
const
e
=
udt
::
bit_more_complex_type
{{
42
,
42
,
42
},
{
41
,
41
,
41
},
"forty"
};
auto
j
=
json
{
e
};
auto
k
=
json
{{
"a"
,
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}}},
{
"b"
,
{{
"a"
,
41
},
{
"b"
,
41
},
{
"c"
,
41
}}},
{
"c"
,
"forty"
}};
CHECK
(
j
==
k
);
}
SECTION
(
"vector of udt"
)
{
std
::
vector
<
udt
::
bit_more_complex_type
>
v
;
auto
const
e
=
udt
::
bit_more_complex_type
{{
42
,
42
,
42
},
{
41
,
41
,
41
},
"forty"
};
v
.
emplace_back
(
e
);
v
.
emplace_back
(
e
);
v
.
emplace_back
(
e
);
json
j
=
v
;
auto
k
=
json
{{
"a"
,
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}}},
{
"b"
,
{{
"a"
,
41
},
{
"b"
,
41
},
{
"c"
,
41
}}},
{
"c"
,
"forty"
}};
auto
l
=
json
{
k
,
k
,
k
};
CHECK
(
j
==
l
);
}
SECTION
(
"optional type"
)
{
SECTION
(
"regular case"
)
{
udt
::
optional_type
<
int
>
u
{
3
};
CHECK
(
json
{
u
}
==
json
(
3
));
}
SECTION
(
"nullopt case"
)
{
udt
::
optional_type
<
float
>
v
;
CHECK
(
json
{
v
}
==
json
{});
}
SECTION
(
"optional of json convertible type"
)
{
{
auto
const
e
=
int
val
;
udt
::
bit_more_complex_type
{{
42
,
42
,
42
},
{
41
,
41
,
41
},
"forty"
};
};
udt
::
optional_type
<
udt
::
bit_more_complex_type
>
o
{
e
};
auto
k
=
json
{{
"a"
,
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}}},
{
"b"
,
{{
"a"
,
41
},
{
"b"
,
41
},
{
"c"
,
41
}}},
{
"c"
,
"forty"
}};
CHECK
(
json
{
o
}
==
k
);
}
SECTION
(
"optional of vector of json convertible type"
)
{
std
::
vector
<
udt
::
bit_more_complex_type
>
v
;
auto
const
e
=
udt
::
bit_more_complex_type
{{
42
,
42
,
42
},
{
41
,
41
,
41
},
"forty"
};
v
.
emplace_back
(
e
);
v
.
emplace_back
(
e
);
v
.
emplace_back
(
e
);
udt
::
optional_type
<
std
::
vector
<
udt
::
bit_more_complex_type
>>
o
{
v
};
auto
k
=
json
{{
"a"
,
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}}},
{
"b"
,
{{
"a"
,
41
},
{
"b"
,
41
},
{
"c"
,
41
}}},
{
"c"
,
"forty"
}};
auto
l
=
json
{
k
,
k
,
k
};
CHECK
(
json
{
o
}
==
l
);
}
}
}
TEST_CASE
(
"get<> for user-defined types"
,
"[udt]"
)
struct
name
{
SECTION
(
"pod type"
)
{
{
auto
const
e
=
udt
::
pod_type
{
42
,
42
,
42
};
std
::
string
val
;
auto
const
j
=
json
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}};
};
auto
const
obj
=
j
.
get
<
udt
::
pod_type
>
();
CHECK
(
e
==
obj
);
}
SECTION
(
"bit more complex type"
)
struct
address
{
{
auto
const
e
=
std
::
string
val
;
udt
::
bit_more_complex_type
{{
42
,
42
,
42
},
{
41
,
41
,
41
},
"forty"
};
};
auto
const
j
=
json
{{
"a"
,
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}}},
{
"b"
,
{{
"a"
,
41
},
{
"b"
,
41
},
{
"c"
,
41
}}},
{
"c"
,
"forty"
}};
auto
const
obj
=
j
.
get
<
udt
::
bit_more_complex_type
>
();
CHECK
(
e
==
obj
);
}
SECTION
(
"vector of udt"
)
struct
person
{
{
auto
const
e
=
age
age
;
udt
::
bit_more_complex_type
{{
42
,
42
,
42
},
{
41
,
41
,
41
},
"forty"
};
name
name
;
std
::
vector
<
udt
::
bit_more_complex_type
>
v
{
e
,
e
,
e
};
};
auto
const
j
=
json
(
v
);
auto
const
obj
=
j
.
get
<
decltype
(
v
)
>
();
CHECK
(
v
==
obj
);
}
SECTION
(
"optional"
)
struct
contact
{
SECTION
(
"from null"
)
{
{
udt
::
optional_type
<
int
>
o
;
person
person
;
json
j
;
address
address
;
CHECK
(
j
.
get
<
decltype
(
o
)
>
()
==
o
);
};
}
SECTION
(
"from value"
)
struct
contact_book
{
{
json
j
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}};
name
book_name
;
auto
v
=
j
.
get
<
udt
::
optional_type
<
udt
::
pod_type
>>
();
std
::
vector
<
contact
>
contacts
;
auto
expected
=
udt
::
pod_type
{
42
,
42
,
42
};
};
REQUIRE
(
v
);
CHECK
(
*
v
==
expected
);
}
}
}
}
TEST_CASE
(
"to_json free function"
,
"[udt]"
)
// to_json methods for default basic_json
namespace
udt
{
{
SECTION
(
"pod_type"
)
void
to_json
(
nlohmann
::
json
&
j
,
age
a
)
{
auto
const
e
=
udt
::
pod_type
{
42
,
42
,
42
};
auto
const
expected
=
json
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}};
json
j
;
nlohmann
::
to_json
(
j
,
e
);
CHECK
(
j
==
expected
);
}
SECTION
(
"bit_more_complex_type"
)
{
{
auto
const
e
=
j
=
a
.
val
;
udt
::
bit_more_complex_type
{{
42
,
42
,
42
},
{
41
,
41
,
41
},
"forty"
};
auto
const
expected
=
json
{{
"a"
,
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}}},
{
"b"
,
{{
"a"
,
41
},
{
"b"
,
41
},
{
"c"
,
41
}}},
{
"c"
,
"forty"
}};
json
j
;
nlohmann
::
to_json
(
j
,
e
);
CHECK
(
j
==
expected
);
}
}
SECTION
(
"optional_type"
)
void
to_json
(
nlohmann
::
json
&
j
,
name
const
&
n
)
{
{
SECTION
(
"from null"
)
j
=
n
.
val
;
{
udt
::
optional_type
<
udt
::
pod_type
>
o
;
json
expected
;
json
j
;
nlohmann
::
to_json
(
j
,
o
);
CHECK
(
expected
==
j
);
}
}
SECTION
(
"from value"
)
void
to_json
(
nlohmann
::
json
&
j
,
person
const
&
p
)
{
udt
::
optional_type
<
udt
::
pod_type
>
o
{{
42
,
42
,
42
}};
auto
const
expected
=
json
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}};
json
j
;
nlohmann
::
to_json
(
j
,
o
);
CHECK
(
expected
==
j
);
}
}
}
TEST_CASE
(
"from_json free function"
,
"[udt]"
)
{
SECTION
(
"pod_type"
)
{
{
auto
const
expected
=
udt
::
pod_type
{
42
,
42
,
42
}
;
using
nlohmann
::
json
;
auto
const
j
=
json
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}};
j
=
json
{{
"age"
,
json
{
p
.
age
}},
{
"name"
,
json
{
p
.
name
}
}};
udt
::
pod_type
p
;
// this unfortunately does not compile ...
nlohmann
::
from_json
(
j
,
p
)
;
// j["age"] = p.age
;
CHECK
(
p
==
expected
)
;
// j["name"] = p.name
;
}
}
SECTION
(
"bit_more_complex_type"
)
void
to_json
(
nlohmann
::
json
&
j
,
address
const
&
a
)
{
{
auto
const
expected
=
j
=
a
.
val
;
udt
::
bit_more_complex_type
{{
42
,
42
,
42
},
{
41
,
41
,
41
},
"forty"
};
auto
const
j
=
json
{{
"a"
,
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}}},
{
"b"
,
{{
"a"
,
41
},
{
"b"
,
41
},
{
"c"
,
41
}}},
{
"c"
,
"forty"
}};
udt
::
bit_more_complex_type
p
;
nlohmann
::
from_json
(
j
,
p
);
CHECK
(
p
==
expected
);
}
}
SECTION
(
"optional_type"
)
void
to_json
(
nlohmann
::
json
&
j
,
contact
const
&
c
)
{
{
SECTION
(
"from null"
)
using
nlohmann
::
json
;
{
j
=
json
{{
"person"
,
json
{
c
.
person
}},
{
"address"
,
json
{
c
.
address
}}};
udt
::
optional_type
<
udt
::
pod_type
>
expected
;
json
j
;
udt
::
optional_type
<
udt
::
pod_type
>
o
;
nlohmann
::
from_json
(
j
,
o
);
CHECK
(
expected
==
o
);
}
}
SECTION
(
"from value"
)
void
to_json
(
nlohmann
::
json
&
j
,
contact_book
const
&
cb
)
{
{
udt
::
optional_type
<
udt
::
pod_type
>
expected
{{
42
,
42
,
42
}};
using
nlohmann
::
json
;
auto
const
j
=
json
{{
"a"
,
42
},
{
"b"
,
42
},
{
"c"
,
42
}};
j
=
json
{{
"name"
,
json
{
cb
.
book_name
}},
{
"contacts"
,
cb
.
contacts
}};
udt
::
optional_type
<
udt
::
pod_type
>
o
;
nlohmann
::
from_json
(
j
,
o
);
CHECK
(
expected
==
o
);
}
}
}
}
}
// custom serializer, uses adl by default
TEST_CASE
(
"basic usage"
,
"[udt]"
)
template
<
typename
T
,
typename
=
void
>
struct
my_serializer
{
template
<
typename
Json
>
static
void
from_json
(
Json
const
&
j
,
T
&
val
)
{
nlohmann
::
from_json
(
j
,
val
);
}
template
<
typename
Json
>
static
void
to_json
(
Json
&
j
,
T
const
&
val
)
{
nlohmann
::
to_json
(
j
,
val
);
}
};
// partial specialization on optional_type
template
<
typename
T
>
struct
my_serializer
<
udt
::
optional_type
<
T
>>
{
{
template
<
typename
Json
>
using
nlohmann
::
json
;
static
void
from_json
(
Json
const
&
j
,
udt
::
optional_type
<
T
>&
opt
)
{
if
(
j
.
is_null
())
opt
=
nullptr
;
else
opt
=
j
.
get
<
T
>
();
}
template
<
typename
Json
>
SECTION
(
"conversion to json via free-functions"
)
static
void
to_json
(
Json
&
j
,
udt
::
optional_type
<
T
>
const
&
opt
)
{
{
if
(
opt
)
udt
::
age
a
{
23
};
j
=
*
opt
;
else
j
=
nullptr
;
}
};
using
my_json
=
nlohmann
::
basic_json
<
std
::
map
,
std
::
vector
,
std
::
string
,
bool
,
CHECK
(
json
{
a
}
==
json
{
23
});
std
::
int64_t
,
std
::
uint64_t
,
double
,
std
::
allocator
,
my_serializer
>
;
namespace
udt
{
void
to_json
(
my_json
&
j
,
pod_type
const
&
val
)
{
j
=
my_json
{{
"a"
,
val
.
a
},
{
"b"
,
val
.
b
},
{
"c"
,
val
.
c
}};
}
void
from_json
(
my_json
const
&
j
,
pod_type
&
val
)
{
val
=
{
j
[
"a"
].
get
<
int
>
(),
j
[
"b"
].
get
<
char
>
(),
j
[
"c"
].
get
<
short
>
()};
}
}
TEST_CASE
(
"custom serializer"
,
"[udt]"
)
{
SECTION
(
"default use works like default serializer"
)
{
udt
::
pod_type
pod
{
1
,
2
,
3
};
auto
const
j
=
my_json
{
pod
};
auto
const
j2
=
json
{
pod
};
CHECK
(
j
.
dump
()
==
j2
.
dump
());
auto
const
pod2
=
j
.
get
<
udt
::
pod_type
>
();
auto
const
pod3
=
j2
.
get
<
udt
::
pod_type
>
();
CHECK
(
pod2
==
pod3
);
CHECK
(
pod2
==
pod
);
}
SECTION
(
"serializer specialization"
)
{
udt
::
optional_type
<
int
>
opt
;
json
j
{
opt
};
// a bit narcissic maybe :) ?
CHECK
(
j
.
is_null
());
udt
::
name
n
{
"theo"
};
CHECK
(
json
{
n
}
==
json
{
"theo"
});
opt
=
42
;
udt
::
person
sfinae_addict
{
a
,
n
};
j
=
json
{
opt
};
CHECK
(
json
{
sfinae_addict
}
==
R"({"name":"theo", "age":23})"
_json
);
CHECK
(
j
.
get
<
udt
::
optional_type
<
int
>>
()
==
opt
);
CHECK
(
42
==
j
.
get
<
int
>
());
}
}
}
}
\ No newline at end of file
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