]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // ---------------------------------------------------------------------------- |
2 | // Copyright (C) 2002-2006 Marcin Kalicinski | |
3 | // Copyright (C) 2015 Sebastian Redl | |
4 | // | |
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) | |
8 | // | |
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 | |
13 | ||
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> | |
18 | ||
19 | #include <fstream> | |
20 | #include <string> | |
21 | #include <locale> | |
22 | ||
23 | namespace boost { namespace property_tree { namespace json_parser | |
24 | { | |
25 | ||
26 | /** | |
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 | |
36 | * tree. | |
37 | * @param stream Stream from which to read in the property tree. | |
38 | * @param[out] pt The property tree to populate. | |
39 | */ | |
40 | template<class Ptree> | |
41 | void read_json(std::basic_istream< | |
42 | typename Ptree::key_type::value_type | |
43 | > &stream, | |
44 | Ptree &pt) | |
45 | { | |
46 | detail::read_json_internal(stream, pt, std::string()); | |
47 | } | |
48 | ||
49 | /** | |
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 | |
59 | * tree. | |
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. | |
63 | */ | |
64 | template<class Ptree> | |
65 | void read_json(const std::string &filename, | |
66 | Ptree &pt, | |
67 | const std::locale &loc = std::locale()) | |
68 | { | |
69 | std::basic_ifstream<typename Ptree::key_type::value_type> | |
70 | stream(filename.c_str()); | |
71 | if (!stream) | |
72 | BOOST_PROPERTY_TREE_THROW(json_parser_error( | |
73 | "cannot open file", filename, 0)); | |
74 | stream.imbue(loc); | |
75 | detail::read_json_internal(stream, pt, filename); | |
76 | } | |
77 | ||
78 | /** | |
79 | * Translates the property tree to JSON and writes it the given output | |
80 | * stream. | |
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 | |
87 | * property tree. | |
88 | * @param pt The property tree to tranlsate to JSON and output. | |
89 | * @param pretty Whether to pretty-print. Defaults to true for backward | |
90 | * compatibility. | |
91 | */ | |
92 | template<class Ptree> | |
93 | void write_json(std::basic_ostream< | |
94 | typename Ptree::key_type::value_type | |
95 | > &stream, | |
96 | const Ptree &pt, | |
97 | bool pretty = true) | |
98 | { | |
99 | write_json_internal(stream, pt, std::string(), pretty); | |
100 | } | |
101 | ||
102 | /** | |
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. | |
115 | */ | |
116 | template<class Ptree> | |
117 | void write_json(const std::string &filename, | |
118 | const Ptree &pt, | |
119 | const std::locale &loc = std::locale(), | |
120 | bool pretty = true) | |
121 | { | |
122 | std::basic_ofstream<typename Ptree::key_type::value_type> | |
123 | stream(filename.c_str()); | |
124 | if (!stream) | |
125 | BOOST_PROPERTY_TREE_THROW(json_parser_error( | |
126 | "cannot open file", filename, 0)); | |
127 | stream.imbue(loc); | |
128 | write_json_internal(stream, pt, filename, pretty); | |
129 | } | |
130 | ||
131 | } } } | |
132 | ||
133 | namespace boost { namespace property_tree | |
134 | { | |
135 | using json_parser::read_json; | |
136 | using json_parser::write_json; | |
137 | using json_parser::json_parser_error; | |
138 | } } | |
139 | ||
140 | #endif |