Add example/parse_into_canada.cpp

This commit is contained in:
Peter Dimov 2021-10-06 21:10:32 +03:00 committed by Dmitry Arkhipov
parent 7761788417
commit 76c234b877
3 changed files with 107 additions and 0 deletions

View File

@ -10,6 +10,7 @@
source_group("" FILES
file.hpp
parse_into.cpp
parse_into_canada.cpp
path.cpp
pretty.cpp
proxy.cpp
@ -24,6 +25,12 @@ add_executable(parse_into
set_property(TARGET parse_into PROPERTY FOLDER "example")
target_link_libraries(parse_into PRIVATE Boost::json)
add_executable(parse_into_canada
parse_into_canada.cpp
)
set_property(TARGET parse_into_canada PROPERTY FOLDER "example")
target_link_libraries(parse_into_canada PRIVATE Boost::json)
#
add_executable(path

View File

@ -13,6 +13,8 @@ exe path : path.cpp ;
exe parse_into : parse_into.cpp ;
exe parse_into_canada : parse_into_canada.cpp ;
exe pretty : pretty.cpp ;
exe proxy : proxy.cpp ;

View File

@ -0,0 +1,98 @@
//
// Copyright (c) 2021 Peter Dimov
//
// 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
//
//
// An example that compares the performance of json::parse and
// json::parse_into on canada.json
//
#include <boost/json.hpp>
#if BOOST_CXX_VERSION < 201400L || ( defined(BOOST_MSVC) && BOOST_MSVC < 1910 )
#include <boost/config/pragma_message.hpp>
BOOST_PRAGMA_MESSAGE( "This example requires C++14 or VS2017 or later" )
int main() {}
#else
#include <boost/describe.hpp>
#include <chrono>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <utility>
#include <vector>
struct geometry_type
{
std::string type;
std::vector< std::vector<std::pair<double, double>> > coordinates;
};
BOOST_DESCRIBE_STRUCT(geometry_type, (), (type, coordinates))
struct feature
{
std::string type;
std::map<std::string, std::string> properties;
geometry_type geometry;
};
BOOST_DESCRIBE_STRUCT(feature, (), (type, properties, geometry))
struct canada
{
std::string type;
std::vector<feature> features;
};
BOOST_DESCRIBE_STRUCT(canada, (), (type, features))
using namespace std::chrono_literals;
int main()
{
std::ifstream is( "canada.json" );
std::string json( std::istreambuf_iterator<char>( is ), std::istreambuf_iterator<char>{} );
std::cout << "canada.json: " << json.size() << " bytes\n";
{
auto tp1 = std::chrono::steady_clock::now();
boost::json::value jv = boost::json::parse( json );
auto tp2 = std::chrono::steady_clock::now();
std::cout << "boost::json::parse: " << (tp2 - tp1) / 1ms << " ms\n";
}
{
auto tp1 = std::chrono::steady_clock::now();
canada w;
boost::json::error_code ec;
boost::json::parse_into( w, json, ec );
if( ec.failed() )
{
std::cout << "Error: " << ec.what() << std::endl;
}
auto tp2 = std::chrono::steady_clock::now();
std::cout << "parse_into<canada>: " << (tp2 - tp1) / 1ms << " ms\n";
}
}
#endif