]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/example/pretty.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / json / example / pretty.cpp
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 //
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)
6 //
7 // Official repository: https://github.com/boostorg/json
8 //
9
10 //[example_pretty
11
12 /*
13 This example parses a JSON file and pretty-prints
14 it to standard output.
15 */
16
17 #include <boost/json.hpp>
18 #include <iomanip>
19 #include <iostream>
20
21 #include "file.hpp"
22
23 namespace json = boost::json;
24
25 json::value
26 parse_file( char const* filename )
27 {
28 file f( filename, "r" );
29 json::stream_parser p;
30 json::error_code ec;
31 do
32 {
33 char buf[4096];
34 auto const nread = f.read( buf, sizeof(buf) );
35 p.write( buf, nread, ec );
36 }
37 while( ! f.eof() );
38 if( ec )
39 return nullptr;
40 p.finish( ec );
41 if( ec )
42 return nullptr;
43 return p.release();
44 }
45
46 void
47 pretty_print( std::ostream& os, json::value const& jv, std::string* indent = nullptr )
48 {
49 std::string indent_;
50 if(! indent)
51 indent = &indent_;
52 switch(jv.kind())
53 {
54 case json::kind::object:
55 {
56 os << "{\n";
57 indent->append(4, ' ');
58 auto const& obj = jv.get_object();
59 if(! obj.empty())
60 {
61 auto it = obj.begin();
62 for(;;)
63 {
64 os << *indent << json::serialize(it->key()) << " : ";
65 pretty_print(os, it->value(), indent);
66 if(++it == obj.end())
67 break;
68 os << ",\n";
69 }
70 }
71 os << "\n";
72 indent->resize(indent->size() - 4);
73 os << *indent << "}";
74 break;
75 }
76
77 case json::kind::array:
78 {
79 os << "[\n";
80 indent->append(4, ' ');
81 auto const& arr = jv.get_array();
82 if(! arr.empty())
83 {
84 auto it = arr.begin();
85 for(;;)
86 {
87 os << *indent;
88 pretty_print( os, *it, indent);
89 if(++it == arr.end())
90 break;
91 os << ",\n";
92 }
93 }
94 os << "\n";
95 indent->resize(indent->size() - 4);
96 os << *indent << "]";
97 break;
98 }
99
100 case json::kind::string:
101 {
102 os << json::serialize(jv.get_string());
103 break;
104 }
105
106 case json::kind::uint64:
107 os << jv.get_uint64();
108 break;
109
110 case json::kind::int64:
111 os << jv.get_int64();
112 break;
113
114 case json::kind::double_:
115 os << jv.get_double();
116 break;
117
118 case json::kind::bool_:
119 if(jv.get_bool())
120 os << "true";
121 else
122 os << "false";
123 break;
124
125 case json::kind::null:
126 os << "null";
127 break;
128 }
129
130 if(indent->empty())
131 os << "\n";
132 }
133
134 int
135 main(int argc, char** argv)
136 {
137 if(argc != 2)
138 {
139 std::cerr <<
140 "Usage: pretty <filename>"
141 << std::endl;
142 return EXIT_FAILURE;
143 }
144
145 try
146 {
147 // Parse the file as JSON
148 auto const jv = parse_file( argv[1] );
149
150 // Now pretty-print the value
151 pretty_print(std::cout, jv);
152 }
153 catch(std::exception const& e)
154 {
155 std::cerr <<
156 "Caught exception: "
157 << e.what() << std::endl;
158 return EXIT_FAILURE;
159 }
160
161 return EXIT_SUCCESS;
162 }
163
164 //]