forked from GothenburgBitFactory/libshared
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSON.h
183 lines (163 loc) · 5.57 KB
/
JSON.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
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2006 - 2021, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_JSON
#define INCLUDED_JSON
#include <map>
#include <vector>
#include <string>
#include <Pig.h>
namespace json
{
enum jtype
{
j_value, // 0
j_object, // 1
j_array, // 2
j_string, // 3
j_number, // 4
j_literal // 5
};
class value
{
public:
value () {}
virtual ~value () {}
static value* parse (Pig&);
virtual jtype type ();
virtual std::string dump () const;
};
class string : public value
{
public:
string () {}
string (const std::string&);
~string () {}
static string* parse (Pig&);
jtype type ();
std::string dump () const;
public:
std::string _data;
};
class number : public value
{
public:
number () : _dvalue (0.0) {}
~number () {}
static number* parse (Pig&);
jtype type ();
std::string dump () const;
operator double () const;
public:
double _dvalue;
};
class literal : public value
{
public:
literal () : _lvalue (none) {}
~literal () {}
static literal* parse (Pig&);
jtype type ();
std::string dump () const;
public:
enum literal_value {none, nullvalue, falsevalue, truevalue};
literal_value _lvalue;
};
class array : public value
{
public:
array () {}
~array ();
static array* parse (Pig&);
jtype type ();
std::string dump () const;
public:
std::vector <value*> _data;
};
class object : public value
{
public:
object () {}
~object ();
static object* parse (Pig&);
static bool parse_pair (Pig&, std::string&, value*&);
jtype type ();
std::string dump () const;
public:
std::map <std::string, value*> _data;
};
// Parser entry point.
value* parse (const std::string&);
// Encode/decode for JSON entities.
std::string encode (const std::string&);
std::string decode (const std::string&);
class SAX
{
public:
class Sink
{
public:
virtual void eventDocStart () {}
virtual void eventDocEnd () {}
virtual void eventObjectStart () {}
virtual void eventObjectEnd (int) {}
virtual void eventArrayStart () {}
virtual void eventArrayEnd (int) {}
virtual void eventName (const std::string&) {}
virtual void eventValueNull () {}
virtual void eventValueBool (bool) {}
virtual void eventValueInt (int64_t) {}
virtual void eventValueUint (uint64_t) {}
virtual void eventValueDouble (double) {}
virtual void eventValueString (const std::string&) {}
};
bool parse (const std::string&, json::SAX::Sink&);
private:
void ignoreWhitespace (const std::string&, std::string::size_type&);
bool isObject (const std::string&, std::string::size_type&, SAX::Sink&);
bool isArray (const std::string&, std::string::size_type&, SAX::Sink&);
bool isPair (const std::string&, std::string::size_type&, SAX::Sink&);
bool isValue (const std::string&, std::string::size_type&, SAX::Sink&);
bool isKey (const std::string&, std::string::size_type&, SAX::Sink&);
bool isString (const std::string&, std::string::size_type&, SAX::Sink&);
bool isStringValue (const std::string&, std::string::size_type&, std::string&);
bool isNumber (const std::string&, std::string::size_type&, SAX::Sink&);
bool isInt (const std::string&, std::string::size_type&, std::string&);
bool isFrac (const std::string&, std::string::size_type&, std::string&);
bool isDigits (const std::string&, std::string::size_type&);
bool isDecDigit (int);
bool isHexDigit (int);
int hexToInt (int);
int hexToInt (int, int, int, int);
bool isExp (const std::string&, std::string::size_type&, std::string&);
bool isE (const std::string&, std::string::size_type&);
bool isBool (const std::string&, std::string::size_type&, SAX::Sink&);
bool isNull (const std::string&, std::string::size_type&, SAX::Sink&);
bool isLiteral (const std::string&, char, std::string::size_type&);
void error (const std::string&, std::string::size_type);
};
}
#endif
////////////////////////////////////////////////////////////////////////////////