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