fix doc example typo
[boost.git] / boost / property_tree / json_parser.hpp
blob3f032fb68b0435b75f2336473c8bdc4165387e9d
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
3 //
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)
7 //
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>
18 #include <fstream>
19 #include <string>
20 #include <locale>
22 namespace boost { namespace property_tree { namespace json_parser
25 /**
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.
38 template<class Ptree>
39 void read_json(std::basic_istream<typename Ptree::key_type::value_type> &stream,
40 Ptree &pt)
42 read_json_internal(stream, pt, std::string());
45 /**
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.
59 template<class Ptree>
60 void read_json(const std::string &filename,
61 Ptree &pt,
62 const std::locale &loc = std::locale())
64 std::basic_ifstream<typename Ptree::key_type::value_type> stream(filename.c_str());
65 if (!stream)
66 BOOST_PROPERTY_TREE_THROW(json_parser_error("cannot open file", filename, 0));
67 stream.imbue(loc);
68 read_json_internal(stream, pt, filename);
71 /**
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
74 * as JSON arrays.
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
79 * property tree.
80 * @param pt The property tree to tranlsate to JSON and output.
82 template<class Ptree>
83 void write_json(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
84 const Ptree &pt)
86 write_json_internal(stream, pt, std::string());
89 /**
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
92 * as JSON arrays.
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,
103 const Ptree &pt,
104 const std::locale &loc = std::locale())
106 std::basic_ofstream<typename Ptree::key_type::value_type> stream(filename.c_str());
107 if (!stream)
108 BOOST_PROPERTY_TREE_THROW(json_parser_error("cannot open file", filename, 0));
109 stream.imbue(loc);
110 write_json_internal(stream, pt, filename);
113 } } }
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;
122 #endif