]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/example/validate.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / json / example / validate.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_validate
11
12 /*
13 This example verifies that a file contains valid JSON.
14 */
15
16 #include <boost/json.hpp>
17
18 // This file must be manually included when
19 // using basic_parser to implement a parser.
20 #include <boost/json/basic_parser_impl.hpp>
21
22 #include <iomanip>
23 #include <iostream>
24
25 #include "file.hpp"
26
27 using namespace boost::json;
28
29 // The null parser discards all the data
30 class null_parser
31 {
32 struct handler
33 {
34 constexpr static std::size_t max_object_size = std::size_t(-1);
35 constexpr static std::size_t max_array_size = std::size_t(-1);
36 constexpr static std::size_t max_key_size = std::size_t(-1);
37 constexpr static std::size_t max_string_size = std::size_t(-1);
38
39 bool on_document_begin( error_code& ) { return true; }
40 bool on_document_end( error_code& ) { return true; }
41 bool on_object_begin( error_code& ) { return true; }
42 bool on_object_end( std::size_t, error_code& ) { return true; }
43 bool on_array_begin( error_code& ) { return true; }
44 bool on_array_end( std::size_t, error_code& ) { return true; }
45 bool on_key_part( string_view, std::size_t, error_code& ) { return true; }
46 bool on_key( string_view, std::size_t, error_code& ) { return true; }
47 bool on_string_part( string_view, std::size_t, error_code& ) { return true; }
48 bool on_string( string_view, std::size_t, error_code& ) { return true; }
49 bool on_number_part( string_view, error_code& ) { return true; }
50 bool on_int64( std::int64_t, string_view, error_code& ) { return true; }
51 bool on_uint64( std::uint64_t, string_view, error_code& ) { return true; }
52 bool on_double( double, string_view, error_code& ) { return true; }
53 bool on_bool( bool, error_code& ) { return true; }
54 bool on_null( error_code& ) { return true; }
55 bool on_comment_part(string_view, error_code&) { return true; }
56 bool on_comment(string_view, error_code&) { return true; }
57 };
58
59 basic_parser<handler> p_;
60
61 public:
62 null_parser()
63 : p_(parse_options())
64 {
65 }
66
67 ~null_parser()
68 {
69 }
70
71 std::size_t
72 write(
73 char const* data,
74 std::size_t size,
75 error_code& ec)
76 {
77 auto const n = p_.write_some( false, data, size, ec );
78 if(! ec && n < size)
79 ec = error::extra_data;
80 return n;
81 }
82 };
83
84 bool
85 validate( string_view s )
86 {
87 // Parse with the null parser and return false on error
88 null_parser p;
89 error_code ec;
90 p.write( s.data(), s.size(), ec );
91 if( ec )
92 return false;
93
94 // The string is valid JSON.
95 return true;
96 }
97
98 int
99 main(int argc, char** argv)
100 {
101 if(argc != 2)
102 {
103 std::cerr <<
104 "Usage: validate <filename>"
105 << std::endl;
106 return EXIT_FAILURE;
107 }
108
109 try
110 {
111 // Read the file into a string
112 auto const s = read_file( argv[1] );
113
114 // See if the string is valid JSON
115 auto const valid = validate( s );
116
117 // Print the result
118 if( valid )
119 std::cout << argv[1] << " contains a valid JSON\n";
120 else
121 std::cout << argv[1] << " does not contain a valid JSON\n";
122 }
123 catch(std::exception const& e)
124 {
125 std::cerr <<
126 "Caught exception: "
127 << e.what() << std::endl;
128 return EXIT_FAILURE;
129 }
130
131 return EXIT_SUCCESS;
132 }
133
134 //]