1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
3 // Copyright (C) 2015 Sebastian Redl
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // For more information, see www.boost.org
10 // ----------------------------------------------------------------------------
11 #ifndef BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED
12 #define BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED
14 #include <boost/property_tree/ptree.hpp>
15 #include <boost/property_tree/json_parser/error.hpp>
16 #include <boost/property_tree/json_parser/detail/read.hpp>
17 #include <boost/property_tree/json_parser/detail/write.hpp>
23 namespace boost { namespace property_tree { namespace json_parser
27 * Read JSON from a the given stream and translate it to a property tree.
28 * @note Clears existing contents of property tree. In case of error the
29 * property tree unmodified.
30 * @note Items of JSON arrays are translated into ptree keys with empty
31 * names. Members of objects are translated into named keys.
32 * @note JSON data can be a string, a numeric value, or one of literals
33 * "null", "true" and "false". During parse, any of the above is
34 * copied verbatim into ptree data string.
35 * @throw json_parser_error In case of error deserializing the property
37 * @param stream Stream from which to read in the property tree.
38 * @param[out] pt The property tree to populate.
41 void read_json(std::basic_istream<
42 typename Ptree::key_type::value_type
46 detail::read_json_internal(stream, pt, std::string());
50 * Read JSON from a the given file and translate it to a property tree.
51 * @note Clears existing contents of property tree. In case of error the
52 * property tree unmodified.
53 * @note Items of JSON arrays are translated into ptree keys with empty
54 * names. Members of objects are translated into named keys.
55 * @note JSON data can be a string, a numeric value, or one of literals
56 * "null", "true" and "false". During parse, any of the above is
57 * copied verbatim into ptree data string.
58 * @throw json_parser_error In case of error deserializing the property
60 * @param filename Name of file from which to read in the property tree.
61 * @param[out] pt The property tree to populate.
62 * @param loc The locale to use when reading in the file contents.
65 void read_json(const std::string &filename,
67 const std::locale &loc = std::locale())
69 std::basic_ifstream<typename Ptree::key_type::value_type>
70 stream(filename.c_str());
72 BOOST_PROPERTY_TREE_THROW(json_parser_error(
73 "cannot open file", filename, 0));
75 detail::read_json_internal(stream, pt, filename);
79 * Translates the property tree to JSON and writes it the given output
81 * @note Any property tree key containing only unnamed subkeys will be
82 * rendered as JSON arrays.
83 * @pre @e pt cannot contain keys that have both subkeys and non-empty data.
84 * @throw json_parser_error In case of error translating the property tree
85 * to JSON or writing to the output stream.
86 * @param stream The stream to which to write the JSON representation of the
88 * @param pt The property tree to tranlsate to JSON and output.
89 * @param pretty Whether to pretty-print. Defaults to true for backward
93 void write_json(std::basic_ostream<
94 typename Ptree::key_type::value_type
99 write_json_internal(stream, pt, std::string(), pretty);
103 * Translates the property tree to JSON and writes it the given file.
104 * @note Any property tree key containing only unnamed subkeys will be
105 * rendered as JSON arrays.
106 * @pre @e pt cannot contain keys that have both subkeys and non-empty data.
107 * @throw json_parser_error In case of error translating the property tree
108 * to JSON or writing to the file.
109 * @param filename The name of the file to which to write the JSON
110 * representation of the property tree.
111 * @param pt The property tree to translate to JSON and output.
112 * @param loc The locale to use when writing out to the output file.
113 * @param pretty Whether to pretty-print. Defaults to true and last place
114 * for backward compatibility.
116 template<class Ptree>
117 void write_json(const std::string &filename,
119 const std::locale &loc = std::locale(),
122 std::basic_ofstream<typename Ptree::key_type::value_type>
123 stream(filename.c_str());
125 BOOST_PROPERTY_TREE_THROW(json_parser_error(
126 "cannot open file", filename, 0));
128 write_json_internal(stream, pt, filename, pretty);
133 namespace boost { namespace property_tree
135 using json_parser::read_json;
136 using json_parser::write_json;
137 using json_parser::json_parser_error;