]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/example/pretty.cpp
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Official repository: https://github.com/boostorg/json
13 This example parses a JSON file and pretty-prints
14 it to standard output.
17 #include <boost/json.hpp>
23 namespace json
= boost::json
;
26 parse_file( char const* filename
)
28 file
f( filename
, "r" );
29 json::stream_parser p
;
34 auto const nread
= f
.read( buf
, sizeof(buf
) );
35 p
.write( buf
, nread
, ec
);
47 pretty_print( std::ostream
& os
, json::value
const& jv
, std::string
* indent
= nullptr )
54 case json::kind::object
:
57 indent
->append(4, ' ');
58 auto const& obj
= jv
.get_object();
61 auto it
= obj
.begin();
64 os
<< *indent
<< json::serialize(it
->key()) << " : ";
65 pretty_print(os
, it
->value(), indent
);
72 indent
->resize(indent
->size() - 4);
77 case json::kind::array
:
80 indent
->append(4, ' ');
81 auto const& arr
= jv
.get_array();
84 auto it
= arr
.begin();
88 pretty_print( os
, *it
, indent
);
95 indent
->resize(indent
->size() - 4);
100 case json::kind::string
:
102 os
<< json::serialize(jv
.get_string());
106 case json::kind::uint64
:
107 os
<< jv
.get_uint64();
110 case json::kind::int64
:
111 os
<< jv
.get_int64();
114 case json::kind::double_
:
115 os
<< jv
.get_double();
118 case json::kind::bool_
:
125 case json::kind::null
:
135 main(int argc
, char** argv
)
140 "Usage: pretty <filename>"
147 // Parse the file as JSON
148 auto const jv
= parse_file( argv
[1] );
150 // Now pretty-print the value
151 pretty_print(std::cout
, jv
);
153 catch(std::exception
const& e
)
157 << e
.what() << std::endl
;