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
8e43d476
Commit
8e43d476
authored
Dec 13, 2016
by
Théo DELRIEU
Committed by
Théo DELRIEU
Jan 21, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add more tests to unit-udt.cpp
parent
f5cb089f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
127 additions
and
0 deletions
+127
-0
test/src/unit-udt.cpp
test/src/unit-udt.cpp
+127
-0
No files found.
test/src/unit-udt.cpp
View file @
8e43d476
...
@@ -226,3 +226,130 @@ TEST_CASE("basic usage", "[udt]")
...
@@ -226,3 +226,130 @@ TEST_CASE("basic usage", "[udt]")
CHECK
(
book
==
parsed_book
);
CHECK
(
book
==
parsed_book
);
}
}
}
}
namespace
udt
{
template
<
typename
T
>
class
optional_type
{
public:
optional_type
()
=
default
;
optional_type
(
T
t
)
{
_impl
=
std
::
make_shared
<
T
>
(
std
::
move
(
t
));
}
optional_type
(
std
::
nullptr_t
)
{
_impl
=
nullptr
;
}
optional_type
&
operator
=
(
std
::
nullptr_t
)
{
_impl
=
nullptr
;
return
*
this
;
}
optional_type
&
operator
=
(
T
t
)
{
_impl
=
std
::
make_shared
<
T
>
(
std
::
move
(
t
));
return
*
this
;
}
explicit
operator
bool
()
const
noexcept
{
return
_impl
!=
nullptr
;
}
T
const
&
operator
*
()
const
noexcept
{
return
*
_impl
;
}
T
&
operator
*
()
noexcept
{
return
*
_impl
;
}
private:
std
::
shared_ptr
<
T
>
_impl
;
};
struct
legacy_type
{
std
::
string
number
;
};
}
namespace
nlohmann
{
template
<
typename
T
>
struct
adl_serializer
<
udt
::
optional_type
<
T
>>
{
static
void
to_json
(
json
&
j
,
udt
::
optional_type
<
T
>
const
&
opt
)
{
if
(
opt
)
j
=
*
opt
;
else
j
=
nullptr
;
}
static
void
from_json
(
json
const
&
j
,
udt
::
optional_type
<
T
>
&
opt
)
{
if
(
j
.
is_null
())
opt
=
nullptr
;
else
opt
=
j
.
get
<
T
>
();
}
};
template
<
>
struct
adl_serializer
<
udt
::
legacy_type
>
{
static
void
to_json
(
json
&
j
,
udt
::
legacy_type
const
&
l
)
{
j
=
std
::
stoi
(
l
.
number
);
}
static
void
from_json
(
json
const
&
j
,
udt
::
legacy_type
&
l
)
{
l
.
number
=
std
::
to_string
(
j
.
get
<
int
>
());
}
};
}
TEST_CASE
(
"adl_serializer specialization"
,
"[udt]"
)
{
using
nlohmann
::
json
;
SECTION
(
"partial specialization"
)
{
SECTION
(
"to_json"
)
{
udt
::
optional_type
<
udt
::
person
>
optPerson
;
json
j
=
optPerson
;
CHECK
(
j
.
is_null
());
optPerson
=
udt
::
person
{{
42
},
{
"John Doe"
}};
j
=
optPerson
;
CHECK_FALSE
(
j
.
is_null
());
CHECK
(
j
.
get
<
udt
::
person
>
()
==
*
optPerson
);
}
SECTION
(
"from_json"
)
{
auto
person
=
udt
::
person
{{
42
},
{
"John Doe"
}};
json
j
=
person
;
auto
optPerson
=
j
.
get
<
udt
::
optional_type
<
udt
::
person
>>
();
REQUIRE
(
optPerson
);
CHECK
(
*
optPerson
==
person
);
j
=
nullptr
;
optPerson
=
j
.
get
<
udt
::
optional_type
<
udt
::
person
>>
();
CHECK
(
!
optPerson
);
}
}
SECTION
(
"total specialization"
)
{
SECTION
(
"to_json"
)
{
udt
::
legacy_type
lt
{
"4242"
};
json
j
=
lt
;
CHECK
(
j
.
get
<
int
>
()
==
4242
);
}
SECTION
(
"from_json"
)
{
json
j
=
4242
;
auto
lt
=
j
.
get
<
udt
::
legacy_type
>
();
CHECK
(
lt
.
number
==
"4242"
);
}
}
}
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