// // 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 citm_catalog.json // #include #if !defined(BOOST_DESCRIBE_CXX14) #include BOOST_PRAGMA_MESSAGE( "This example requires C++14" ) int main() {} #else #include #include #include #include #include #include #include #include struct event { std::nullptr_t description; unsigned long long id; boost::variant2::variant logo; std::string name; std::vector subTopicIds; std::nullptr_t subjectCode; std::nullptr_t subtitle; std::vector topicIds; }; BOOST_DESCRIBE_STRUCT(event, (), (description, id, logo, name, subTopicIds, subjectCode, subtitle, topicIds)) struct price { unsigned amount; unsigned long long audienceSubCategoryId; unsigned long long seatCategoryId; }; BOOST_DESCRIBE_STRUCT(price, (), (amount, audienceSubCategoryId, seatCategoryId)) struct area { unsigned long long areaId; std::vector blockIds; }; BOOST_DESCRIBE_STRUCT(area, (), (areaId, blockIds)) struct seatCategory { std::vector areas; unsigned long long seatCategoryId; }; BOOST_DESCRIBE_STRUCT(seatCategory, (), (areas, seatCategoryId)) struct performance { unsigned long long eventId; unsigned long long id; boost::variant2::variant logo; std::nullptr_t name; std::vector prices; std::vector seatCategories; std::nullptr_t seatMapImage; unsigned long long start; std::string venueCode; }; BOOST_DESCRIBE_STRUCT(performance, (), (eventId, id, logo, name, prices, seatCategories, seatMapImage, start, venueCode)) struct citm_catalog { std::map areaNames; std::map audienceSubCategoryNames; std::map blockNames; std::map events; std::vector performances; std::map seatCategoryNames; std::map subTopicNames; std::map subjectNames; std::map topicNames; std::map> topicSubTopics; std::map venueNames; }; BOOST_DESCRIBE_STRUCT(citm_catalog, (), (areaNames, audienceSubCategoryNames, blockNames, events, performances, seatCategoryNames, subTopicNames, subjectNames, topicNames, topicSubTopics, venueNames)) using namespace std::chrono_literals; int main() { std::ifstream is( "citm_catalog.json" ); std::string json( std::istreambuf_iterator( is ), std::istreambuf_iterator{} ); std::cout << "citm_catalog.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(); citm_catalog 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: " << (tp2 - tp1) / 1ms << " ms\n"; } } #endif