summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/json/example
diff options
context:
space:
mode:
Diffstat (limited to 'src/boost/libs/json/example')
-rw-r--r--src/boost/libs/json/example/CMakeLists.txt51
-rw-r--r--src/boost/libs/json/example/Jamfile18
-rw-r--r--src/boost/libs/json/example/file.hpp167
-rw-r--r--src/boost/libs/json/example/path.cpp51
-rw-r--r--src/boost/libs/json/example/pretty.cpp164
-rw-r--r--src/boost/libs/json/example/proxy.cpp63
-rw-r--r--src/boost/libs/json/example/validate.cpp134
7 files changed, 648 insertions, 0 deletions
diff --git a/src/boost/libs/json/example/CMakeLists.txt b/src/boost/libs/json/example/CMakeLists.txt
new file mode 100644
index 000000000..7fa2bb6c9
--- /dev/null
+++ b/src/boost/libs/json/example/CMakeLists.txt
@@ -0,0 +1,51 @@
+#
+# Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
+#
+# Distributed under the Boost Software License, Version 1.0. (See accompanying
+# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+#
+# Official repository: https://github.com/boostorg/json
+#
+
+source_group("" FILES
+ file.hpp
+ path.cpp
+ pretty.cpp
+ proxy.cpp
+ validate.cpp
+)
+
+#
+
+add_executable(path
+ path.cpp
+)
+set_property(TARGET path PROPERTY FOLDER "example")
+target_link_libraries(path PRIVATE Boost::json)
+
+#
+
+add_executable(pretty
+ file.hpp
+ pretty.cpp
+)
+set_property(TARGET pretty PROPERTY FOLDER "example")
+target_link_libraries(pretty PRIVATE Boost::json)
+
+#
+
+add_executable(proxy
+ file.hpp
+ proxy.cpp
+)
+set_property(TARGET proxy PROPERTY FOLDER "example")
+target_link_libraries(proxy PRIVATE Boost::json)
+
+#
+
+add_executable(validate
+ file.hpp
+ validate.cpp
+)
+set_property(TARGET validate PROPERTY FOLDER "example")
+target_link_libraries(validate PRIVATE Boost::json)
diff --git a/src/boost/libs/json/example/Jamfile b/src/boost/libs/json/example/Jamfile
new file mode 100644
index 000000000..41fd9efdb
--- /dev/null
+++ b/src/boost/libs/json/example/Jamfile
@@ -0,0 +1,18 @@
+#
+# Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
+#
+# Distributed under the Boost Software License, Version 1.0. (See accompanying
+# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+#
+# Official repository: https://github.com/boostorg/json
+#
+
+project : requirements <library>/boost/json//boost_json ;
+
+exe path : path.cpp ;
+
+exe pretty : pretty.cpp ;
+
+exe proxy : proxy.cpp ;
+
+exe validate : validate.cpp ;
diff --git a/src/boost/libs/json/example/file.hpp b/src/boost/libs/json/example/file.hpp
new file mode 100644
index 000000000..eb41dd925
--- /dev/null
+++ b/src/boost/libs/json/example/file.hpp
@@ -0,0 +1,167 @@
+//
+// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// Official repository: https://github.com/boostorg/json
+//
+
+#ifndef BOOST_JSON_EXAMPLE_FILE_HPP
+#define BOOST_JSON_EXAMPLE_FILE_HPP
+
+#include <boost/json/detail/config.hpp>
+#include <cstdio>
+#include <string>
+
+class file
+{
+ FILE* f_ = nullptr;
+ long size_ = 0;
+
+ void
+ fail(boost::json::error_code& ec)
+ {
+ ec.assign( errno, boost::json::generic_category() );
+ }
+
+public:
+ ~file()
+ {
+ if(f_)
+ std::fclose(f_);
+ }
+
+ file() = default;
+
+ file( file&& other ) noexcept
+ : f_(other.f_)
+ {
+ other.f_ = nullptr;
+ }
+
+ file( char const* path, char const* mode )
+ {
+ open( path, mode );
+ }
+
+ file&
+ operator=(file&& other) noexcept
+ {
+ close();
+ f_ = other.f_;
+ other.f_ = nullptr;
+ return *this;
+ }
+
+ void
+ close()
+ {
+ if(f_)
+ {
+ std::fclose(f_);
+ f_ = nullptr;
+ size_ = 0;
+ }
+ }
+
+ void
+ open(
+ char const* path,
+ char const* mode,
+ boost::json::error_code& ec)
+ {
+ close();
+#if defined(_MSC_VER)
+# pragma warning( push )
+# pragma warning( disable : 4996 )
+#endif
+ f_ = std::fopen( path, mode );
+#if defined(_MSC_VER)
+# pragma warning( pop )
+#endif
+ if( ! f_ )
+ return fail(ec);
+ if( std::fseek( f_, 0, SEEK_END ) != 0)
+ return fail(ec);
+ size_ = std::ftell( f_ );
+ if( size_ == -1 )
+ {
+ size_ = 0;
+ return fail(ec);
+ }
+ if( std::fseek( f_, 0, SEEK_SET ) != 0)
+ return fail(ec);
+ }
+
+ void
+ open(
+ char const* path,
+ char const* mode)
+ {
+ boost::json::error_code ec;
+ open(path, mode, ec);
+ if(ec)
+ throw boost::json::system_error(ec);
+ }
+
+ long
+ size() const noexcept
+ {
+ return size_;
+ }
+
+ bool
+ eof() const noexcept
+ {
+ return std::feof( f_ ) != 0;
+ }
+
+ std::size_t
+ read( char* data, std::size_t size, boost::json::error_code& ec)
+ {
+ auto const nread = std::fread( data, 1, size, f_ );
+ if( std::ferror(f_) )
+ ec.assign( errno, boost::json::generic_category() );
+ return nread;
+ }
+
+ std::size_t
+ read( char* data, std::size_t size )
+ {
+ boost::json::error_code ec;
+ auto const nread = read( data, size, ec );
+ if(ec)
+ throw boost::json::system_error(ec);
+ return nread;
+ }
+};
+
+inline
+std::string
+read_file( char const* path, boost::json::error_code& ec )
+{
+ file f;
+ f.open( path, "r", ec );
+ if(ec)
+ return {};
+ std::string s;
+ s.resize( f.size() );
+ s.resize( f.read( &s[0], s.size(), ec) );
+ if(ec)
+ return {};
+ return s;
+}
+
+inline
+std::string
+read_file( char const* path )
+{
+ boost::json::error_code ec;
+ auto s = read_file( path, ec);
+ if(ec)
+ throw boost::json::system_error(ec);
+ return s;
+}
+
+#endif
diff --git a/src/boost/libs/json/example/path.cpp b/src/boost/libs/json/example/path.cpp
new file mode 100644
index 000000000..d6fe937bb
--- /dev/null
+++ b/src/boost/libs/json/example/path.cpp
@@ -0,0 +1,51 @@
+//
+// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// Official repository: https://github.com/boostorg/json
+//
+
+#include <boost/json.hpp>
+
+using namespace boost::json;
+
+value&
+path( value& jv, std::size_t index)
+{
+ array *arr;
+ if(jv.is_null())
+ arr = &jv.emplace_array();
+ else
+ arr = &jv.as_array();
+ if(arr->size() <= index)
+ arr->resize(index + 1);
+ return (*arr)[index];
+}
+
+value&
+path( value& jv, string_view key )
+{
+ object* obj;
+ if(jv.is_null())
+ obj = &jv.emplace_object();
+ else
+ obj = &jv.as_object();
+ return (*obj)[key];
+}
+
+template<class Arg0, class Arg1, class... Args>
+value&
+path( value& jv, Arg0 const& arg0, Arg1 const& arg1, Args const&... args )
+{
+ return path( path( jv, arg0 ), arg1, args... );
+}
+
+int
+main(int, char**)
+{
+ value jv;
+ path( jv, "user:config", "authority", "router", 0, "users" ) = 42;
+ return EXIT_SUCCESS;
+}
diff --git a/src/boost/libs/json/example/pretty.cpp b/src/boost/libs/json/example/pretty.cpp
new file mode 100644
index 000000000..18bd2f71b
--- /dev/null
+++ b/src/boost/libs/json/example/pretty.cpp
@@ -0,0 +1,164 @@
+//
+// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// Official repository: https://github.com/boostorg/json
+//
+
+//[example_pretty
+
+/*
+ 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;
+ json::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:
+ os << jv.get_uint64();
+ break;
+
+ case json::kind::int64:
+ os << jv.get_int64();
+ break;
+
+ case json::kind::double_:
+ os << jv.get_double();
+ 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;
+}
+
+//]
diff --git a/src/boost/libs/json/example/proxy.cpp b/src/boost/libs/json/example/proxy.cpp
new file mode 100644
index 000000000..646df1bb0
--- /dev/null
+++ b/src/boost/libs/json/example/proxy.cpp
@@ -0,0 +1,63 @@
+//
+// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// Official repository: https://github.com/boostorg/json
+//
+
+#include <boost/json.hpp>
+
+using namespace boost::json;
+
+class proxy
+{
+ value& jv_;
+
+public:
+ explicit
+ proxy(value& jv) noexcept
+ : jv_(jv)
+ {
+ }
+
+ proxy
+ operator[](string_view key)
+ {
+ object* obj;
+ if(jv_.is_null())
+ obj = &jv_.emplace_object();
+ else
+ obj = &jv_.as_object();
+ return proxy((*obj)[key]);
+ }
+
+ proxy
+ operator[](std::size_t index)
+ {
+ array *arr;
+ if(jv_.is_null())
+ arr = &jv_.emplace_array();
+ else
+ arr = &jv_.as_array();
+ if(arr->size() <= index)
+ arr->resize(index + 1);
+ return proxy((*arr)[index]);
+ }
+
+ value&
+ operator*() noexcept
+ {
+ return jv_;
+ }
+};
+
+
+int
+main(int, char**)
+{
+ value jv;
+ *proxy(jv)["user:config"]["authority"]["router"][0]["users"] = 42;
+ return EXIT_SUCCESS;
+}
diff --git a/src/boost/libs/json/example/validate.cpp b/src/boost/libs/json/example/validate.cpp
new file mode 100644
index 000000000..545f79361
--- /dev/null
+++ b/src/boost/libs/json/example/validate.cpp
@@ -0,0 +1,134 @@
+//
+// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// Official repository: https://github.com/boostorg/json
+//
+
+//[example_validate
+
+/*
+ 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( error_code& ) { return true; }
+ bool on_document_end( error_code& ) { return true; }
+ bool on_object_begin( error_code& ) { return true; }
+ bool on_object_end( std::size_t, error_code& ) { return true; }
+ bool on_array_begin( error_code& ) { return true; }
+ bool on_array_end( std::size_t, error_code& ) { return true; }
+ bool on_key_part( string_view, std::size_t, error_code& ) { return true; }
+ bool on_key( string_view, std::size_t, error_code& ) { return true; }
+ bool on_string_part( string_view, std::size_t, error_code& ) { return true; }
+ bool on_string( string_view, std::size_t, error_code& ) { return true; }
+ bool on_number_part( string_view, error_code& ) { return true; }
+ bool on_int64( std::int64_t, string_view, error_code& ) { return true; }
+ bool on_uint64( std::uint64_t, string_view, error_code& ) { return true; }
+ bool on_double( double, string_view, error_code& ) { return true; }
+ bool on_bool( bool, error_code& ) { return true; }
+ bool on_null( error_code& ) { return true; }
+ bool on_comment_part(string_view, error_code&) { return true; }
+ bool on_comment(string_view, 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,
+ 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;
+ 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;
+}
+
+//]