2018-12-17 16:55:11 +00:00
|
|
|
#include <qpdf/JSON.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2022-05-04 13:12:57 +00:00
|
|
|
#include <qpdf/BufferInputSource.hh>
|
2022-05-04 20:28:12 +00:00
|
|
|
#include <qpdf/Pl_Base64.hh>
|
|
|
|
#include <qpdf/Pl_Concatenate.hh>
|
2022-05-04 12:32:54 +00:00
|
|
|
#include <qpdf/Pl_String.hh>
|
2018-12-17 16:55:11 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2022-01-17 23:40:38 +00:00
|
|
|
#include <cstring>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <stdexcept>
|
2018-12-17 16:55:11 +00:00
|
|
|
|
2022-02-04 15:52:37 +00:00
|
|
|
JSON::Members::Members(std::shared_ptr<JSON_value> value) :
|
2022-05-01 18:06:31 +00:00
|
|
|
value(value),
|
|
|
|
start(0),
|
|
|
|
end(0)
|
2018-12-17 16:55:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-02-04 15:52:37 +00:00
|
|
|
JSON::JSON(std::shared_ptr<JSON_value> value) :
|
2018-12-17 16:55:11 +00:00
|
|
|
m(new Members(value))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:32:54 +00:00
|
|
|
void
|
|
|
|
JSON::writeClose(Pipeline* p, bool first, size_t depth, char const* delimiter)
|
|
|
|
{
|
|
|
|
if (!first) {
|
|
|
|
*p << "\n";
|
|
|
|
writeIndent(p, depth);
|
|
|
|
}
|
|
|
|
*p << delimiter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSON::writeIndent(Pipeline* p, size_t depth)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < depth; ++i) {
|
|
|
|
*p << " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSON::writeNext(Pipeline* p, bool& first, size_t depth)
|
|
|
|
{
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
*p << ",";
|
|
|
|
}
|
|
|
|
*p << "\n";
|
|
|
|
writeIndent(p, 1 + depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSON::writeDictionaryOpen(Pipeline* p, bool& first, size_t depth)
|
|
|
|
{
|
|
|
|
*p << "{";
|
|
|
|
first = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSON::writeArrayOpen(Pipeline* p, bool& first, size_t depth)
|
|
|
|
{
|
|
|
|
*p << "[";
|
|
|
|
first = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSON::writeDictionaryClose(Pipeline* p, bool first, size_t depth)
|
|
|
|
{
|
|
|
|
writeClose(p, first, depth, "}");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSON::writeArrayClose(Pipeline* p, bool first, size_t depth)
|
|
|
|
{
|
|
|
|
writeClose(p, first, depth, "]");
|
|
|
|
}
|
|
|
|
|
Objects json: write incrementally and in numeric order
The following script was used to adjust test data:
----------
#!/usr/bin/env python3
import json
import sys
import re
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
if 'objects' not in data:
continue
trailer = None
to_sort = []
for k, v in data['objects'].items():
if k == 'trailer':
trailer = v
else:
m = re.match(r'^(\d+) \d+ R', k)
if m:
to_sort.append([int(m.group(1)), k, v])
newobjects = {x[1]: x[2] for x in sorted(to_sort)}
if trailer is not None:
newobjects['trailer'] = trailer
data['objects'] = newobjects
print(json_dumps(data))
----------
2022-05-06 23:04:20 +00:00
|
|
|
void
|
|
|
|
JSON::writeDictionaryKey(
|
|
|
|
Pipeline* p, bool& first, std::string const& key, size_t depth)
|
|
|
|
{
|
|
|
|
writeNext(p, first, depth);
|
|
|
|
*p << "\"" << key << "\": ";
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:32:54 +00:00
|
|
|
void
|
|
|
|
JSON::writeDictionaryItem(
|
|
|
|
Pipeline* p,
|
|
|
|
bool& first,
|
|
|
|
std::string const& key,
|
|
|
|
JSON const& value,
|
|
|
|
size_t depth)
|
|
|
|
{
|
Objects json: write incrementally and in numeric order
The following script was used to adjust test data:
----------
#!/usr/bin/env python3
import json
import sys
import re
def json_dumps(data):
return json.dumps(data, ensure_ascii=False,
indent=2, separators=(',', ': '))
for filename in sys.argv[1:]:
with open(filename, 'r') as f:
data = json.loads(f.read())
if 'objects' not in data:
continue
trailer = None
to_sort = []
for k, v in data['objects'].items():
if k == 'trailer':
trailer = v
else:
m = re.match(r'^(\d+) \d+ R', k)
if m:
to_sort.append([int(m.group(1)), k, v])
newobjects = {x[1]: x[2] for x in sorted(to_sort)}
if trailer is not None:
newobjects['trailer'] = trailer
data['objects'] = newobjects
print(json_dumps(data))
----------
2022-05-06 23:04:20 +00:00
|
|
|
writeDictionaryKey(p, first, key, depth);
|
2022-05-04 12:32:54 +00:00
|
|
|
value.write(p, 1 + depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSON::writeArrayItem(
|
|
|
|
Pipeline* p, bool& first, JSON const& element, size_t depth)
|
|
|
|
{
|
|
|
|
writeNext(p, first, depth);
|
|
|
|
element.write(p, 1 + depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSON::JSON_dictionary::write(Pipeline* p, size_t depth) const
|
2018-12-17 16:55:11 +00:00
|
|
|
{
|
|
|
|
bool first = true;
|
2022-05-04 12:32:54 +00:00
|
|
|
writeDictionaryOpen(p, first, depth);
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& iter: members) {
|
2022-05-04 12:32:54 +00:00
|
|
|
writeDictionaryItem(p, first, iter.first, iter.second, depth);
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
2022-05-04 12:32:54 +00:00
|
|
|
writeDictionaryClose(p, first, depth);
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 12:32:54 +00:00
|
|
|
void
|
|
|
|
JSON::JSON_array::write(Pipeline* p, size_t depth) const
|
2018-12-17 16:55:11 +00:00
|
|
|
{
|
|
|
|
bool first = true;
|
2022-05-04 12:32:54 +00:00
|
|
|
writeArrayOpen(p, first, depth);
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& element: elements) {
|
2022-05-04 12:32:54 +00:00
|
|
|
writeArrayItem(p, first, element, depth);
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
2022-05-04 12:32:54 +00:00
|
|
|
writeArrayClose(p, first, depth);
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON::JSON_string::JSON_string(std::string const& utf8) :
|
2022-01-19 14:31:28 +00:00
|
|
|
utf8(utf8),
|
2018-12-17 16:55:11 +00:00
|
|
|
encoded(encode_string(utf8))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:32:54 +00:00
|
|
|
void
|
|
|
|
JSON::JSON_string::write(Pipeline* p, size_t) const
|
2018-12-17 16:55:11 +00:00
|
|
|
{
|
2022-05-04 12:32:54 +00:00
|
|
|
*p << "\"" << encoded << "\"";
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON::JSON_number::JSON_number(long long value) :
|
|
|
|
encoded(QUtil::int_to_string(value))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON::JSON_number::JSON_number(double value) :
|
|
|
|
encoded(QUtil::double_to_string(value, 6))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON::JSON_number::JSON_number(std::string const& value) :
|
|
|
|
encoded(value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:32:54 +00:00
|
|
|
void
|
|
|
|
JSON::JSON_number::write(Pipeline* p, size_t) const
|
2018-12-17 16:55:11 +00:00
|
|
|
{
|
2022-05-04 12:32:54 +00:00
|
|
|
*p << encoded;
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON::JSON_bool::JSON_bool(bool val) :
|
|
|
|
value(val)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:32:54 +00:00
|
|
|
void
|
|
|
|
JSON::JSON_bool::write(Pipeline* p, size_t) const
|
2018-12-17 16:55:11 +00:00
|
|
|
{
|
2022-05-04 12:32:54 +00:00
|
|
|
*p << (value ? "true" : "false");
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 12:32:54 +00:00
|
|
|
void
|
|
|
|
JSON::JSON_null::write(Pipeline* p, size_t) const
|
2018-12-17 16:55:11 +00:00
|
|
|
{
|
2022-05-04 12:32:54 +00:00
|
|
|
*p << "null";
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 20:28:12 +00:00
|
|
|
JSON::JSON_blob::JSON_blob(std::function<void(Pipeline*)> fn) :
|
|
|
|
fn(fn)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSON::JSON_blob::write(Pipeline* p, size_t) const
|
|
|
|
{
|
|
|
|
*p << "\"";
|
|
|
|
Pl_Concatenate cat("blob concatenate", p);
|
|
|
|
Pl_Base64 base64("blob base64", &cat, Pl_Base64::a_encode);
|
|
|
|
fn(&base64);
|
|
|
|
base64.finish();
|
|
|
|
*p << "\"";
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:32:54 +00:00
|
|
|
void
|
|
|
|
JSON::write(Pipeline* p, size_t depth) const
|
2018-12-17 16:55:11 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (0 == this->m->value.get()) {
|
2022-05-04 12:32:54 +00:00
|
|
|
*p << "null";
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-05-04 12:32:54 +00:00
|
|
|
this->m->value->write(p, depth);
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:32:54 +00:00
|
|
|
std::string
|
|
|
|
JSON::unparse() const
|
|
|
|
{
|
|
|
|
std::string s;
|
2022-05-06 21:37:25 +00:00
|
|
|
Pl_String p("unparse", nullptr, s);
|
2022-05-04 12:32:54 +00:00
|
|
|
write(&p, 0);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-12-17 16:55:11 +00:00
|
|
|
std::string
|
|
|
|
JSON::encode_string(std::string const& str)
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
size_t len = str.length();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < len; ++i) {
|
2018-12-17 16:55:11 +00:00
|
|
|
unsigned char ch = static_cast<unsigned char>(str.at(i));
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (ch) {
|
|
|
|
case '\\':
|
2018-12-17 16:55:11 +00:00
|
|
|
result += "\\\\";
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case '\"':
|
2018-12-17 16:55:11 +00:00
|
|
|
result += "\\\"";
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case '\b':
|
2018-12-17 16:55:11 +00:00
|
|
|
result += "\\b";
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case '\f':
|
2022-01-17 23:38:12 +00:00
|
|
|
result += "\\f";
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case '\n':
|
2018-12-17 16:55:11 +00:00
|
|
|
result += "\\n";
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case '\r':
|
2018-12-17 16:55:11 +00:00
|
|
|
result += "\\r";
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case '\t':
|
2018-12-17 16:55:11 +00:00
|
|
|
result += "\\t";
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
|
|
|
if (ch < 32) {
|
2018-12-17 16:55:11 +00:00
|
|
|
result += "\\u" + QUtil::int_to_string_base(ch, 16, 4);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2019-06-21 03:35:23 +00:00
|
|
|
result.append(1, static_cast<char>(ch));
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeDictionary()
|
|
|
|
{
|
2022-02-04 15:52:37 +00:00
|
|
|
return JSON(std::make_shared<JSON_dictionary>());
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::addDictionaryMember(std::string const& key, JSON const& val)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
JSON_dictionary* obj = dynamic_cast<JSON_dictionary*>(this->m->value.get());
|
|
|
|
if (0 == obj) {
|
2018-12-17 16:55:11 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON::addDictionaryMember called on non-dictionary");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (val.m->value.get()) {
|
2018-12-17 16:55:11 +00:00
|
|
|
obj->members[encode_string(key)] = val.m->value;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-04 15:52:37 +00:00
|
|
|
obj->members[encode_string(key)] = std::make_shared<JSON_null>();
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
return obj->members[encode_string(key)];
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeArray()
|
|
|
|
{
|
2022-02-04 15:52:37 +00:00
|
|
|
return JSON(std::make_shared<JSON_array>());
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::addArrayElement(JSON const& val)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
JSON_array* arr = dynamic_cast<JSON_array*>(this->m->value.get());
|
|
|
|
if (0 == arr) {
|
2018-12-17 16:55:11 +00:00
|
|
|
throw std::runtime_error("JSON::addArrayElement called on non-array");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (val.m->value.get()) {
|
2018-12-17 16:55:11 +00:00
|
|
|
arr->elements.push_back(val.m->value);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-04 15:52:37 +00:00
|
|
|
arr->elements.push_back(std::make_shared<JSON_null>());
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
return arr->elements.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeString(std::string const& utf8)
|
|
|
|
{
|
2022-02-04 15:52:37 +00:00
|
|
|
return JSON(std::make_shared<JSON_string>(utf8));
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeInt(long long int value)
|
|
|
|
{
|
2022-02-04 15:52:37 +00:00
|
|
|
return JSON(std::make_shared<JSON_number>(value));
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeReal(double value)
|
|
|
|
{
|
2022-02-04 15:52:37 +00:00
|
|
|
return JSON(std::make_shared<JSON_number>(value));
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeNumber(std::string const& encoded)
|
|
|
|
{
|
2022-02-04 15:52:37 +00:00
|
|
|
return JSON(std::make_shared<JSON_number>(encoded));
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeBool(bool value)
|
|
|
|
{
|
2022-02-04 15:52:37 +00:00
|
|
|
return JSON(std::make_shared<JSON_bool>(value));
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeNull()
|
|
|
|
{
|
2022-02-04 15:52:37 +00:00
|
|
|
return JSON(std::make_shared<JSON_null>());
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 20:28:12 +00:00
|
|
|
JSON
|
|
|
|
JSON::makeBlob(std::function<void(Pipeline*)> fn)
|
|
|
|
{
|
|
|
|
return JSON(std::make_shared<JSON_blob>(fn));
|
|
|
|
}
|
|
|
|
|
2022-01-17 23:38:05 +00:00
|
|
|
bool
|
|
|
|
JSON::isArray() const
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
return nullptr != dynamic_cast<JSON_array const*>(this->m->value.get());
|
2022-01-17 23:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JSON::isDictionary() const
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
return nullptr !=
|
|
|
|
dynamic_cast<JSON_dictionary const*>(this->m->value.get());
|
2022-01-17 23:38:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 14:31:28 +00:00
|
|
|
bool
|
|
|
|
JSON::getString(std::string& utf8) const
|
|
|
|
{
|
2022-02-04 15:10:19 +00:00
|
|
|
auto v = dynamic_cast<JSON_string const*>(this->m->value.get());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (v == nullptr) {
|
2022-01-19 14:31:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
utf8 = v->utf8;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JSON::getNumber(std::string& value) const
|
|
|
|
{
|
2022-02-04 15:10:19 +00:00
|
|
|
auto v = dynamic_cast<JSON_number const*>(this->m->value.get());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (v == nullptr) {
|
2022-01-19 14:31:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value = v->encoded;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JSON::getBool(bool& value) const
|
|
|
|
{
|
2022-02-04 15:10:19 +00:00
|
|
|
auto v = dynamic_cast<JSON_bool const*>(this->m->value.get());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (v == nullptr) {
|
2022-01-19 14:31:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value = v->value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JSON::isNull() const
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (dynamic_cast<JSON_null const*>(this->m->value.get())) {
|
2022-01-19 14:31:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JSON::forEachDictItem(
|
|
|
|
std::function<void(std::string const& key, JSON value)> fn) const
|
|
|
|
{
|
2022-02-04 15:10:19 +00:00
|
|
|
auto v = dynamic_cast<JSON_dictionary const*>(this->m->value.get());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (v == nullptr) {
|
2022-01-19 14:31:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& k: v->members) {
|
2022-01-19 14:31:28 +00:00
|
|
|
fn(k.first, JSON(k.second));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JSON::forEachArrayItem(std::function<void(JSON value)> fn) const
|
|
|
|
{
|
2022-02-04 15:10:19 +00:00
|
|
|
auto v = dynamic_cast<JSON_array const*>(this->m->value.get());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (v == nullptr) {
|
2022-01-19 14:31:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& i: v->elements) {
|
2022-01-19 14:31:28 +00:00
|
|
|
fn(JSON(i));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-17 16:55:11 +00:00
|
|
|
bool
|
|
|
|
JSON::checkSchema(JSON schema, std::list<std::string>& errors)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
return checkSchemaInternal(
|
|
|
|
this->m->value.get(), schema.m->value.get(), 0, errors, "");
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 19:33:26 +00:00
|
|
|
bool
|
2022-04-02 21:14:10 +00:00
|
|
|
JSON::checkSchema(
|
|
|
|
JSON schema, unsigned long flags, std::list<std::string>& errors)
|
2022-01-22 19:33:26 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
return checkSchemaInternal(
|
|
|
|
this->m->value.get(), schema.m->value.get(), flags, errors, "");
|
2022-01-22 19:33:26 +00:00
|
|
|
}
|
2018-12-17 16:55:11 +00:00
|
|
|
|
|
|
|
bool
|
2022-04-02 21:14:10 +00:00
|
|
|
JSON::checkSchemaInternal(
|
|
|
|
JSON_value* this_v,
|
|
|
|
JSON_value* sch_v,
|
|
|
|
unsigned long flags,
|
|
|
|
std::list<std::string>& errors,
|
|
|
|
std::string prefix)
|
2018-12-17 16:55:11 +00:00
|
|
|
{
|
|
|
|
JSON_array* this_arr = dynamic_cast<JSON_array*>(this_v);
|
|
|
|
JSON_dictionary* this_dict = dynamic_cast<JSON_dictionary*>(this_v);
|
|
|
|
|
|
|
|
JSON_array* sch_arr = dynamic_cast<JSON_array*>(sch_v);
|
|
|
|
JSON_dictionary* sch_dict = dynamic_cast<JSON_dictionary*>(sch_v);
|
|
|
|
|
2022-01-22 19:33:26 +00:00
|
|
|
JSON_string* sch_str = dynamic_cast<JSON_string*>(sch_v);
|
|
|
|
|
2018-12-17 16:55:11 +00:00
|
|
|
std::string err_prefix;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (prefix.empty()) {
|
2018-12-17 16:55:11 +00:00
|
|
|
err_prefix = "top-level object";
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2018-12-17 16:55:11 +00:00
|
|
|
err_prefix = "json key \"" + prefix + "\"";
|
|
|
|
}
|
|
|
|
|
2020-04-04 21:31:07 +00:00
|
|
|
std::string pattern_key;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (sch_dict) {
|
|
|
|
if (!this_dict) {
|
2018-12-17 16:55:11 +00:00
|
|
|
QTC::TC("libtests", "JSON wanted dictionary");
|
|
|
|
errors.push_back(err_prefix + " is supposed to be a dictionary");
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-04 21:31:07 +00:00
|
|
|
auto members = sch_dict->members;
|
|
|
|
std::string key;
|
|
|
|
if ((members.size() == 1) &&
|
|
|
|
((key = members.begin()->first, key.length() > 2) &&
|
2022-04-02 21:14:10 +00:00
|
|
|
(key.at(0) == '<') && (key.at(key.length() - 1) == '>'))) {
|
2020-04-04 21:31:07 +00:00
|
|
|
pattern_key = key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (sch_dict && (!pattern_key.empty())) {
|
2022-02-04 15:10:19 +00:00
|
|
|
auto pattern_schema = sch_dict->members[pattern_key].get();
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& iter: this_dict->members) {
|
2020-04-04 21:31:07 +00:00
|
|
|
std::string const& key = iter.first;
|
|
|
|
checkSchemaInternal(
|
2022-04-02 21:14:10 +00:00
|
|
|
this_dict->members[key].get(),
|
|
|
|
pattern_schema,
|
|
|
|
flags,
|
|
|
|
errors,
|
|
|
|
prefix + "." + key);
|
2020-04-04 21:31:07 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (sch_dict) {
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto& iter: sch_dict->members) {
|
2022-01-22 19:33:26 +00:00
|
|
|
std::string const& key = iter.first;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this_dict->members.count(key)) {
|
2018-12-17 16:55:11 +00:00
|
|
|
checkSchemaInternal(
|
2022-02-04 15:10:19 +00:00
|
|
|
this_dict->members[key].get(),
|
|
|
|
iter.second.get(),
|
2022-04-02 21:14:10 +00:00
|
|
|
flags,
|
|
|
|
errors,
|
|
|
|
prefix + "." + key);
|
|
|
|
} else {
|
|
|
|
if (flags & f_optional) {
|
2022-01-22 19:33:26 +00:00
|
|
|
QTC::TC("libtests", "JSON optional key");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-22 19:33:26 +00:00
|
|
|
QTC::TC("libtests", "JSON key missing in object");
|
|
|
|
errors.push_back(
|
|
|
|
err_prefix + ": key \"" + key +
|
|
|
|
"\" is present in schema but missing in object");
|
|
|
|
}
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& iter: this_dict->members) {
|
2022-02-04 15:52:37 +00:00
|
|
|
std::string const& key = iter.first;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (sch_dict->members.count(key) == 0) {
|
2018-12-17 16:55:11 +00:00
|
|
|
QTC::TC("libtests", "JSON key extra in object");
|
|
|
|
errors.push_back(
|
|
|
|
err_prefix + ": key \"" + key +
|
|
|
|
"\" is not present in schema but appears in object");
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (sch_arr) {
|
|
|
|
if (!this_arr) {
|
2018-12-17 16:55:11 +00:00
|
|
|
QTC::TC("libtests", "JSON wanted array");
|
|
|
|
errors.push_back(err_prefix + " is supposed to be an array");
|
|
|
|
return false;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (sch_arr->elements.size() != 1) {
|
2018-12-17 16:55:11 +00:00
|
|
|
QTC::TC("libtests", "JSON schema array error");
|
2022-04-02 21:14:10 +00:00
|
|
|
errors.push_back(
|
|
|
|
err_prefix + " schema array contains other than one item");
|
2018-12-17 16:55:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int i = 0;
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& element: this_arr->elements) {
|
2018-12-17 16:55:11 +00:00
|
|
|
checkSchemaInternal(
|
2022-02-04 15:52:37 +00:00
|
|
|
element.get(),
|
2022-02-04 15:10:19 +00:00
|
|
|
sch_arr->elements.at(0).get(),
|
2022-04-02 21:14:10 +00:00
|
|
|
flags,
|
|
|
|
errors,
|
|
|
|
prefix + "." + QUtil::int_to_string(i));
|
2022-02-04 15:52:37 +00:00
|
|
|
++i;
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (!sch_str) {
|
2022-01-22 19:33:26 +00:00
|
|
|
QTC::TC("libtests", "JSON schema other type");
|
2022-04-02 21:14:10 +00:00
|
|
|
errors.push_back(
|
|
|
|
err_prefix + " schema value is not dictionary, array, or string");
|
2022-01-22 19:33:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-17 16:55:11 +00:00
|
|
|
|
|
|
|
return errors.empty();
|
|
|
|
}
|
2022-01-17 23:40:38 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
namespace
|
|
|
|
{
|
2022-01-17 23:40:38 +00:00
|
|
|
class JSONParser
|
|
|
|
{
|
|
|
|
public:
|
2022-05-04 13:12:57 +00:00
|
|
|
JSONParser(InputSource& is, JSON::Reactor* reactor) :
|
|
|
|
is(is),
|
2022-05-01 18:06:31 +00:00
|
|
|
reactor(reactor),
|
2022-01-17 23:40:38 +00:00
|
|
|
lex_state(ls_top),
|
|
|
|
number_before_point(0),
|
|
|
|
number_after_point(0),
|
|
|
|
number_after_e(0),
|
|
|
|
number_saw_point(false),
|
|
|
|
number_saw_e(false),
|
2022-05-04 13:12:57 +00:00
|
|
|
bytes(0),
|
|
|
|
p(buf),
|
|
|
|
u_count(0),
|
|
|
|
offset(0),
|
|
|
|
done(false),
|
2022-01-17 23:40:38 +00:00
|
|
|
parser_state(ps_top)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-04 13:12:57 +00:00
|
|
|
std::shared_ptr<JSON> parse();
|
2022-01-17 23:40:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void getToken();
|
|
|
|
void handleToken();
|
|
|
|
static std::string decode_string(std::string const& json);
|
|
|
|
|
|
|
|
enum parser_state_e {
|
|
|
|
ps_top,
|
|
|
|
ps_dict_begin,
|
|
|
|
ps_dict_after_key,
|
|
|
|
ps_dict_after_colon,
|
|
|
|
ps_dict_after_item,
|
|
|
|
ps_dict_after_comma,
|
|
|
|
ps_array_begin,
|
|
|
|
ps_array_after_item,
|
|
|
|
ps_array_after_comma,
|
|
|
|
ps_done,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum lex_state_e {
|
|
|
|
ls_top,
|
|
|
|
ls_number,
|
|
|
|
ls_alpha,
|
|
|
|
ls_string,
|
|
|
|
ls_backslash,
|
2022-05-04 13:12:57 +00:00
|
|
|
ls_u4,
|
2022-01-17 23:40:38 +00:00
|
|
|
};
|
|
|
|
|
2022-05-04 13:12:57 +00:00
|
|
|
InputSource& is;
|
2022-05-01 18:06:31 +00:00
|
|
|
JSON::Reactor* reactor;
|
2022-01-17 23:40:38 +00:00
|
|
|
lex_state_e lex_state;
|
|
|
|
size_t number_before_point;
|
|
|
|
size_t number_after_point;
|
|
|
|
size_t number_after_e;
|
|
|
|
bool number_saw_point;
|
|
|
|
bool number_saw_e;
|
2022-05-04 13:12:57 +00:00
|
|
|
char buf[16384];
|
|
|
|
size_t bytes;
|
2022-01-17 23:40:38 +00:00
|
|
|
char const* p;
|
2022-05-04 13:12:57 +00:00
|
|
|
size_t u_count;
|
|
|
|
size_t offset;
|
|
|
|
bool done;
|
|
|
|
std::string token;
|
2022-01-17 23:40:38 +00:00
|
|
|
parser_state_e parser_state;
|
2022-02-04 15:52:37 +00:00
|
|
|
std::vector<std::shared_ptr<JSON>> stack;
|
2022-01-17 23:40:38 +00:00
|
|
|
std::vector<parser_state_e> ps_stack;
|
|
|
|
std::string dict_key;
|
|
|
|
};
|
2022-04-02 21:14:10 +00:00
|
|
|
} // namespace
|
2022-01-17 23:40:38 +00:00
|
|
|
|
|
|
|
std::string
|
|
|
|
JSONParser::decode_string(std::string const& str)
|
|
|
|
{
|
|
|
|
// The string has already been validated when this private method
|
|
|
|
// is called, so errors are logic errors instead of runtime
|
|
|
|
// errors.
|
|
|
|
size_t len = str.length();
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((len < 2) || (str.at(0) != '"') || (str.at(len - 1) != '"')) {
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"JSON Parse: decode_string called with other than \"...\"");
|
|
|
|
}
|
|
|
|
char const* s = str.c_str();
|
|
|
|
// Move inside the quotation marks
|
|
|
|
++s;
|
|
|
|
len -= 2;
|
|
|
|
std::string result;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
if (s[i] == '\\') {
|
|
|
|
if (i + 1 >= len) {
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::logic_error("JSON parse: nothing after \\");
|
|
|
|
}
|
|
|
|
char ch = s[++i];
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (ch) {
|
|
|
|
case '\\':
|
|
|
|
case '\"':
|
|
|
|
case '/':
|
2022-02-25 16:42:50 +00:00
|
|
|
// \/ is allowed in json input, but so is /, so we
|
|
|
|
// don't map / to \/ in output.
|
2022-01-17 23:40:38 +00:00
|
|
|
result.append(1, ch);
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 'b':
|
2022-01-17 23:40:38 +00:00
|
|
|
result.append(1, '\b');
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 'f':
|
2022-01-17 23:40:38 +00:00
|
|
|
result.append(1, '\f');
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 'n':
|
2022-01-17 23:40:38 +00:00
|
|
|
result.append(1, '\n');
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 'r':
|
2022-01-17 23:40:38 +00:00
|
|
|
result.append(1, '\r');
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 't':
|
2022-01-17 23:40:38 +00:00
|
|
|
result.append(1, '\t');
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case 'u':
|
|
|
|
if (i + 4 >= len) {
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"JSON parse: not enough characters after \\u");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
std::string hex =
|
2022-04-02 21:14:10 +00:00
|
|
|
QUtil::hex_decode(std::string(s + i + 1, s + i + 5));
|
2022-01-17 23:40:38 +00:00
|
|
|
i += 4;
|
|
|
|
unsigned char high = static_cast<unsigned char>(hex.at(0));
|
|
|
|
unsigned char low = static_cast<unsigned char>(hex.at(1));
|
|
|
|
unsigned long codepoint = high;
|
|
|
|
codepoint <<= 8;
|
|
|
|
codepoint += low;
|
|
|
|
result += QUtil::toUTF8(codepoint);
|
|
|
|
}
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
|
|
|
throw std::logic_error("JSON parse: bad character after \\");
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-17 23:40:38 +00:00
|
|
|
result.append(1, s[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
void
|
|
|
|
JSONParser::getToken()
|
2022-01-17 23:40:38 +00:00
|
|
|
{
|
2022-05-04 13:12:57 +00:00
|
|
|
enum { append, ignore, reread } action = append;
|
|
|
|
bool ready = false;
|
|
|
|
token.clear();
|
|
|
|
while (!done) {
|
|
|
|
if (p == (buf + bytes)) {
|
|
|
|
p = buf;
|
|
|
|
bytes = is.read(buf, sizeof(buf));
|
|
|
|
if (bytes == 0) {
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (*p == 0) {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse null character");
|
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: null character at offset " +
|
2022-05-04 13:12:57 +00:00
|
|
|
QUtil::uint_to_string(offset));
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2022-05-04 13:12:57 +00:00
|
|
|
action = append;
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (lex_state) {
|
|
|
|
case ls_top:
|
|
|
|
if (*p == '"') {
|
2022-01-17 23:40:38 +00:00
|
|
|
lex_state = ls_string;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (QUtil::is_space(*p)) {
|
2022-05-04 13:12:57 +00:00
|
|
|
action = ignore;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if ((*p >= 'a') && (*p <= 'z')) {
|
2022-01-17 23:40:38 +00:00
|
|
|
lex_state = ls_alpha;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (*p == '-') {
|
2022-01-17 23:40:38 +00:00
|
|
|
lex_state = ls_number;
|
|
|
|
number_before_point = 0;
|
|
|
|
number_after_point = 0;
|
|
|
|
number_after_e = 0;
|
|
|
|
number_saw_point = false;
|
|
|
|
number_saw_e = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if ((*p >= '0') && (*p <= '9')) {
|
2022-01-17 23:40:38 +00:00
|
|
|
lex_state = ls_number;
|
|
|
|
number_before_point = 1;
|
|
|
|
number_after_point = 0;
|
|
|
|
number_after_e = 0;
|
|
|
|
number_saw_point = false;
|
|
|
|
number_saw_e = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (*p == '.') {
|
2022-01-17 23:40:38 +00:00
|
|
|
lex_state = ls_number;
|
|
|
|
number_before_point = 0;
|
|
|
|
number_after_point = 0;
|
|
|
|
number_after_e = 0;
|
|
|
|
number_saw_point = true;
|
|
|
|
number_saw_e = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (strchr("{}[]:,", *p)) {
|
2022-05-04 13:12:57 +00:00
|
|
|
ready = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse bad character");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": unexpected character " + std::string(p, 1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ls_number:
|
|
|
|
if ((*p >= '0') && (*p <= '9')) {
|
|
|
|
if (number_saw_e) {
|
2022-01-17 23:40:38 +00:00
|
|
|
++number_after_e;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (number_saw_point) {
|
2022-01-17 23:40:38 +00:00
|
|
|
++number_after_point;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-17 23:40:38 +00:00
|
|
|
++number_before_point;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (*p == '.') {
|
|
|
|
if (number_saw_e) {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse point after e");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": numeric literal: decimal point after e");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (number_saw_point) {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse duplicate point");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": numeric literal: decimal point already seen");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-17 23:40:38 +00:00
|
|
|
number_saw_point = true;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (*p == 'e') {
|
|
|
|
if (number_saw_e) {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse duplicate e");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": numeric literal: e already seen");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-17 23:40:38 +00:00
|
|
|
number_saw_e = true;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if ((*p == '+') || (*p == '-')) {
|
|
|
|
if (number_saw_e && (number_after_e == 0)) {
|
2022-01-17 23:40:38 +00:00
|
|
|
// okay
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse unexpected sign");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": numeric literal: unexpected sign");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (QUtil::is_space(*p)) {
|
2022-05-04 13:12:57 +00:00
|
|
|
action = ignore;
|
|
|
|
ready = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (strchr("{}[]:,", *p)) {
|
2022-05-04 13:12:57 +00:00
|
|
|
action = reread;
|
|
|
|
ready = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse numeric bad character");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": numeric literal: unexpected character " +
|
|
|
|
std::string(p, 1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ls_alpha:
|
|
|
|
if ((*p >= 'a') && (*p <= 'z')) {
|
2022-01-17 23:40:38 +00:00
|
|
|
// okay
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (QUtil::is_space(*p)) {
|
2022-05-04 13:12:57 +00:00
|
|
|
action = ignore;
|
|
|
|
ready = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (strchr("{}[]:,", *p)) {
|
2022-05-04 13:12:57 +00:00
|
|
|
action = reread;
|
|
|
|
ready = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse keyword bad character");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": keyword: unexpected character " + std::string(p, 1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ls_string:
|
|
|
|
if (*p == '"') {
|
2022-05-04 13:12:57 +00:00
|
|
|
ready = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (*p == '\\') {
|
2022-01-17 23:40:38 +00:00
|
|
|
lex_state = ls_backslash;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ls_backslash:
|
2022-01-28 14:05:06 +00:00
|
|
|
/* cSpell: ignore bfnrt */
|
2022-04-02 21:14:10 +00:00
|
|
|
if (strchr("\\\"/bfnrt", *p)) {
|
2022-01-17 23:40:38 +00:00
|
|
|
lex_state = ls_string;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (*p == 'u') {
|
2022-05-04 13:12:57 +00:00
|
|
|
lex_state = ls_u4;
|
|
|
|
u_count = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse backslash bad character");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": invalid character after backslash: " +
|
|
|
|
std::string(p, 1));
|
|
|
|
}
|
|
|
|
break;
|
2022-05-04 13:12:57 +00:00
|
|
|
|
|
|
|
case ls_u4:
|
|
|
|
if (!QUtil::is_hex_digit(*p)) {
|
|
|
|
QTC::TC("libtests", "JSON parse bad hex after u");
|
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " +
|
|
|
|
QUtil::uint_to_string(offset - u_count - 1) +
|
|
|
|
": \\u must be followed by four hex digits");
|
|
|
|
}
|
|
|
|
if (++u_count == 4) {
|
|
|
|
lex_state = ls_string;
|
|
|
|
}
|
|
|
|
break;
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2022-05-04 13:12:57 +00:00
|
|
|
switch (action) {
|
|
|
|
case reread:
|
|
|
|
break;
|
|
|
|
case append:
|
|
|
|
token.append(1, *p);
|
|
|
|
// fall through
|
|
|
|
case ignore:
|
|
|
|
++p;
|
|
|
|
++offset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ready) {
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-05-04 13:12:57 +00:00
|
|
|
if (done) {
|
|
|
|
if ((!token.empty()) && (!ready)) {
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (lex_state) {
|
|
|
|
case ls_top:
|
2022-01-17 23:40:38 +00:00
|
|
|
// Can't happen
|
2022-05-04 13:12:57 +00:00
|
|
|
throw std::logic_error("tok_start set in ls_top while parsing");
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ls_number:
|
|
|
|
case ls_alpha:
|
2022-05-04 13:12:57 +00:00
|
|
|
// okay
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
|
2022-05-04 13:12:57 +00:00
|
|
|
case ls_u4:
|
|
|
|
QTC::TC("libtests", "JSON parse premature end of u");
|
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " +
|
|
|
|
QUtil::uint_to_string(offset - u_count - 1) +
|
|
|
|
": \\u must be followed by four characters");
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ls_string:
|
|
|
|
case ls_backslash:
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse unterminated string");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": unterminated string");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSONParser::handleToken()
|
|
|
|
{
|
2022-05-04 13:12:57 +00:00
|
|
|
if (token.empty()) {
|
2022-01-17 23:40:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (parser_state == ps_done) {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse junk after object");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
|
|
|
": material follows end of object: " + token);
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Git string value
|
2022-01-28 14:05:06 +00:00
|
|
|
std::string s_value;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (lex_state == ls_string) {
|
2022-01-17 23:40:38 +00:00
|
|
|
// Token includes the quotation marks
|
2022-05-04 13:12:57 +00:00
|
|
|
if (token.length() < 2) {
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::logic_error("JSON string length < 2");
|
|
|
|
}
|
2022-05-04 13:12:57 +00:00
|
|
|
s_value = decode_string(token);
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
|
|
|
// Based on the lexical state and value, figure out whether we are
|
|
|
|
// looking at an item or a delimiter. It will always be exactly
|
|
|
|
// one of those two or an error condition.
|
|
|
|
|
2022-02-04 15:52:37 +00:00
|
|
|
std::shared_ptr<JSON> item;
|
2022-01-17 23:40:38 +00:00
|
|
|
char delimiter = '\0';
|
2022-05-04 13:12:57 +00:00
|
|
|
// Already verified that token is not empty
|
|
|
|
char first_char = token.at(0);
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (lex_state) {
|
|
|
|
case ls_top:
|
2022-05-04 13:12:57 +00:00
|
|
|
switch (first_char) {
|
2022-04-02 21:14:10 +00:00
|
|
|
case '{':
|
2022-02-04 15:52:37 +00:00
|
|
|
item = std::make_shared<JSON>(JSON::makeDictionary());
|
2022-05-04 13:12:57 +00:00
|
|
|
item->setStart(offset - token.length());
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case '[':
|
2022-02-04 15:52:37 +00:00
|
|
|
item = std::make_shared<JSON>(JSON::makeArray());
|
2022-05-04 13:12:57 +00:00
|
|
|
item->setStart(offset - token.length());
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
2022-05-04 13:12:57 +00:00
|
|
|
delimiter = first_char;
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ls_number:
|
|
|
|
if (number_saw_point && (number_after_point == 0)) {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse decimal with no digits");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": decimal point with no digits");
|
|
|
|
}
|
|
|
|
if ((number_before_point > 1) &&
|
2022-05-04 13:12:57 +00:00
|
|
|
((first_char == '0') ||
|
|
|
|
((first_char == '-') && (token.at(1) == '0')))) {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse leading zero");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": number with leading zero");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((number_before_point == 0) && (number_after_point == 0)) {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse number no digits");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": number with no digits");
|
|
|
|
}
|
2022-05-04 13:12:57 +00:00
|
|
|
item = std::make_shared<JSON>(JSON::makeNumber(token));
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ls_alpha:
|
2022-05-04 13:12:57 +00:00
|
|
|
if (token == "true") {
|
2022-02-04 15:52:37 +00:00
|
|
|
item = std::make_shared<JSON>(JSON::makeBool(true));
|
2022-05-04 13:12:57 +00:00
|
|
|
} else if (token == "false") {
|
2022-02-04 15:52:37 +00:00
|
|
|
item = std::make_shared<JSON>(JSON::makeBool(false));
|
2022-05-04 13:12:57 +00:00
|
|
|
} else if (token == "null") {
|
2022-02-04 15:52:37 +00:00
|
|
|
item = std::make_shared<JSON>(JSON::makeNull());
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse invalid keyword");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
|
|
|
": invalid keyword " + token);
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ls_string:
|
2022-02-04 15:52:37 +00:00
|
|
|
item = std::make_shared<JSON>(JSON::makeString(s_value));
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ls_backslash:
|
2022-05-04 13:12:57 +00:00
|
|
|
case ls_u4:
|
|
|
|
throw std::logic_error(
|
|
|
|
"tok_end is set while state = ls_backslash or ls_u4");
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((item.get() == nullptr) == (delimiter == '\0')) {
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"JSONParser::handleToken: logic error: exactly one of item"
|
|
|
|
" or delimiter must be set");
|
|
|
|
}
|
|
|
|
|
|
|
|
// See whether what we have is allowed at this point.
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (item.get()) {
|
|
|
|
switch (parser_state) {
|
|
|
|
case ps_done:
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::logic_error("can't happen; ps_done already handled");
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ps_dict_after_key:
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse expected colon");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": expected ':'");
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ps_dict_after_item:
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse expected , or }");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": expected ',' or '}'");
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ps_array_after_item:
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse expected, or ]");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": expected ',' or ']'");
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ps_dict_begin:
|
|
|
|
case ps_dict_after_comma:
|
|
|
|
if (lex_state != ls_string) {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse string as dict key");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": expect string as dictionary key");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ps_top:
|
|
|
|
case ps_dict_after_colon:
|
|
|
|
case ps_array_begin:
|
|
|
|
case ps_array_after_comma:
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
// okay
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (delimiter == '}') {
|
|
|
|
if (!((parser_state == ps_dict_begin) ||
|
|
|
|
(parser_state == ps_dict_after_item)))
|
2022-01-17 23:40:38 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
QTC::TC("libtests", "JSON parse unexpected }");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": unexpected dictionary end delimiter");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (delimiter == ']') {
|
|
|
|
if (!((parser_state == ps_array_begin) ||
|
|
|
|
(parser_state == ps_array_after_item)))
|
2022-01-17 23:40:38 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
QTC::TC("libtests", "JSON parse unexpected ]");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": unexpected array end delimiter");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (delimiter == ':') {
|
|
|
|
if (parser_state != ps_dict_after_key) {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse unexpected :");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": unexpected colon");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (delimiter == ',') {
|
|
|
|
if (!((parser_state == ps_dict_after_item) ||
|
|
|
|
(parser_state == ps_array_after_item))) {
|
2022-01-17 23:40:38 +00:00
|
|
|
QTC::TC("libtests", "JSON parse unexpected ,");
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-01-17 23:40:38 +00:00
|
|
|
": unexpected comma");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (delimiter != '\0') {
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::logic_error("JSONParser::handleToken: bad delimiter");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we know we have a delimiter or item that is allowed. Do
|
|
|
|
// whatever we need to do with it.
|
|
|
|
|
|
|
|
parser_state_e next_state = ps_top;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (delimiter == ':') {
|
2022-01-17 23:40:38 +00:00
|
|
|
next_state = ps_dict_after_colon;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (delimiter == ',') {
|
|
|
|
if (parser_state == ps_dict_after_item) {
|
2022-01-17 23:40:38 +00:00
|
|
|
next_state = ps_dict_after_comma;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (parser_state == ps_array_after_item) {
|
2022-01-17 23:40:38 +00:00
|
|
|
next_state = ps_array_after_comma;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
|
|
|
throw std::logic_error("JSONParser::handleToken: unexpected parser"
|
|
|
|
" state for comma");
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if ((delimiter == '}') || (delimiter == ']')) {
|
2022-01-17 23:40:38 +00:00
|
|
|
next_state = ps_stack.back();
|
|
|
|
ps_stack.pop_back();
|
2022-05-01 18:06:31 +00:00
|
|
|
auto tos = stack.back();
|
2022-05-04 13:12:57 +00:00
|
|
|
tos->setEnd(offset);
|
2022-05-01 18:06:31 +00:00
|
|
|
if (reactor) {
|
|
|
|
reactor->containerEnd(*tos);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (next_state != ps_done) {
|
2022-01-17 23:40:38 +00:00
|
|
|
stack.pop_back();
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (delimiter != '\0') {
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"JSONParser::handleToken: unexpected delimiter in transition");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (item.get()) {
|
2022-05-01 18:06:31 +00:00
|
|
|
if (!(item->isArray() || item->isDictionary())) {
|
2022-05-04 13:12:57 +00:00
|
|
|
item->setStart(offset - token.length());
|
|
|
|
item->setEnd(offset);
|
2022-05-01 18:06:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-04 15:52:37 +00:00
|
|
|
std::shared_ptr<JSON> tos;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!stack.empty()) {
|
2022-01-17 23:40:38 +00:00
|
|
|
tos = stack.back();
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (parser_state) {
|
|
|
|
case ps_dict_begin:
|
|
|
|
case ps_dict_after_comma:
|
2022-01-28 14:05:06 +00:00
|
|
|
this->dict_key = s_value;
|
2022-01-17 23:40:38 +00:00
|
|
|
item = nullptr;
|
|
|
|
next_state = ps_dict_after_key;
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ps_dict_after_colon:
|
2022-05-01 18:06:31 +00:00
|
|
|
if (!reactor || !reactor->dictionaryItem(dict_key, *item)) {
|
|
|
|
tos->addDictionaryMember(dict_key, *item);
|
|
|
|
}
|
2022-01-17 23:40:38 +00:00
|
|
|
next_state = ps_dict_after_item;
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ps_array_begin:
|
|
|
|
case ps_array_after_comma:
|
2022-05-01 18:06:31 +00:00
|
|
|
if (!reactor || !reactor->arrayItem(*item)) {
|
|
|
|
tos->addArrayElement(*item);
|
|
|
|
}
|
2022-01-17 23:40:38 +00:00
|
|
|
next_state = ps_array_after_item;
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ps_top:
|
2022-01-17 23:40:38 +00:00
|
|
|
next_state = ps_done;
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case ps_dict_after_key:
|
|
|
|
case ps_dict_after_item:
|
|
|
|
case ps_array_after_item:
|
|
|
|
case ps_done:
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"JSONParser::handleToken: unexpected parser state");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"JSONParser::handleToken: unexpected null item in transition");
|
|
|
|
}
|
|
|
|
|
2022-05-14 14:11:52 +00:00
|
|
|
if (reactor && item.get()) {
|
|
|
|
// Calling container start method is postponed until after
|
|
|
|
// adding the containers to their parent containers, if any.
|
|
|
|
// This makes it much easier to keep track of the current
|
|
|
|
// nesting level.
|
|
|
|
if (item->isDictionary()) {
|
|
|
|
reactor->dictionaryStart();
|
|
|
|
} else if (item->isArray()) {
|
|
|
|
reactor->arrayStart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 23:40:38 +00:00
|
|
|
// Prepare for next token
|
2022-04-02 21:14:10 +00:00
|
|
|
if (item.get()) {
|
|
|
|
if (item->isDictionary()) {
|
2022-01-17 23:40:38 +00:00
|
|
|
stack.push_back(item);
|
|
|
|
ps_stack.push_back(next_state);
|
|
|
|
next_state = ps_dict_begin;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (item->isArray()) {
|
2022-01-17 23:40:38 +00:00
|
|
|
stack.push_back(item);
|
|
|
|
ps_stack.push_back(next_state);
|
|
|
|
next_state = ps_array_begin;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (parser_state == ps_top) {
|
2022-01-17 23:40:38 +00:00
|
|
|
stack.push_back(item);
|
|
|
|
}
|
|
|
|
}
|
2022-05-01 13:34:17 +00:00
|
|
|
if (ps_stack.size() > 500) {
|
|
|
|
throw std::runtime_error(
|
2022-05-04 13:12:57 +00:00
|
|
|
"JSON: offset " + QUtil::uint_to_string(offset) +
|
2022-05-01 13:34:17 +00:00
|
|
|
": maximum object depth exceeded");
|
|
|
|
}
|
2022-01-17 23:40:38 +00:00
|
|
|
parser_state = next_state;
|
|
|
|
lex_state = ls_top;
|
|
|
|
}
|
|
|
|
|
2022-02-04 15:52:37 +00:00
|
|
|
std::shared_ptr<JSON>
|
2022-05-04 13:12:57 +00:00
|
|
|
JSONParser::parse()
|
2022-01-17 23:40:38 +00:00
|
|
|
{
|
2022-05-04 13:12:57 +00:00
|
|
|
while (!done) {
|
2022-01-17 23:40:38 +00:00
|
|
|
getToken();
|
|
|
|
handleToken();
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (parser_state != ps_done) {
|
2022-01-28 14:05:06 +00:00
|
|
|
QTC::TC("libtests", "JSON parse premature EOF");
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::runtime_error("JSON: premature end of input");
|
|
|
|
}
|
2022-05-01 18:06:31 +00:00
|
|
|
auto const& tos = stack.back();
|
|
|
|
if (reactor && tos.get() && !(tos->isArray() || tos->isDictionary())) {
|
|
|
|
reactor->topLevelScalar();
|
|
|
|
}
|
|
|
|
return tos;
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
2022-05-04 13:12:57 +00:00
|
|
|
JSON::parse(InputSource& is, Reactor* reactor)
|
|
|
|
{
|
|
|
|
JSONParser jp(is, reactor);
|
|
|
|
return *jp.parse();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::parse(std::string const& s)
|
2022-01-17 23:40:38 +00:00
|
|
|
{
|
2022-05-04 13:12:57 +00:00
|
|
|
BufferInputSource bis("json input", s);
|
|
|
|
JSONParser jp(bis, nullptr);
|
|
|
|
return *jp.parse();
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2022-05-01 18:06:31 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
JSON::setStart(size_t start)
|
|
|
|
{
|
|
|
|
this->m->start = start;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSON::setEnd(size_t end)
|
|
|
|
{
|
|
|
|
this->m->end = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
JSON::getStart() const
|
|
|
|
{
|
|
|
|
return this->m->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
JSON::getEnd() const
|
|
|
|
{
|
|
|
|
return this->m->end;
|
|
|
|
}
|