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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#include "json.hpp"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>
namespace VideoStitch {
namespace Parse {
DataInputStream::DataInputStream(const std::string& data) : data(data), pos(0) {}
int DataInputStream::get() {
if (pos < data.size()) {
return data[pos++];
} else {
return EOF;
}
}
int DataInputStream::peek() {
if (pos < data.size()) {
return data[pos];
} else {
return EOF;
}
}
DataInputStream& DataInputStream::read(char* s, size_t n) {
if (pos + n <= data.size()) {
std::memcpy(s, data.data() + pos, n);
pos += n;
} else {
std::memcpy(s, data.data() + pos, n);
pos = data.size();
}
return *this;
}
bool DataInputStream::fail() const { return pos >= data.size(); }
namespace {
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
#error TODO
#endif
/**
* Prints the given int value on the smallest number of bytes, with type marker.
* @param os output sink
* @param value value
*/
void printUBJsonInt(std::ostream& os, const int64_t value) {
if (-128ll <= value && value <= 127ll) {
os << 'i' << (char)value;
} else if (-32768ll <= value && value <= 32767ll) {
os << 'I' << (char)(((uint64_t)value >> 8) & 0xffull) << (char)((value)&0xffull);
} else if (-2147483648ll <= value && value <= 2147483647ll) {
os << 'l' << (char)(((uint64_t)value >> 24) & 0xffull) << (char)(((uint64_t)value >> 16) & 0xffull)
<< (char)(((uint64_t)value >> 8) & 0xffull) << (char)(value & 0xffull);
} else {
os << 'L' << (char)(((uint64_t)value >> 56) & 0xffull) << (char)(((uint64_t)value >> 48) & 0xffull)
<< (char)(((uint64_t)value >> 40) & 0xffull) << (char)(((uint64_t)value >> 32) & 0xffull)
<< (char)(((uint64_t)value >> 24) & 0xffull) << (char)(((uint64_t)value >> 16) & 0xffull)
<< (char)(((uint64_t)value >> 8) & 0xffull) << (char)(value & 0xffull);
}
}
/**
* Writes a double value as UBJson, without leading string marker 'D'.
* We assume a ieee754 compliant compiler.
* @param os output sink
* @param value value
*/
void printUBJsonDouble(std::ostream& os, double value) {
union Bits {
double dValue;
uint64_t iValue;
};
Bits bits;
bits.dValue = value;
os << (char)((bits.iValue >> 56) & 0xffull) << (char)((bits.iValue >> 48) & 0xffull)
<< (char)((bits.iValue >> 40) & 0xffull) << (char)((bits.iValue >> 32) & 0xffull)
<< (char)((bits.iValue >> 24) & 0xffull) << (char)((bits.iValue >> 16) & 0xffull)
<< (char)((bits.iValue >> 8) & 0xffull) << (char)((bits.iValue) & 0xffull);
}
/**
* Prints the given string value, without leading string marker 'S'.
* @param os output sink
* @param value value
*/
void printUBJsonString(std::ostream& os, const std::string& value) {
printUBJsonInt(os, (int64_t)value.size());
os << value;
}
} // namespace
void JsonValue::printUBJson(std::ostream& os) const {
switch (type) {
case Ptv::Value::NIL:
os << 'Z';
break;
case Ptv::Value::BOOL:
os << (boolValue ? 'T' : 'F');
break;
case Ptv::Value::INT:
printUBJsonInt(os, intValue);
break;
case Ptv::Value::DOUBLE:
os << 'D';
printUBJsonDouble(os, doubleValue);
break;
case Ptv::Value::STRING:
os << 'S';
printUBJsonString(os, stringValue);
break;
case Ptv::Value::LIST:
os << '[';
for (size_t i = 0; i < listValue.size(); ++i) {
listValue[i]->printUBJson(os);
}
os << ']';
break;
case Ptv::Value::OBJECT:
os << '{';
for (int i = 0; i < content.size(); ++i) {
std::pair<const std::string*, const Ptv::Value*> p = content.get(i);
printUBJsonString(os, *p.first);
p.second->printUBJson(os);
}
os << '}';
break;
}
}
namespace {
/**
* Reads an int64_t from an UBJson stream. On error, the stream state is undefined.
* @param token the int type token.
* @param input Input stream.
* @param value Result.
* @return false on error.
*/
template <class StreamT>
bool readUBJsonInt(const int token, StreamT& input, int64_t& value) {
unsigned char buffer[] = {0, 0, 0, 0, 0, 0, 0, 0};
switch (token) {
case 'i':
input.read((char*)buffer, 1);
if (input.fail()) {
return false;
}
value = (char)buffer[0];
return true;
case 'U':
input.read((char*)buffer, 1);
if (input.fail()) {
return false;
}
value = buffer[0];
return true;
case 'I':
input.read((char*)buffer, 2);
if (input.fail()) {
return false;
}
value = (int16_t)(((uint16_t)buffer[0] << 8) | ((uint16_t)buffer[1]));
return true;
case 'l':
input.read((char*)buffer, 4);
if (input.fail()) {
return false;
}
value = (int32_t)(((uint32_t)buffer[0] << 24) | ((uint32_t)buffer[1] << 16) | ((uint32_t)buffer[2] << 8) |
((uint32_t)buffer[3]));
return true;
case 'L':
input.read((char*)buffer, 8);
if (input.fail()) {
return false;
}
value = (int64_t)(((uint64_t)buffer[0] << 56) | ((uint64_t)buffer[1] << 48) | ((uint64_t)buffer[2] << 40) |
((uint64_t)buffer[3] << 32) | ((uint64_t)buffer[4] << 24) | ((uint64_t)buffer[5] << 16) |
((uint64_t)buffer[6] << 8) | ((uint64_t)buffer[7]));
return true;
}
value = 0;
return false;
}
/**
* Reads a string from an UBJson stream (starting at str len). On error, the stream state is undefined.
* @param input Input stream.
* @param value Result.
* @return false on error.
*/
template <class StreamT>
bool readUBJsonString(StreamT& input, std::string& str) {
str.clear();
const int token = input.get();
int64_t sLen = 0;
if (!readUBJsonInt(token, input, sLen) || sLen < 0) {
return false;
}
for (int i = 0; i < sLen; ++i) {
str.push_back((char)input.get());
}
if (input.fail()) {
return false;
}
return true;
}
} // namespace
template <class StreamT>
PotentialJsonValue JsonValue::parseUBJson(StreamT& input) {
if (!(input.get() == '{' && (input.peek() == 'i' || input.peek() == 'U' || input.peek() == 'I' ||
input.peek() == 'l' || input.peek() == 'L'))) {
return ParseStatus::fromCode<ParseStatus::StatusCode::NotUBJsonFormat>();
}
PotentialJsonValue result(new JsonValue());
// The objects being populated. If we are expecting a value, the object will be a NIL.
// If we are expecting a list item, it will be a LIST, If we are expecting a field name, it will be an OBJECT.
// Anything else is invalid. Elements are not owned.
std::vector<JsonValue*> valueStack;
valueStack.push_back(result.object());
for (;;) {
if (input.peek() == EOF) {
if (!valueStack.empty()) {
return ParseStatus::fromCode<ParseStatus::StatusCode::InvalidUBJson>();
}
return result;
}
if (valueStack.empty()) {
return ParseStatus::fromCode<ParseStatus::StatusCode::InvalidUBJson>();
}
if (valueStack.back()->getType() == OBJECT) {
if (input.peek() == '}') {
input.get();
// End the object.
valueStack.pop_back();
continue;
}
// Expect a field name.
std::string fieldName;
if (!readUBJsonString(input, fieldName)) {
return ParseStatus::fromCode<ParseStatus::StatusCode::InvalidUBJson>();
}
JsonValue* newValue = new JsonValue((void*)NULL);
delete valueStack.back()->push(fieldName, newValue);
valueStack.push_back(newValue);
} else {
const int token = input.get();
JsonValue* value = NULL;
if (valueStack.back()->getType() == LIST) {
if (token == ']') {
// End the list.
valueStack.pop_back();
continue;
} else {
// Add an element.
value = new JsonValue((void*)NULL);
valueStack.back()->asList().push_back(value);
}
} else {
value = valueStack.back();
assert(value->getType() == NIL);
valueStack.pop_back();
}
switch (token) {
case 'Z':
value->asNil();
break;
case 'T':
value->asBool() = true;
break;
case 'F':
value->asBool() = false;
break;
case 'i':
case 'U':
case 'I':
case 'l':
case 'L':
if (!readUBJsonInt(token, input, value->asInt())) {
return ParseStatus::fromCode<ParseStatus::StatusCode::InvalidUBJson>();
}
break;
case 'D': {
union Bits {
double dValue;
int64_t iValue;
};
Bits bits;
if (!readUBJsonInt('L', input, bits.iValue)) {
return ParseStatus::fromCode<ParseStatus::StatusCode::InvalidUBJson>();
}
value->asDouble() = bits.dValue;
break;
}
case 'S':
if (!readUBJsonString(input, value->asString())) {
return ParseStatus::fromCode<ParseStatus::StatusCode::InvalidUBJson>();
}
break;
case '[':
value->asList();
valueStack.push_back(value);
break;
case '{':
value->asObject();
valueStack.push_back(value);
break;
default:
return ParseStatus::fromCode<ParseStatus::StatusCode::InvalidUBJson>();
}
}
}
}
// Explicit instantiations.
template PotentialJsonValue JsonValue::parseUBJson(std::istream& input);
template PotentialJsonValue JsonValue::parseUBJson(DataInputStream& input);
} // namespace Parse
} // namespace VideoStitch