...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
/* 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; }
/* This example verifies that a file contains valid JSON. */ #include <boost/json.hpp> // This file must be manually included when // using basic_parser to implement a parser. #include <boost/json/basic_parser_impl.hpp> #include <iomanip> #include <iostream> #include "file.hpp" using namespace boost::json; // The null parser discards all the data class null_parser { struct handler { constexpr static std::size_t max_object_size = std::size_t(-1); constexpr static std::size_t max_array_size = std::size_t(-1); constexpr static std::size_t max_key_size = std::size_t(-1); constexpr static std::size_t max_string_size = std::size_t(-1); bool on_document_begin( boost::system::error_code& ) { return true; } bool on_document_end( boost::system::error_code& ) { return true; } bool on_object_begin( boost::system::error_code& ) { return true; } bool on_object_end( std::size_t, boost::system::error_code& ) { return true; } bool on_array_begin( boost::system::error_code& ) { return true; } bool on_array_end( std::size_t, boost::system::error_code& ) { return true; } bool on_key_part( string_view, std::size_t, boost::system::error_code& ) { return true; } bool on_key( string_view, std::size_t, boost::system::error_code& ) { return true; } bool on_string_part( string_view, std::size_t, boost::system::error_code& ) { return true; } bool on_string( string_view, std::size_t, boost::system::error_code& ) { return true; } bool on_number_part( string_view, boost::system::error_code& ) { return true; } bool on_int64( std::int64_t, string_view, boost::system::error_code& ) { return true; } bool on_uint64( std::uint64_t, string_view, boost::system::error_code& ) { return true; } bool on_double( double, string_view, boost::system::error_code& ) { return true; } bool on_bool( bool, boost::system::error_code& ) { return true; } bool on_null( boost::system::error_code& ) { return true; } bool on_comment_part(string_view, boost::system::error_code&) { return true; } bool on_comment(string_view, boost::system::error_code&) { return true; } }; basic_parser<handler> p_; public: null_parser() : p_(parse_options()) { } ~null_parser() { } std::size_t write( char const* data, std::size_t size, boost::system::error_code& ec) { auto const n = p_.write_some( false, data, size, ec ); if(! ec && n < size) ec = error::extra_data; return n; } }; bool validate( string_view s ) { // Parse with the null parser and return false on error null_parser p; boost::system::error_code ec; p.write( s.data(), s.size(), ec ); if( ec ) return false; // The string is valid JSON. return true; } int main(int argc, char** argv) { if(argc != 2) { std::cerr << "Usage: validate <filename>" << std::endl; return EXIT_FAILURE; } try { // Read the file into a string auto const s = read_file( argv[1] ); // See if the string is valid JSON auto const valid = validate( s ); // Print the result if( valid ) std::cout << argv[1] << " contains a valid JSON\n"; else std::cout << argv[1] << " does not contain a valid JSON\n"; } catch(std::exception const& e) { std::cerr << "Caught exception: " << e.what() << std::endl; return EXIT_FAILURE; } return EXIT_SUCCESS; }
/* This example uses a context that stores an allocator to create sequences during conversions */ #include <boost/container/pmr/monotonic_buffer_resource.hpp> #include <boost/container/pmr/vector.hpp> #include <boost/json.hpp> #include <boost/system/errc.hpp> using namespace boost::json; using namespace boost::container; template< class Alloc > struct use_allocator_t { Alloc allocator; }; template< class Alloc > use_allocator_t< Alloc > use_allocator( Alloc alloc ) noexcept { return { alloc }; } template< class T, class Alloc, class FullContext, class = typename std::enable_if< is_sequence_like<T>::value && std::uses_allocator<T, Alloc>::value >::type > boost::system::result<T> tag_invoke( try_value_to_tag<T>, const value& jv, const use_allocator_t<Alloc>& ctx, const FullContext& full_ctx ) { array const* arr = jv.if_array(); if( !arr ) return { boost::system::in_place_error, make_error_code(boost::system::errc::invalid_argument) }; T result(ctx.allocator); auto ins = std::inserter(result, result.end()); for( value const& val: *arr ) { using ValueType = typename T::value_type; auto elem_res = try_value_to<ValueType>( val, full_ctx ); if( elem_res.has_error() ) return {boost::system::in_place_error, elem_res.error()}; *ins++ = std::move(*elem_res); } return result; } int main(int, char**) { value const jv = { 1, 2, 3, 4, 5, 6, 7, 8 }; unsigned char buf[1024]; pmr::monotonic_buffer_resource mr( buf, sizeof(buf) ); auto v = value_to< pmr::vector<int> >( jv, use_allocator( pmr::polymorphic_allocator<int>(&mr) ) ); assert( v.size() == jv.as_array().size() ); for( auto i = 0u; i < v.size(); ++i ) assert( v[i] == jv.at(i).to_number<int>() ); return EXIT_SUCCESS; }