forked from hacst/spirit2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspirit2json.h
84 lines (67 loc) · 1.97 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
#ifndef SPIRIT2JSON_H
#define SPIRIT2JSON_H
#include <string>
#include <stdexcept>
#include <ostream>
#include <vector>
#include <map>
#include <cstddef>
#include <boost/variant.hpp>
#include <boost/config.hpp>
struct boost::recursive_variant_ {};
namespace spirit2json {
enum JSONValueTypes
{
JSON_STRING, //!< JSONValue(JSONString()).which() value
JSON_LONG, //!< JSONValue(JSONLong(0)).which() value
JSON_DOUBLE, //!< JSONValue(JSONDouble(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
};
#if defined(BOOST_NO_NULLPTR)
struct JSONNull {
bool operator==(const JSONNull&) const {
return true;
}
operator bool() const {
return false;
}
};
static JSONNull nullptr = JSONNull();
#else
typedef std::nullptr_t JSONNull;
#endif
typedef std::string JSONString;
typedef long JSONLong;
typedef double JSONDouble;
typedef bool JSONBool;
typedef boost::make_recursive_variant<
JSONString,
JSONLong,
JSONDouble,
JSONBool,
JSONNull,
std::vector<boost::recursive_variant_ >,
std::map<std::string, boost::recursive_variant_ > >::type JSONValue;
typedef std::vector<JSONValue> JSONArray;
typedef std::map<std::string, JSONValue> JSONObject;
typedef JSONObject::value_type JSONPair;
class Exception : public std::exception {
virtual const char* what() const throw() {
return "spirit2json: Exception";
}
};
class ParsingFailed : public Exception {
virtual const char* what() const throw() {
return "spirit2json: Failed to parse given json";
}
};
JSONValue parse(const JSONString& str);
JSONString generate(const JSONValue& val);
}
std::ostream& operator<<(std::ostream& output, const spirit2json::JSONValue& val);
std::ostream& operator<<(std::ostream& output, const spirit2json::JSONArray& arr);
std::ostream& operator<<(std::ostream& output, const spirit2json::JSONObject& obj);
#endif