-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspirit2json.h
218 lines (197 loc) · 6.35 KB
/
spirit2json.h
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* \file spirit2json.h
* \author Stefan Hacker
* \copyright \verbatim
*
* Copyright (c) 2011, Stefan Hacker <[email protected]>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the authors nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* \endverbatim
*/
/**
* \mainpage Overview
*
* spirit2json is a json parser/generator written in C++ using the Boost Spirit library (http://boost-spirit.com/).
*
* \section Usage
* Use spirit2::parse() to parse json and spirit2::generate() to generate it.
*/
#ifndef SPIRIT2JSON_H
#define SPIRIT2JSON_H
#include <string>
#include <stdexcept>
#include <ostream>
#include <vector>
#include <map>
#include <cstddef>
#include <boost/version.hpp>
#include <boost/variant.hpp>
#include <boost/config.hpp>
#if !defined(BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT) \
&& BOOST_VERSION < 104700 // Work around incorrect recursive_variant_ definition in older boosts
struct boost::recursive_variant_ {};
#endif
/**
* \brief Contains everything related spirit2json JSON parser/generator.
*/
namespace spirit2json {
/**
* \brief Typedef to std::nullptr_t for compilers that support it. Emulate otherwise.
*/
#if defined(BOOST_NO_NULLPTR) // C++03 compat
class JSONNull {
public:
template<class T>
operator T*() const { return 0; }
template <class C, class T>
operator T C::*() const { return 0; }
bool operator==(const JSONNull&) const { return true; }
private:
void operator &() const;
};
#else
typedef std::nullptr_t JSONNull;
#endif
/**
* \brief which() value enumeration enum for all types that can be contained in a JSONValue.
* \note Not to confuse with RTTI typeinfo related data. This is boost::variant specific.
*/
enum JSONValueTypes
{
JSON_STRING, //!< JSONValue(JSONString()).which() value
JSON_NUMBER, //!< JSONValue(JSONNumber(0)).which() value
JSON_BOOL, //!< JSONValue(JSONBool(false)).which() value
JSON_NULL, //!< JSONValue(JSONNull()).which() value
JSON_ARRAY, //!< JSONValue(JSONArray()).which() value
JSON_OBJECT //!< JSONValue(JSONObject()).which() value
};
//! Typedef to std::wstring
typedef std::wstring JSONString;
//! Typedef to double
typedef double JSONNumber;
//! Typedef to bool
typedef bool JSONBool;
/**
* \brief JSON-AST value type.
* Used to store the abstract JSON representation generated by the parse function
* and as the input to the generate and stream generation functionality.
*
* \note The recommended way to walk an JSONValue AST is with a boost visitor.
*
* Usage:
* \code
* wstring answer = generate(JSONValue(42));
* wstring hello = generate(JSONValue(L"world"));
* wstring array = generate(JSONValue(JSONArray()));
* ...
* JSONValue val(parse(L"{\"hello\":\"world\", \"array\":[4,2]}"));
* if (val.type() == JSON_OBJECT) {
* JSONObject object(boost::get<JSONObject>(val));
* ...
* \endcode
*/
typedef boost::make_recursive_variant<
JSONString,
JSONNumber,
JSONBool,
JSONNull,
std::vector<boost::recursive_variant_ >,
std::map<JSONString, boost::recursive_variant_ > >::type JSONValue;
/**
* \brief JSON-AST array type.
* A vector storing JSONValue objects.
*
* Usage:
* \code
* JSONArray array;
* array.push_back(JSONValue(L"Hello World"));
* array.push_back(JSONValue(42));
* array.push_back(JSONArray());
* ...
* wstring json = generate(JSONValue(array));
* ...
* wcout << JSONValue(array) << endl;
* \endcode
*
*/
typedef std::vector<JSONValue> JSONArray;
/**
* \brief JSON-AST object type.
* A map storing string, JSONValue pairs.
*
* Usage:
* \code
* JSONObject object;
* object.insert(JSONObjectPair(L"key", "value"));
* object.insert(JSONObjectPair(L"the answer to life the universe and everything", 42);
* object.insert(JSONObjectPair(L"an array", JSONArray());
* ...
* wstring json = generate(JSONValue(object));
* ...
* wcout << JSONValue(object) << endl;
* \endcode
*/
typedef std::map<JSONString, JSONValue> JSONObject;
//! Value type for JSONObject
typedef JSONObject::value_type JSONObjectPair;
/**
* \brief Baseclass for all exceptions in simple2json
*/
class Exception : public std::exception {
virtual const char* what() const throw() {
return "spirit2json: Exception";
}
};
/**
* \brief Thrown on parser failure.
*/
class ParsingFailed : public Exception {
virtual const char* what() const throw() {
return "spirit2json: Failed to parse given json";
}
};
/**
* \brief Parse a given JSON string and return its JSONValue representation.
* \param str JSON string
* \throw ParsingFailed Parser failure
* \return JSONValue representation
*/
JSONValue parse(JSONString str);
/**
* \brief Generate a JSON string representation from a given JSONValue.
* \param val JSONValue representation
* \return String representation
*/
JSONString generate(JSONValue& val);
}
/**
* \brief Stream output operator implementation for JSVONValues.
* \param output Output stream to write to
* \param val JSONValue to generate json from
* \return Given output stream
*/
std::wostream& operator<<(std::wostream& output, spirit2json::JSONValue& val);
#endif