|
||||
File indexing completed on 2025-01-18 09:50:18
0001 // ---------------------------------------------------------------------------- 0002 // Copyright (C) 2002-2006 Marcin Kalicinski 0003 // Copyright (C) 2015 Sebastian Redl 0004 // 0005 // Distributed under the Boost Software License, Version 1.0. 0006 // (See accompanying file LICENSE_1_0.txt or copy at 0007 // http://www.boost.org/LICENSE_1_0.txt) 0008 // 0009 // For more information, see www.boost.org 0010 // ---------------------------------------------------------------------------- 0011 #ifndef BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED 0012 #define BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED 0013 0014 #include <boost/property_tree/ptree.hpp> 0015 #include <boost/property_tree/json_parser/error.hpp> 0016 #include <boost/property_tree/json_parser/detail/read.hpp> 0017 #include <boost/property_tree/json_parser/detail/write.hpp> 0018 0019 #include <fstream> 0020 #include <string> 0021 #include <locale> 0022 0023 namespace boost { namespace property_tree { namespace json_parser 0024 { 0025 0026 /** 0027 * Read JSON from a the given stream and translate it to a property tree. 0028 * @note Clears existing contents of property tree. In case of error the 0029 * property tree unmodified. 0030 * @note Items of JSON arrays are translated into ptree keys with empty 0031 * names. Members of objects are translated into named keys. 0032 * @note JSON data can be a string, a numeric value, or one of literals 0033 * "null", "true" and "false". During parse, any of the above is 0034 * copied verbatim into ptree data string. 0035 * @throw json_parser_error In case of error deserializing the property 0036 * tree. 0037 * @param stream Stream from which to read in the property tree. 0038 * @param[out] pt The property tree to populate. 0039 */ 0040 template<class Ptree> 0041 void read_json(std::basic_istream< 0042 typename Ptree::key_type::value_type 0043 > &stream, 0044 Ptree &pt) 0045 { 0046 detail::read_json_internal(stream, pt, std::string()); 0047 } 0048 0049 /** 0050 * Read JSON from a the given file and translate it to a property tree. 0051 * @note Clears existing contents of property tree. In case of error the 0052 * property tree unmodified. 0053 * @note Items of JSON arrays are translated into ptree keys with empty 0054 * names. Members of objects are translated into named keys. 0055 * @note JSON data can be a string, a numeric value, or one of literals 0056 * "null", "true" and "false". During parse, any of the above is 0057 * copied verbatim into ptree data string. 0058 * @throw json_parser_error In case of error deserializing the property 0059 * tree. 0060 * @param filename Name of file from which to read in the property tree. 0061 * @param[out] pt The property tree to populate. 0062 * @param loc The locale to use when reading in the file contents. 0063 */ 0064 template<class Ptree> 0065 void read_json(const std::string &filename, 0066 Ptree &pt, 0067 const std::locale &loc = std::locale()) 0068 { 0069 std::basic_ifstream<typename Ptree::key_type::value_type> 0070 stream(filename.c_str()); 0071 if (!stream) 0072 BOOST_PROPERTY_TREE_THROW(json_parser_error( 0073 "cannot open file", filename, 0)); 0074 stream.imbue(loc); 0075 detail::read_json_internal(stream, pt, filename); 0076 } 0077 0078 /** 0079 * Translates the property tree to JSON and writes it the given output 0080 * stream. 0081 * @note Any property tree key containing only unnamed subkeys will be 0082 * rendered as JSON arrays. 0083 * @pre @e pt cannot contain keys that have both subkeys and non-empty data. 0084 * @throw json_parser_error In case of error translating the property tree 0085 * to JSON or writing to the output stream. 0086 * @param stream The stream to which to write the JSON representation of the 0087 * property tree. 0088 * @param pt The property tree to tranlsate to JSON and output. 0089 * @param pretty Whether to pretty-print. Defaults to true for backward 0090 * compatibility. 0091 */ 0092 template<class Ptree> 0093 void write_json(std::basic_ostream< 0094 typename Ptree::key_type::value_type 0095 > &stream, 0096 const Ptree &pt, 0097 bool pretty = true) 0098 { 0099 write_json_internal(stream, pt, std::string(), pretty); 0100 } 0101 0102 /** 0103 * Translates the property tree to JSON and writes it the given file. 0104 * @note Any property tree key containing only unnamed subkeys will be 0105 * rendered as JSON arrays. 0106 * @pre @e pt cannot contain keys that have both subkeys and non-empty data. 0107 * @throw json_parser_error In case of error translating the property tree 0108 * to JSON or writing to the file. 0109 * @param filename The name of the file to which to write the JSON 0110 * representation of the property tree. 0111 * @param pt The property tree to translate to JSON and output. 0112 * @param loc The locale to use when writing out to the output file. 0113 * @param pretty Whether to pretty-print. Defaults to true and last place 0114 * for backward compatibility. 0115 */ 0116 template<class Ptree> 0117 void write_json(const std::string &filename, 0118 const Ptree &pt, 0119 const std::locale &loc = std::locale(), 0120 bool pretty = true) 0121 { 0122 std::basic_ofstream<typename Ptree::key_type::value_type> 0123 stream(filename.c_str()); 0124 if (!stream) 0125 BOOST_PROPERTY_TREE_THROW(json_parser_error( 0126 "cannot open file", filename, 0)); 0127 stream.imbue(loc); 0128 write_json_internal(stream, pt, filename, pretty); 0129 } 0130 0131 } } } 0132 0133 namespace boost { namespace property_tree 0134 { 0135 using json_parser::read_json; 0136 using json_parser::write_json; 0137 using json_parser::json_parser_error; 0138 } } 0139 0140 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |