FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
json/example/pretty.cpp at develop · boostorg/json · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
boostorg
/
json
Public
Notifications
You must be signed in to change notification settings
Fork
109
Star
478
Code
Issues
66
Pull requests
26
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
json
/
example
/
pretty.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
158 lines (140 loc) · 3.25 KB
Breadcrumbs
json
/
example
/
pretty.cpp
Copy path
File metadata and controls
158 lines (140 loc) · 3.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
//
Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
//
//
Distributed under the Boost Software License, Version 1.0. (See accompanying
//
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
//
Official repository: https://github.com/boostorg/json
//
//
tag::example_pretty[]
/*
This example parses a JSON file and pretty-prints
it to standard output.
*/
#
include
<
boost/json.hpp
>
#
include
<
iomanip
>
#
include
<
iostream
>
#
include
"
file.hpp
"
namespace
json
=
boost::json;
json::value
parse_file
(
char
const
* filename )
{
file
f
( filename,
"
r
"
);
json::stream_parser p;
boost::system::error_code ec;
do
{
char
buf[
4096
];
auto
const
nread = f.
read
( buf,
sizeof
(buf) );
p.
write
( buf, nread, ec );
}
while
( ! f.
eof
() );
if
( ec )
return
nullptr
;
p.
finish
( ec );
if
( ec )
return
nullptr
;
return
p.
release
();
}
void
pretty_print
( std::ostream& os, json::value
const
& jv, std::string* indent =
nullptr
)
{
std::string indent_;
if
(! indent)
indent = &indent_;
switch
(jv.
kind
())
{
case
json::kind::object:
{
os <<
"
{
\n
"
;
indent->
append
(
4
,
'
'
);
auto
const
& obj = jv.
get_object
();
if
(! obj.
empty
())
{
auto
it = obj.
begin
();
for
(;;)
{
os << *indent <<
json::serialize
(it->
key
()) <<
"
:
"
;
pretty_print
(os, it->
value
(), indent);
if
(++it == obj.
end
())
break
;
os <<
"
,
\n
"
;
}
}
os <<
"
\n
"
;
indent->
resize
(indent->
size
() -
4
);
os << *indent <<
"
}
"
;
break
;
}
case
json::kind::array:
{
os <<
"
[
\n
"
;
indent->
append
(
4
,
'
'
);
auto
const
& arr = jv.
get_array
();
if
(! arr.
empty
())
{
auto
it = arr.
begin
();
for
(;;)
{
os << *indent;
pretty_print
( os, *it, indent);
if
(++it == arr.
end
())
break
;
os <<
"
,
\n
"
;
}
}
os <<
"
\n
"
;
indent->
resize
(indent->
size
() -
4
);
os << *indent <<
"
]
"
;
break
;
}
case
json::kind::string:
{
os <<
json::serialize
(jv.
get_string
());
break
;
}
case
json::kind::uint64:
case
json::kind::int64:
case
json::kind::double_:
os << jv;
break
;
case
json::kind::bool_:
if
(jv.
get_bool
())
os <<
"
true
"
;
else
os <<
"
false
"
;
break
;
case
json::kind::null:
os <<
"
null
"
;
break
;
}
if
(indent->
empty
())
os <<
"
\n
"
;
}
int
main
(
int
argc,
char
** argv)
{
if
(argc !=
2
)
{
std::cerr <<
"
Usage: pretty <filename>
"
<< std::endl;
return
EXIT_FAILURE
;
}
try
{
//
Parse the file as JSON
auto
const
jv =
parse_file
( argv[
1
] );
//
Now pretty-print the value
pretty_print
(std::cout, jv);
}
catch
(std::exception
const
& e)
{
std::cerr <<
"
Caught exception:
"
<< e.
what
() << std::endl;
return
EXIT_FAILURE
;
}
return
EXIT_SUCCESS
;
}
//
end::example_pretty[]
Back
|
FazBrowse Home
|
New Git URL