forked from goatpig/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON_codec.h
More file actions
237 lines (185 loc) · 5.39 KB
/
JSON_codec.h
File metadata and controls
237 lines (185 loc) · 5.39 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2016, goatpig. //
// Distributed under the MIT license //
// See LICENSE-MIT or https://opensource.org/licenses/MIT //
// //
////////////////////////////////////////////////////////////////////////////////
#ifndef _H_JSONCODEC_
#define _H_JSONCODEC_
using namespace std;
#include <stdexcept>
#include <memory>
#include <vector>
#include <string>
#include <map>
#include <sstream>
#define FEE_STRAT_CONSERVATIVE "CONSERVATIVE"
#define FEE_STRAT_ECONOMICAL "ECONOMICAL"
enum JSON_StateEnum
{
JSON_null,
JSON_true,
JSON_false
};
////////////////////////////////////////////////////////////////////////////////
class JSON_Exception : public runtime_error
{
public:
JSON_Exception(const string& str) : runtime_error(str)
{}
};
////////////////////////////////////////////////////////////////////////////////
struct JSON_value
{
virtual ~JSON_value(void) = 0;
virtual void serialize(ostream&) const = 0;
};
struct JSON_array;
////////////////////////////////////////////////////////////////////////////////
struct JSON_string : JSON_value
{
string val_;
JSON_string(void)
{}
JSON_string(const string& val) : val_(val)
{}
void serialize(ostream& s) const
{
s << "\"" << val_ << "\"";
}
void unserialize(istream& s);
bool operator<(const JSON_string& rhs) const
{
return val_ < rhs.val_;
}
};
////////////////////////////////////////////////////////////////////////////////
struct JSON_number : JSON_value
{
double val_;
JSON_number(void)
{}
JSON_number(double val) : val_(val)
{}
JSON_number(int val) : val_(double(val))
{}
JSON_number(unsigned val) : val_(double(val))
{}
void serialize(ostream& s) const
{
s << val_;
}
void unserialize(istream& s)
{
s >> val_;
}
};
////////////////////////////////////////////////////////////////////////////////
struct JSON_state : JSON_value
{
JSON_StateEnum state_ = JSON_null;
void serialize(ostream& s) const
{
if (state_ == JSON_null)
s << "null";
else if (state_ == JSON_true)
s << "true";
else if (state_ == JSON_false)
s << "false";
else
throw JSON_Exception("unexpected state at ser");
}
void unserialize(istream& s);
};
////////////////////////////////////////////////////////////////////////////////
struct JSON_object : public JSON_value
{
private:
static int id_counter_;
public:
map<JSON_string, shared_ptr<JSON_value>> keyval_pairs_;
public:
const int id_;
JSON_object(void) :
id_(id_counter_++)
{}
bool add_pair(const string& key, const string& val)
{
auto jsonstr = make_shared<JSON_string>(val);
auto&& keyval = make_pair(
move(JSON_string(key)), dynamic_pointer_cast<JSON_value>(jsonstr));
auto insert_iter = keyval_pairs_.insert(move(keyval));
return insert_iter.second;
}
bool add_pair(const string& key, shared_ptr<JSON_value> val)
{
auto&& keyval = make_pair(move(JSON_string(key)), val);
auto insert_iter = keyval_pairs_.insert(move(keyval));
return insert_iter.second;
}
bool add_pair(const string& key, JSON_array& val)
{
auto jsonarr = make_shared<JSON_array>(move(val));
auto&& keyval = make_pair(
move(JSON_string(key)), dynamic_pointer_cast<JSON_value>(jsonarr));
auto insert_iter = keyval_pairs_.insert(move(keyval));
return insert_iter.second;
}
bool add_pair(const string& key, float val)
{
auto jsonarr = make_shared<JSON_number>(val);
auto&& keyval = make_pair(
move(JSON_string(key)), dynamic_pointer_cast<JSON_value>(jsonarr));
auto insert_iter = keyval_pairs_.insert(move(keyval));
return insert_iter.second;
}
bool add_pair(const string& key, int val)
{
return add_pair(key, float(val));
}
void serialize(ostream& s) const;
void unserialize(istream& s);
shared_ptr<JSON_value> getValForKey(const string&);
bool isResponseValid(int);
};
////////////////////////////////////////////////////////////////////////////////
struct JSON_array : public JSON_value
{
vector<shared_ptr<JSON_value>> values_;
void add_value(string& val)
{
auto jsonstr = make_shared<JSON_string>(val);
values_.push_back(dynamic_pointer_cast<JSON_value>(jsonstr));
}
void add_value(unsigned val)
{
auto jsonnum = make_shared<JSON_number>(val);
values_.push_back(dynamic_pointer_cast<JSON_value>(jsonnum));
}
void add_value(shared_ptr<JSON_value> valptr)
{
values_.push_back(valptr);
}
void serialize(ostream& s) const
{
s << "[";
if (values_.size() > 0)
{
auto iter = values_.begin();
while (1)
{
(*iter)->serialize(s);
++iter;
if (iter == values_.end())
break;
s << ", ";
}
}
s << "]";
}
void unserialize(istream&);
};
string JSON_encode(JSON_object& json_obj);
JSON_object JSON_decode(const string& json_str);
#endif