1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // For more information, see www.boost.org
9 // ----------------------------------------------------------------------------
10 #ifndef BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED
11 #define BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED
13 #include <boost/property_tree/ptree.hpp>
14 #include <boost/property_tree/detail/json_parser_read.hpp>
15 #include <boost/property_tree/detail/json_parser_write.hpp>
16 #include <boost/property_tree/detail/json_parser_error.hpp>
22 namespace boost
{ namespace property_tree
{ namespace json_parser
26 * Read JSON from a the given stream and translate it to a property tree.
27 * @note Clears existing contents of property tree. In case of error the
28 * property tree unmodified.
29 * @note Items of JSON arrays are translated into ptree keys with empty
30 * names. Members of objects are translated into named keys.
31 * @note JSON data can be a string, a numeric value, or one of literals
32 * "null", "true" and "false". During parse, any of the above is copied
33 * verbatim into ptree data string.
34 * @throw json_parser_error In case of error deserializing the property tree.
35 * @param stream Stream from which to read in the property tree.
36 * @param[out] pt The property tree to populate.
39 void read_json(std::basic_istream
<typename
Ptree::key_type::value_type
> &stream
,
42 read_json_internal(stream
, pt
, std::string());
46 * Read JSON from a the given file and translate it to a property tree.
47 * @note Clears existing contents of property tree. In case of error the
48 * property tree unmodified.
49 * @note Items of JSON arrays are translated into ptree keys with empty
50 * names. Members of objects are translated into named keys.
51 * @note JSON data can be a string, a numeric value, or one of literals
52 * "null", "true" and "false". During parse, any of the above is copied
53 * verbatim into ptree data string.
54 * @throw json_parser_error In case of error deserializing the property tree.
55 * @param filename Name of file from which to read in the property tree.
56 * @param[out] pt The property tree to populate.
57 * @param loc The locale to use when reading in the file contents.
60 void read_json(const std::string
&filename
,
62 const std::locale
&loc
= std::locale())
64 std::basic_ifstream
<typename
Ptree::key_type::value_type
> stream(filename
.c_str());
66 BOOST_PROPERTY_TREE_THROW(json_parser_error("cannot open file", filename
, 0));
68 read_json_internal(stream
, pt
, filename
);
72 * Translates the property tree to JSON and writes it the given output stream.
73 * @note Any property tree key containing only unnamed subkeys will be rendered
75 * @pre @e pt cannot contain keys that have both subkeys and non-empty data.
76 * @throw json_parser_error In case of error translating the property tree to JSON
77 * or writing to the output stream.
78 * @param stream The stream to which to write the JSON representation of the
80 * @param pt The property tree to tranlsate to JSON and output.
83 void write_json(std::basic_ostream
<typename
Ptree::key_type::value_type
> &stream
,
86 write_json_internal(stream
, pt
, std::string());
90 * Translates the property tree to JSON and writes it the given file.
91 * @note Any property tree key containing only unnamed subkeys will be rendered
93 * @pre @e pt cannot contain keys that have both subkeys and non-empty data.
94 * @throw json_parser_error In case of error translating the property tree to JSON
95 * or writing to the file.
96 * @param filename The name of the file to which to write the JSON representation
97 * of the property tree.
98 * @param pt The property tree to tranlsate to JSON and output.
99 * @param loc The locale to use when writing out to the output file.
101 template<class Ptree
>
102 void write_json(const std::string
&filename
,
104 const std::locale
&loc
= std::locale())
106 std::basic_ofstream
<typename
Ptree::key_type::value_type
> stream(filename
.c_str());
108 BOOST_PROPERTY_TREE_THROW(json_parser_error("cannot open file", filename
, 0));
110 write_json_internal(stream
, pt
, filename
);
115 namespace boost
{ namespace property_tree
117 using json_parser::read_json
;
118 using json_parser::write_json
;
119 using json_parser::json_parser_error
;