Chris@16
|
1 // ----------------------------------------------------------------------------
|
Chris@16
|
2 // Copyright (C) 2002-2006 Marcin Kalicinski
|
Chris@16
|
3 //
|
Chris@16
|
4 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
5 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 //
|
Chris@16
|
8 // For more information, see www.boost.org
|
Chris@16
|
9 // ----------------------------------------------------------------------------
|
Chris@16
|
10 #ifndef BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED
|
Chris@16
|
11 #define BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/property_tree/ptree.hpp>
|
Chris@16
|
14 #include <boost/property_tree/detail/json_parser_read.hpp>
|
Chris@16
|
15 #include <boost/property_tree/detail/json_parser_write.hpp>
|
Chris@16
|
16 #include <boost/property_tree/detail/json_parser_error.hpp>
|
Chris@16
|
17
|
Chris@16
|
18 #include <fstream>
|
Chris@16
|
19 #include <string>
|
Chris@16
|
20 #include <locale>
|
Chris@16
|
21
|
Chris@16
|
22 namespace boost { namespace property_tree { namespace json_parser
|
Chris@16
|
23 {
|
Chris@16
|
24
|
Chris@16
|
25 /**
|
Chris@16
|
26 * Read JSON from a the given stream and translate it to a property tree.
|
Chris@16
|
27 * @note Clears existing contents of property tree. In case of error the
|
Chris@16
|
28 * property tree unmodified.
|
Chris@16
|
29 * @note Items of JSON arrays are translated into ptree keys with empty
|
Chris@16
|
30 * names. Members of objects are translated into named keys.
|
Chris@16
|
31 * @note JSON data can be a string, a numeric value, or one of literals
|
Chris@16
|
32 * "null", "true" and "false". During parse, any of the above is
|
Chris@16
|
33 * copied verbatim into ptree data string.
|
Chris@16
|
34 * @throw json_parser_error In case of error deserializing the property
|
Chris@16
|
35 * tree.
|
Chris@16
|
36 * @param stream Stream from which to read in the property tree.
|
Chris@16
|
37 * @param[out] pt The property tree to populate.
|
Chris@16
|
38 */
|
Chris@16
|
39 template<class Ptree>
|
Chris@16
|
40 void read_json(std::basic_istream<
|
Chris@16
|
41 typename Ptree::key_type::value_type
|
Chris@16
|
42 > &stream,
|
Chris@16
|
43 Ptree &pt)
|
Chris@16
|
44 {
|
Chris@16
|
45 read_json_internal(stream, pt, std::string());
|
Chris@16
|
46 }
|
Chris@16
|
47
|
Chris@16
|
48 /**
|
Chris@16
|
49 * Read JSON from a the given file and translate it to a property tree.
|
Chris@16
|
50 * @note Clears existing contents of property tree. In case of error the
|
Chris@16
|
51 * property tree unmodified.
|
Chris@16
|
52 * @note Items of JSON arrays are translated into ptree keys with empty
|
Chris@16
|
53 * names. Members of objects are translated into named keys.
|
Chris@16
|
54 * @note JSON data can be a string, a numeric value, or one of literals
|
Chris@16
|
55 * "null", "true" and "false". During parse, any of the above is
|
Chris@16
|
56 * copied verbatim into ptree data string.
|
Chris@16
|
57 * @throw json_parser_error In case of error deserializing the property
|
Chris@16
|
58 * tree.
|
Chris@16
|
59 * @param filename Name of file from which to read in the property tree.
|
Chris@16
|
60 * @param[out] pt The property tree to populate.
|
Chris@16
|
61 * @param loc The locale to use when reading in the file contents.
|
Chris@16
|
62 */
|
Chris@16
|
63 template<class Ptree>
|
Chris@16
|
64 void read_json(const std::string &filename,
|
Chris@16
|
65 Ptree &pt,
|
Chris@16
|
66 const std::locale &loc = std::locale())
|
Chris@16
|
67 {
|
Chris@16
|
68 std::basic_ifstream<typename Ptree::key_type::value_type>
|
Chris@16
|
69 stream(filename.c_str());
|
Chris@16
|
70 if (!stream)
|
Chris@16
|
71 BOOST_PROPERTY_TREE_THROW(json_parser_error(
|
Chris@16
|
72 "cannot open file", filename, 0));
|
Chris@16
|
73 stream.imbue(loc);
|
Chris@16
|
74 read_json_internal(stream, pt, filename);
|
Chris@16
|
75 }
|
Chris@16
|
76
|
Chris@16
|
77 /**
|
Chris@16
|
78 * Translates the property tree to JSON and writes it the given output
|
Chris@16
|
79 * stream.
|
Chris@16
|
80 * @note Any property tree key containing only unnamed subkeys will be
|
Chris@16
|
81 * rendered as JSON arrays.
|
Chris@16
|
82 * @pre @e pt cannot contain keys that have both subkeys and non-empty data.
|
Chris@16
|
83 * @throw json_parser_error In case of error translating the property tree
|
Chris@16
|
84 * to JSON or writing to the output stream.
|
Chris@16
|
85 * @param stream The stream to which to write the JSON representation of the
|
Chris@16
|
86 * property tree.
|
Chris@16
|
87 * @param pt The property tree to tranlsate to JSON and output.
|
Chris@16
|
88 * @param pretty Whether to pretty-print. Defaults to true for backward
|
Chris@16
|
89 * compatibility.
|
Chris@16
|
90 */
|
Chris@16
|
91 template<class Ptree>
|
Chris@16
|
92 void write_json(std::basic_ostream<
|
Chris@16
|
93 typename Ptree::key_type::value_type
|
Chris@16
|
94 > &stream,
|
Chris@16
|
95 const Ptree &pt,
|
Chris@16
|
96 bool pretty = true)
|
Chris@16
|
97 {
|
Chris@16
|
98 write_json_internal(stream, pt, std::string(), pretty);
|
Chris@16
|
99 }
|
Chris@16
|
100
|
Chris@16
|
101 /**
|
Chris@16
|
102 * Translates the property tree to JSON and writes it the given file.
|
Chris@16
|
103 * @note Any property tree key containing only unnamed subkeys will be
|
Chris@16
|
104 * rendered as JSON arrays.
|
Chris@16
|
105 * @pre @e pt cannot contain keys that have both subkeys and non-empty data.
|
Chris@16
|
106 * @throw json_parser_error In case of error translating the property tree
|
Chris@16
|
107 * to JSON or writing to the file.
|
Chris@16
|
108 * @param filename The name of the file to which to write the JSON
|
Chris@16
|
109 * representation of the property tree.
|
Chris@16
|
110 * @param pt The property tree to translate to JSON and output.
|
Chris@16
|
111 * @param loc The locale to use when writing out to the output file.
|
Chris@16
|
112 * @param pretty Whether to pretty-print. Defaults to true and last place
|
Chris@16
|
113 * for backward compatibility.
|
Chris@16
|
114 */
|
Chris@16
|
115 template<class Ptree>
|
Chris@16
|
116 void write_json(const std::string &filename,
|
Chris@16
|
117 const Ptree &pt,
|
Chris@16
|
118 const std::locale &loc = std::locale(),
|
Chris@16
|
119 bool pretty = true)
|
Chris@16
|
120 {
|
Chris@16
|
121 std::basic_ofstream<typename Ptree::key_type::value_type>
|
Chris@16
|
122 stream(filename.c_str());
|
Chris@16
|
123 if (!stream)
|
Chris@16
|
124 BOOST_PROPERTY_TREE_THROW(json_parser_error(
|
Chris@16
|
125 "cannot open file", filename, 0));
|
Chris@16
|
126 stream.imbue(loc);
|
Chris@16
|
127 write_json_internal(stream, pt, filename, pretty);
|
Chris@16
|
128 }
|
Chris@16
|
129
|
Chris@16
|
130 } } }
|
Chris@16
|
131
|
Chris@16
|
132 namespace boost { namespace property_tree
|
Chris@16
|
133 {
|
Chris@16
|
134 using json_parser::read_json;
|
Chris@16
|
135 using json_parser::write_json;
|
Chris@16
|
136 using json_parser::json_parser_error;
|
Chris@16
|
137 } }
|
Chris@16
|
138
|
Chris@16
|
139 #endif
|