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
50a3f3b3
Commit
50a3f3b3
authored
Jan 24, 2017
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
📝
added nicer example
parent
781fd09f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
5 deletions
+13
-5
README.md
README.md
+13
-5
No files found.
README.md
View file @
50a3f3b3
...
@@ -457,9 +457,10 @@ namespace ns {
...
@@ -457,9 +457,10 @@ namespace ns {
};
};
}
}
ns
::
person
p
=
{
"Ned Flanders"
,
"744 Evergreen Terrace"
,
60
};
// convert to JSON: copy each value into the JSON object
// convert to JSON: copy each value into the JSON object
json
j
;
json
j
;
ns
::
person
p
=
createSomeone
();
j
[
"name"
]
=
p
.
name
;
j
[
"name"
]
=
p
.
name
;
j
[
"address"
]
=
p
.
address
;
j
[
"address"
]
=
p
.
address
;
j
[
"age"
]
=
p
.
age
;
j
[
"age"
]
=
p
.
age
;
...
@@ -477,12 +478,19 @@ ns::person p {
...
@@ -477,12 +478,19 @@ ns::person p {
It works, but that's quite a lot of boilerplate... Hopefully, there's a better way:
It works, but that's quite a lot of boilerplate... Hopefully, there's a better way:
```
cpp
```
cpp
// person -> json
// create a person
ns
::
person
p
=
createPerson
();
ns
::
person
p
{
"Ned Flanders"
,
"744 Evergreen Terrace"
,
60
};
// conversion: person -> json
json
j
=
p
;
json
j
=
p
;
// json -> person
std
::
cout
<<
j
<<
std
::
endl
;
auto
p2
=
j
.
get
<
ns
::
person
>
();
// {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}
// conversion: json -> person
ns
::
person
p2
=
j
;
// that's it
assert
(
p
==
p2
);
assert
(
p
==
p2
);
```
```
...
...
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