]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/example/validate.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 verifies that a file contains valid JSON.
16 #include <boost/json.hpp>
18 // This file must be manually included when
19 // using basic_parser to implement a parser.
20 #include <boost/json/basic_parser_impl.hpp>
27 using namespace boost::json
;
29 // The null parser discards all the data
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);
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; }
59 basic_parser
<handler
> p_
;
77 auto const n
= p_
.write_some( false, data
, size
, ec
);
79 ec
= error::extra_data
;
85 validate( string_view s
)
87 // Parse with the null parser and return false on error
90 p
.write( s
.data(), s
.size(), ec
);
94 // The string is valid JSON.
99 main(int argc
, char** argv
)
104 "Usage: validate <filename>"
111 // Read the file into a string
112 auto const s
= read_file( argv
[1] );
114 // See if the string is valid JSON
115 auto const valid
= validate( s
);
119 std::cout
<< argv
[1] << " contains a valid JSON\n";
121 std::cout
<< argv
[1] << " does not contain a valid JSON\n";
123 catch(std::exception
const& e
)
127 << e
.what() << std::endl
;