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
c7e079cc
Unverified
Commit
c7e079cc
authored
Jun 30, 2020
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🚑
add specialization of get_to #2175
parent
eb7376bb
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
11 deletions
+41
-11
include/nlohmann/json.hpp
include/nlohmann/json.hpp
+12
-0
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+12
-0
test/src/unit-udt_macro.cpp
test/src/unit-udt_macro.cpp
+17
-11
No files found.
include/nlohmann/json.hpp
View file @
c7e079cc
...
...
@@ -3019,6 +3019,18 @@ class basic_json
return
v
;
}
// specialization to allow to call get_to with a basic_json value
// see https://github.com/nlohmann/json/issues/2175
template
<
typename
ValueType
,
detail
::
enable_if_t
<
detail
::
is_basic_json
<
ValueType
>
::
value
,
int
>
=
0
>
ValueType
&
get_to
(
ValueType
&
v
)
const
{
v
=
*
this
;
return
v
;
}
template
<
typename
T
,
std
::
size_t
N
,
typename
Array
=
T
(
&
)[
N
],
...
...
single_include/nlohmann/json.hpp
View file @
c7e079cc
...
...
@@ -18949,6 +18949,18 @@ class basic_json
return
v
;
}
// specialization to allow to call get_to with a basic_json value
// see https://github.com/nlohmann/json/issues/2175
template
<
typename
ValueType
,
detail
::
enable_if_t
<
detail
::
is_basic_json
<
ValueType
>
::
value
,
int
>
=
0
>
ValueType
&
get_to
(
ValueType
&
v
)
const
{
v
=
*
this
;
return
v
;
}
template
<
typename
T
,
std
::
size_t
N
,
typename
Array
=
T
(
&
)[
N
],
...
...
test/src/unit-udt_macro.cpp
View file @
c7e079cc
...
...
@@ -41,20 +41,22 @@ class person_with_private_data
private:
std
::
string
name
;
int
age
=
0
;
json
metadata
;
public:
bool
operator
==
(
const
person_with_private_data
&
rhs
)
const
{
return
std
::
tie
(
name
,
age
)
==
std
::
tie
(
rhs
.
name
,
rhs
.
age
);
return
std
::
tie
(
name
,
age
,
metadata
)
==
std
::
tie
(
rhs
.
name
,
rhs
.
age
,
rhs
.
metadata
);
}
person_with_private_data
()
=
default
;
person_with_private_data
(
std
::
string
name
,
int
age
)
person_with_private_data
(
std
::
string
name
,
int
age
,
json
metadata
)
:
name
(
std
::
move
(
name
))
,
age
(
age
)
,
metadata
(
std
::
move
(
metadata
))
{}
NLOHMANN_DEFINE_TYPE_INTRUSIVE
(
person_with_private_data
,
age
,
name
);
NLOHMANN_DEFINE_TYPE_INTRUSIVE
(
person_with_private_data
,
age
,
name
,
metadata
);
};
class
person_without_private_data_1
...
...
@@ -62,19 +64,21 @@ class person_without_private_data_1
public:
std
::
string
name
;
int
age
=
0
;
json
metadata
;
bool
operator
==
(
const
person_without_private_data_1
&
rhs
)
const
{
return
std
::
tie
(
name
,
age
)
==
std
::
tie
(
rhs
.
name
,
rhs
.
age
);
return
std
::
tie
(
name
,
age
,
metadata
)
==
std
::
tie
(
rhs
.
name
,
rhs
.
age
,
rhs
.
metadata
);
}
person_without_private_data_1
()
=
default
;
person_without_private_data_1
(
std
::
string
name
,
int
age
)
person_without_private_data_1
(
std
::
string
name
,
int
age
,
json
metadata
)
:
name
(
std
::
move
(
name
))
,
age
(
age
)
,
metadata
(
std
::
move
(
metadata
))
{}
NLOHMANN_DEFINE_TYPE_INTRUSIVE
(
person_without_private_data_1
,
age
,
name
);
NLOHMANN_DEFINE_TYPE_INTRUSIVE
(
person_without_private_data_1
,
age
,
name
,
metadata
);
};
class
person_without_private_data_2
...
...
@@ -82,20 +86,22 @@ class person_without_private_data_2
public:
std
::
string
name
;
int
age
=
0
;
json
metadata
;
bool
operator
==
(
const
person_without_private_data_2
&
rhs
)
const
{
return
std
::
tie
(
name
,
age
)
==
std
::
tie
(
rhs
.
name
,
rhs
.
age
);
return
std
::
tie
(
name
,
age
,
metadata
)
==
std
::
tie
(
rhs
.
name
,
rhs
.
age
,
rhs
.
metadata
);
}
person_without_private_data_2
()
=
default
;
person_without_private_data_2
(
std
::
string
name
,
int
age
)
person_without_private_data_2
(
std
::
string
name
,
int
age
,
json
metadata
)
:
name
(
std
::
move
(
name
))
,
age
(
age
)
,
metadata
(
std
::
move
(
metadata
))
{}
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
(
person_without_private_data_2
,
age
,
name
);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
(
person_without_private_data_2
,
age
,
name
,
metadata
);
}
// namespace persons
TEST_CASE_TEMPLATE
(
"Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE"
,
T
,
...
...
@@ -106,8 +112,8 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRU
SECTION
(
"person"
)
{
// serialization
T
p1
(
"Erik"
,
1
);
CHECK
(
json
(
p1
).
dump
()
==
"{
\"
age
\"
:1,
\"
name
\"
:
\"
Erik
\"
}"
);
T
p1
(
"Erik"
,
1
,
{{
"haircuts"
,
2
}}
);
CHECK
(
json
(
p1
).
dump
()
==
"{
\"
age
\"
:1,
\"
metadata
\"
:{
\"
haircuts
\"
:2},
\"
name
\"
:
\"
Erik
\"
}"
);
// deserialization
T
p2
=
json
(
p1
);
...
...
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