2018-12-17 16:55:11 +00:00
|
|
|
#include <qpdf/JSON.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2024-02-09 13:03:53 +00:00
|
|
|
#include <qpdf/JSON_writer.hh>
|
|
|
|
|
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>
|
|
|
|
#include <qpdf/QUtil.hh>
|
2022-01-17 23:40:38 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <stdexcept>
|
2018-12-17 16:55:11 +00:00
|
|
|
|
2023-02-05 18:53:55 +00:00
|
|
|
JSON::Members::Members(std::unique_ptr<JSON_value> value) :
|
|
|
|
value(std::move(value))
|
2018-12-17 16:55:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-05 18:53:55 +00:00
|
|
|
JSON::JSON(std::unique_ptr<JSON_value> value) :
|
|
|
|
m(new Members(std::move(value)))
|
2018-12-17 16:55:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:32:54 +00:00
|
|
|
void
|
|
|
|
JSON::writeClose(Pipeline* p, bool first, size_t depth, char const* delimiter)
|
|
|
|
{
|
2023-01-28 11:09:46 +00:00
|
|
|
if (first) {
|
|
|
|
*p << delimiter;
|
|
|
|
} else {
|
|
|
|
std::string s{"\n"};
|
|
|
|
s.append(2 * depth, ' ');
|
|
|
|
*p << s + delimiter;
|
2022-05-04 12:32:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSON::writeNext(Pipeline* p, bool& first, size_t depth)
|
|
|
|
{
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
2023-01-28 11:15:27 +00:00
|
|
|
std::string s{"\n"};
|
|
|
|
s.append(2 * depth, ' ');
|
|
|
|
*p << s;
|
2022-05-04 12:32:54 +00:00
|
|
|
} else {
|
2023-01-28 11:15:27 +00:00
|
|
|
std::string s{",\n"};
|
|
|
|
s.append(2 * depth, ' ');
|
|
|
|
*p << s;
|
2022-05-04 12:32:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2023-01-28 12:54:49 +00:00
|
|
|
*p << std::string("\"") + key + "\": ";
|
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
|
|
|
}
|
|
|
|
|
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-07-30 17:32:59 +00:00
|
|
|
value.write(p, depth);
|
2022-05-04 12:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSON::writeArrayItem(Pipeline* p, bool& first, JSON const& element, size_t depth)
|
|
|
|
{
|
|
|
|
writeNext(p, first, depth);
|
2022-07-30 17:32:59 +00:00
|
|
|
element.write(p, depth);
|
2022-05-04 12:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-02-04 15:52:37 +00:00
|
|
|
for (auto const& iter: members) {
|
2022-07-30 17:32:59 +00:00
|
|
|
writeDictionaryItem(p, first, iter.first, iter.second, 1 + 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-02-04 15:52:37 +00:00
|
|
|
for (auto const& element: elements) {
|
2022-07-30 17:32:59 +00:00
|
|
|
writeArrayItem(p, first, element, 1 + 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) :
|
2023-02-11 17:46:30 +00:00
|
|
|
JSON_value(vt_string),
|
2022-01-19 14:31:28 +00:00
|
|
|
utf8(utf8),
|
2024-02-09 13:03:53 +00:00
|
|
|
encoded(Writer::encode_string(utf8))
|
2018-12-17 16:55:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2023-01-28 12:59:26 +00:00
|
|
|
*p << std::string("\"") + encoded + "\"";
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON::JSON_number::JSON_number(long long value) :
|
2023-02-11 17:46:30 +00:00
|
|
|
JSON_value(vt_number),
|
2022-09-21 16:49:21 +00:00
|
|
|
encoded(std::to_string(value))
|
2018-12-17 16:55:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON::JSON_number::JSON_number(double value) :
|
2023-02-11 17:46:30 +00:00
|
|
|
JSON_value(vt_number),
|
2018-12-17 16:55:11 +00:00
|
|
|
encoded(QUtil::double_to_string(value, 6))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON::JSON_number::JSON_number(std::string const& value) :
|
2023-02-11 17:46:30 +00:00
|
|
|
JSON_value(vt_number),
|
2018-12-17 16:55:11 +00:00
|
|
|
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) :
|
2023-02-11 17:46:30 +00:00
|
|
|
JSON_value(vt_bool),
|
2018-12-17 16:55:11 +00:00
|
|
|
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) :
|
2023-02-11 17:46:30 +00:00
|
|
|
JSON_value(vt_blob),
|
2022-05-04 20:28:12 +00:00
|
|
|
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
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
if (!m) {
|
2022-05-04 12:32:54 +00:00
|
|
|
*p << "null";
|
2018-12-17 16:55:11 +00:00
|
|
|
} else {
|
2022-05-04 12:32:54 +00:00
|
|
|
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
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
if (!m) {
|
|
|
|
return "null";
|
|
|
|
}
|
2022-05-04 12:32:54 +00:00
|
|
|
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
|
2024-02-09 13:03:53 +00:00
|
|
|
JSON::Writer::encode_string(std::string const& str)
|
2018-12-17 16:55:11 +00:00
|
|
|
{
|
2023-02-03 14:28:45 +00:00
|
|
|
static auto constexpr hexchars = "0123456789abcdef";
|
|
|
|
|
|
|
|
auto begin = str.cbegin();
|
|
|
|
auto end = str.cend();
|
|
|
|
auto iter = begin;
|
|
|
|
while (iter != end) {
|
|
|
|
auto c = static_cast<unsigned char>(*iter);
|
|
|
|
if ((c > 34 && c != '\\') || c == ' ' || c == 33) {
|
|
|
|
// Optimistically check that no char in str requires escaping. Hopefully we can just
|
|
|
|
// return the input str.
|
|
|
|
++iter;
|
|
|
|
} else {
|
|
|
|
// We found a char that requires escaping. Initialize result to the chars scanned so
|
|
|
|
// far, append/replace the rest of str one char at a time, and return the result.
|
|
|
|
std::string result{begin, iter};
|
|
|
|
|
|
|
|
for (; iter != end; ++iter) {
|
|
|
|
auto ch = static_cast<unsigned char>(*iter);
|
|
|
|
if ((ch > 34 && ch != '\\') || ch == ' ' || ch == 33) {
|
|
|
|
// Check for most common case first.
|
|
|
|
result += *iter;
|
|
|
|
} else {
|
|
|
|
switch (ch) {
|
|
|
|
case '\\':
|
|
|
|
result += "\\\\";
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
result += "\\\"";
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
result += "\\b";
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
result += "\\f";
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
result += "\\n";
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
result += "\\r";
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
result += "\\t";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result += ch < 16 ? "\\u000" : "\\u001";
|
|
|
|
result += hexchars[ch % 16];
|
|
|
|
}
|
|
|
|
}
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
2023-02-03 14:28:45 +00:00
|
|
|
return result;
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-03 14:28:45 +00:00
|
|
|
return str;
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeDictionary()
|
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {std::make_unique<JSON_dictionary>()};
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::addDictionaryMember(std::string const& key, JSON const& val)
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
if (auto* obj = m ? dynamic_cast<JSON_dictionary*>(m->value.get()) : nullptr) {
|
2024-02-09 13:03:53 +00:00
|
|
|
return obj->members[Writer::encode_string(key)] = val.m ? val : makeNull();
|
2023-01-28 14:32:01 +00:00
|
|
|
} else {
|
2018-12-17 16:55:11 +00:00
|
|
|
throw std::runtime_error("JSON::addDictionaryMember called on non-dictionary");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 13:57:30 +00:00
|
|
|
bool
|
|
|
|
JSON::checkDictionaryKeySeen(std::string const& key)
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
if (auto* obj = m ? dynamic_cast<JSON_dictionary*>(m->value.get()) : nullptr) {
|
|
|
|
return !obj->parsed_keys.insert(key).second;
|
2022-05-20 13:57:30 +00:00
|
|
|
}
|
2024-01-14 16:38:29 +00:00
|
|
|
throw std::logic_error("JSON::checkDictionaryKey called on non-dictionary");
|
|
|
|
return false; // unreachable
|
2022-05-20 13:57:30 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 16:55:11 +00:00
|
|
|
JSON
|
|
|
|
JSON::makeArray()
|
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {std::make_unique<JSON_array>()};
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::addArrayElement(JSON const& val)
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
if (auto* arr = m ? dynamic_cast<JSON_array*>(m->value.get()) : nullptr) {
|
|
|
|
if (val.m) {
|
|
|
|
arr->elements.push_back(val);
|
|
|
|
} else {
|
|
|
|
arr->elements.push_back(makeNull());
|
|
|
|
}
|
|
|
|
return arr->elements.back();
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
2024-01-14 16:38:29 +00:00
|
|
|
throw std::runtime_error("JSON::addArrayElement called on non-array");
|
|
|
|
return {}; // unreachable
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeString(std::string const& utf8)
|
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {std::make_unique<JSON_string>(utf8)};
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeInt(long long int value)
|
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {std::make_unique<JSON_number>(value)};
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeReal(double value)
|
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {std::make_unique<JSON_number>(value)};
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeNumber(std::string const& encoded)
|
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {std::make_unique<JSON_number>(encoded)};
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeBool(bool value)
|
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {std::make_unique<JSON_bool>(value)};
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSON
|
|
|
|
JSON::makeNull()
|
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {std::make_unique<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)
|
|
|
|
{
|
2023-05-27 20:04:32 +00:00
|
|
|
return {std::make_unique<JSON_blob>(fn)};
|
2022-05-04 20:28:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 23:38:05 +00:00
|
|
|
bool
|
|
|
|
JSON::isArray() const
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
return m ? m->value->type_code == vt_array : false;
|
2022-01-17 23:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JSON::isDictionary() const
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
return m && m->value->type_code == vt_dictionary;
|
2022-01-17 23:38:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 14:31:28 +00:00
|
|
|
bool
|
|
|
|
JSON::getString(std::string& utf8) const
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
if (m && m->value->type_code == vt_string) {
|
2023-02-11 17:46:30 +00:00
|
|
|
auto v = dynamic_cast<JSON_string const*>(m->value.get());
|
|
|
|
utf8 = v->utf8;
|
|
|
|
return true;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
2023-02-11 17:46:30 +00:00
|
|
|
return false;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JSON::getNumber(std::string& value) const
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
if (m && m->value->type_code == vt_number) {
|
2023-02-11 17:46:30 +00:00
|
|
|
auto v = dynamic_cast<JSON_number const*>(m->value.get());
|
|
|
|
value = v->encoded;
|
|
|
|
return true;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
2023-02-11 17:46:30 +00:00
|
|
|
return false;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JSON::getBool(bool& value) const
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
if (m && m->value->type_code == vt_bool) {
|
2023-02-11 17:46:30 +00:00
|
|
|
auto v = dynamic_cast<JSON_bool const*>(m->value.get());
|
|
|
|
value = v->value;
|
|
|
|
return true;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
2023-02-11 17:46:30 +00:00
|
|
|
return false;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JSON::isNull() const
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
return m && m->value->type_code == vt_null;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
2024-01-14 16:38:51 +00:00
|
|
|
JSON
|
|
|
|
JSON::getDictItem(std::string const& key) const
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
if (auto v = m ? dynamic_cast<JSON_dictionary const*>(m->value.get()) : nullptr) {
|
2024-01-14 16:38:51 +00:00
|
|
|
if (auto it = v->members.find(key); it != v->members.end()) {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return makeNull();
|
|
|
|
}
|
|
|
|
|
2022-01-19 14:31:28 +00:00
|
|
|
bool
|
|
|
|
JSON::forEachDictItem(std::function<void(std::string const& key, JSON value)> fn) const
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
if (auto v = m ? dynamic_cast<JSON_dictionary const*>(m->value.get()) : nullptr) {
|
|
|
|
for (auto const& [key, value]: v->members) {
|
|
|
|
fn(key, value);
|
|
|
|
}
|
|
|
|
return true;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
2024-01-14 16:38:29 +00:00
|
|
|
return false;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
JSON::forEachArrayItem(std::function<void(JSON value)> fn) const
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
if (auto v = m ? dynamic_cast<JSON_array const*>(m->value.get()) : nullptr) {
|
|
|
|
for (auto const& i: v->elements) {
|
|
|
|
fn(JSON(i));
|
|
|
|
}
|
|
|
|
return true;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
2024-01-14 16:38:29 +00:00
|
|
|
return false;
|
2022-01-19 14:31:28 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 16:55:11 +00:00
|
|
|
bool
|
|
|
|
JSON::checkSchema(JSON schema, std::list<std::string>& errors)
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
return m && checkSchemaInternal(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
|
|
|
|
JSON::checkSchema(JSON schema, unsigned long flags, std::list<std::string>& errors)
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
return m && checkSchemaInternal(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
|
|
|
|
JSON::checkSchemaInternal(
|
|
|
|
JSON_value* this_v,
|
|
|
|
JSON_value* sch_v,
|
2022-01-22 19:33:26 +00:00
|
|
|
unsigned long flags,
|
2018-12-17 16:55:11 +00:00
|
|
|
std::list<std::string>& errors,
|
|
|
|
std::string prefix)
|
|
|
|
{
|
2023-05-20 12:34:53 +00:00
|
|
|
auto* this_arr = dynamic_cast<JSON_array*>(this_v);
|
|
|
|
auto* this_dict = dynamic_cast<JSON_dictionary*>(this_v);
|
2018-12-17 16:55:11 +00:00
|
|
|
|
2023-05-20 12:34:53 +00:00
|
|
|
auto* sch_arr = dynamic_cast<JSON_array*>(sch_v);
|
|
|
|
auto* sch_dict = dynamic_cast<JSON_dictionary*>(sch_v);
|
2018-12-17 16:55:11 +00:00
|
|
|
|
2023-05-20 12:34:53 +00:00
|
|
|
auto* sch_str = dynamic_cast<JSON_string*>(sch_v);
|
2022-01-22 19:33:26 +00:00
|
|
|
|
2018-12-17 16:55:11 +00:00
|
|
|
std::string err_prefix;
|
|
|
|
if (prefix.empty()) {
|
|
|
|
err_prefix = "top-level object";
|
|
|
|
} else {
|
|
|
|
err_prefix = "json key \"" + prefix + "\"";
|
|
|
|
}
|
|
|
|
|
2020-04-04 21:31:07 +00:00
|
|
|
std::string pattern_key;
|
2018-12-17 16:55:11 +00:00
|
|
|
if (sch_dict) {
|
|
|
|
if (!this_dict) {
|
|
|
|
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) && (key.at(0) == '<') &&
|
|
|
|
(key.at(key.length() - 1) == '>'))) {
|
|
|
|
pattern_key = key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sch_dict && (!pattern_key.empty())) {
|
2023-02-05 18:39:30 +00:00
|
|
|
auto pattern_schema = sch_dict->members[pattern_key].m->value.get();
|
2020-04-09 00:14:04 +00:00
|
|
|
for (auto const& iter: this_dict->members) {
|
2020-04-04 21:31:07 +00:00
|
|
|
std::string const& key = iter.first;
|
|
|
|
checkSchemaInternal(
|
2023-02-05 18:39:30 +00:00
|
|
|
this_dict->members[key].m->value.get(),
|
2022-02-04 15:10:19 +00:00
|
|
|
pattern_schema,
|
2022-01-22 19:33:26 +00:00
|
|
|
flags,
|
|
|
|
errors,
|
|
|
|
prefix + "." + key);
|
2020-04-04 21:31:07 +00:00
|
|
|
}
|
|
|
|
} else if (sch_dict) {
|
2022-01-22 19:33:26 +00:00
|
|
|
for (auto& iter: sch_dict->members) {
|
|
|
|
std::string const& key = iter.first;
|
2018-12-17 16:55:11 +00:00
|
|
|
if (this_dict->members.count(key)) {
|
|
|
|
checkSchemaInternal(
|
2023-02-05 18:39:30 +00:00
|
|
|
this_dict->members[key].m->value.get(),
|
|
|
|
iter.second.m->value.get(),
|
2022-01-22 19:33:26 +00:00
|
|
|
flags,
|
|
|
|
errors,
|
|
|
|
prefix + "." + key);
|
2018-12-17 16:55:11 +00:00
|
|
|
} else {
|
2022-01-22 19:33:26 +00:00
|
|
|
if (flags & f_optional) {
|
|
|
|
QTC::TC("libtests", "JSON optional key");
|
|
|
|
} else {
|
|
|
|
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-02-04 15:52:37 +00:00
|
|
|
for (auto const& iter: this_dict->members) {
|
|
|
|
std::string const& key = iter.first;
|
2018-12-17 16:55:11 +00:00
|
|
|
if (sch_dict->members.count(key) == 0) {
|
|
|
|
QTC::TC("libtests", "JSON key extra in object");
|
|
|
|
errors.push_back(
|
|
|
|
err_prefix + ": key \"" + key +
|
|
|
|
"\" is not present in schema but appears in object");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (sch_arr) {
|
2022-07-24 20:44:51 +00:00
|
|
|
auto n_elements = sch_arr->elements.size();
|
|
|
|
if (n_elements == 1) {
|
|
|
|
// A single-element array in the schema allows a single element in the object or a
|
|
|
|
// variable-length array, each of whose items must conform to the single element of the
|
|
|
|
// schema array. This doesn't apply to arrays of arrays -- we fall back to the behavior
|
|
|
|
// of allowing a single item only when the object is not an array.
|
|
|
|
if (this_arr) {
|
|
|
|
int i = 0;
|
|
|
|
for (auto const& element: this_arr->elements) {
|
|
|
|
checkSchemaInternal(
|
2023-02-05 18:39:30 +00:00
|
|
|
element.m->value.get(),
|
|
|
|
sch_arr->elements.at(0).m->value.get(),
|
2022-07-24 20:44:51 +00:00
|
|
|
flags,
|
|
|
|
errors,
|
2022-09-21 16:49:21 +00:00
|
|
|
prefix + "." + std::to_string(i));
|
2022-07-24 20:44:51 +00:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QTC::TC("libtests", "JSON schema array for single item");
|
|
|
|
checkSchemaInternal(
|
2023-02-05 18:39:30 +00:00
|
|
|
this_v, sch_arr->elements.at(0).m->value.get(), flags, errors, prefix);
|
2022-07-24 20:44:51 +00:00
|
|
|
}
|
|
|
|
} else if (!this_arr || (this_arr->elements.size() != n_elements)) {
|
|
|
|
QTC::TC("libtests", "JSON schema array length mismatch");
|
2018-12-17 16:55:11 +00:00
|
|
|
errors.push_back(
|
2022-07-24 20:44:51 +00:00
|
|
|
err_prefix + " is supposed to be an array of length " + std::to_string(n_elements));
|
2018-12-17 16:55:11 +00:00
|
|
|
return false;
|
2022-07-24 20:44:51 +00:00
|
|
|
} else {
|
|
|
|
// A multi-element array in the schema must correspond to an element of the same length
|
|
|
|
// in the object. Each element in the object is validated against the corresponding
|
|
|
|
// element in the schema.
|
|
|
|
size_t i = 0;
|
2022-07-24 20:17:03 +00:00
|
|
|
for (auto const& element: this_arr->elements) {
|
|
|
|
checkSchemaInternal(
|
2023-02-05 18:39:30 +00:00
|
|
|
element.m->value.get(),
|
|
|
|
sch_arr->elements.at(i).m->value.get(),
|
2022-07-24 20:17:03 +00:00
|
|
|
flags,
|
|
|
|
errors,
|
2022-09-21 16:49:21 +00:00
|
|
|
prefix + "." + std::to_string(i));
|
2022-07-24 20:17:03 +00:00
|
|
|
++i;
|
|
|
|
}
|
2018-12-17 16:55:11 +00:00
|
|
|
}
|
2022-01-22 19:33:26 +00:00
|
|
|
} else if (!sch_str) {
|
|
|
|
QTC::TC("libtests", "JSON schema other type");
|
|
|
|
errors.push_back(err_prefix + " schema value is not dictionary, array, or string");
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-17 16:55:11 +00:00
|
|
|
|
|
|
|
return errors.empty();
|
|
|
|
}
|
2022-01-17 23:40:38 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
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),
|
2023-06-01 13:12:39 +00:00
|
|
|
p(buf)
|
2022-01-17 23:40:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
JSON parse();
|
2022-01-17 23:40:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
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,
|
2023-01-24 11:47:06 +00:00
|
|
|
ls_number_minus,
|
2023-01-24 11:22:06 +00:00
|
|
|
ls_number_leading_zero,
|
2023-01-23 19:51:27 +00:00
|
|
|
ls_number_before_point,
|
2023-01-23 20:08:11 +00:00
|
|
|
ls_number_point,
|
2023-01-24 00:34:22 +00:00
|
|
|
ls_number_after_point,
|
2023-01-24 01:36:38 +00:00
|
|
|
ls_number_e,
|
2023-01-24 13:04:31 +00:00
|
|
|
ls_number_e_sign,
|
2022-01-17 23:40:38 +00:00
|
|
|
ls_alpha,
|
|
|
|
ls_string,
|
2024-02-06 20:30:29 +00:00
|
|
|
ls_after_string,
|
2022-01-17 23:40:38 +00:00
|
|
|
ls_backslash,
|
2022-05-04 13:12:57 +00:00
|
|
|
ls_u4,
|
2023-01-26 13:21:45 +00:00
|
|
|
ls_begin_array,
|
|
|
|
ls_end_array,
|
|
|
|
ls_begin_dict,
|
|
|
|
ls_end_dict,
|
|
|
|
ls_colon,
|
|
|
|
ls_comma,
|
2022-01-17 23:40:38 +00:00
|
|
|
};
|
|
|
|
|
2023-01-27 14:37:25 +00:00
|
|
|
struct StackFrame
|
|
|
|
{
|
2023-01-29 15:21:29 +00:00
|
|
|
StackFrame(parser_state_e state, JSON& item) :
|
2023-01-27 14:37:25 +00:00
|
|
|
state(state),
|
|
|
|
item(item)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
parser_state_e state;
|
2023-01-29 15:21:29 +00:00
|
|
|
JSON item;
|
2023-01-27 14:37:25 +00:00
|
|
|
};
|
|
|
|
|
2023-02-01 10:29:32 +00:00
|
|
|
void getToken();
|
|
|
|
void handleToken();
|
|
|
|
void tokenError();
|
|
|
|
static void handle_u_code(
|
|
|
|
unsigned long codepoint,
|
|
|
|
qpdf_offset_t offset,
|
|
|
|
unsigned long& high_surrogate,
|
|
|
|
qpdf_offset_t& high_offset,
|
|
|
|
std::string& result);
|
|
|
|
inline void append();
|
|
|
|
inline void append(lex_state_e);
|
|
|
|
inline void ignore();
|
|
|
|
inline void ignore(lex_state_e);
|
|
|
|
|
2022-05-04 13:12:57 +00:00
|
|
|
InputSource& is;
|
2022-05-01 18:06:31 +00:00
|
|
|
JSON::Reactor* reactor;
|
2023-06-01 13:12:39 +00:00
|
|
|
lex_state_e lex_state{ls_top};
|
2022-05-04 13:12:57 +00:00
|
|
|
char buf[16384];
|
2023-06-01 13:12:39 +00:00
|
|
|
size_t bytes{0};
|
2022-01-17 23:40:38 +00:00
|
|
|
char const* p;
|
2023-06-01 13:12:39 +00:00
|
|
|
qpdf_offset_t u_count{0};
|
2023-01-27 18:58:50 +00:00
|
|
|
unsigned long u_value{0};
|
2023-06-01 13:12:39 +00:00
|
|
|
qpdf_offset_t offset{0};
|
|
|
|
bool done{false};
|
2022-05-04 13:12:57 +00:00
|
|
|
std::string token;
|
2023-01-26 11:36:44 +00:00
|
|
|
qpdf_offset_t token_start{0};
|
2023-06-01 13:12:39 +00:00
|
|
|
parser_state_e parser_state{ps_top};
|
2023-01-27 14:37:25 +00:00
|
|
|
std::vector<StackFrame> stack;
|
2022-01-17 23:40:38 +00:00
|
|
|
std::string dict_key;
|
2023-06-01 13:12:39 +00:00
|
|
|
qpdf_offset_t dict_key_offset{0};
|
2022-01-17 23:40:38 +00:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2022-05-20 00:28:13 +00:00
|
|
|
void
|
|
|
|
JSONParser::handle_u_code(
|
2023-01-27 18:58:50 +00:00
|
|
|
unsigned long codepoint,
|
2022-05-21 13:05:54 +00:00
|
|
|
qpdf_offset_t offset,
|
2022-05-20 00:28:13 +00:00
|
|
|
unsigned long& high_surrogate,
|
2022-05-21 13:05:54 +00:00
|
|
|
qpdf_offset_t& high_offset,
|
2022-05-20 00:28:13 +00:00
|
|
|
std::string& result)
|
|
|
|
{
|
|
|
|
if ((codepoint & 0xFC00) == 0xD800) {
|
|
|
|
// high surrogate
|
2023-01-27 18:58:50 +00:00
|
|
|
qpdf_offset_t new_high_offset = offset;
|
2022-05-20 00:28:13 +00:00
|
|
|
if (high_offset) {
|
|
|
|
QTC::TC("libtests", "JSON 16 high high");
|
|
|
|
throw std::runtime_error(
|
2022-09-21 16:49:21 +00:00
|
|
|
"JSON: offset " + std::to_string(new_high_offset) +
|
2022-05-20 00:28:13 +00:00
|
|
|
": UTF-16 high surrogate found after previous high surrogate at offset " +
|
2022-09-21 16:49:21 +00:00
|
|
|
std::to_string(high_offset));
|
2022-05-20 00:28:13 +00:00
|
|
|
}
|
|
|
|
high_offset = new_high_offset;
|
|
|
|
high_surrogate = codepoint;
|
|
|
|
} else if ((codepoint & 0xFC00) == 0xDC00) {
|
|
|
|
// low surrogate
|
2023-01-27 18:58:50 +00:00
|
|
|
if (offset != (high_offset + 6)) {
|
2022-05-20 00:28:13 +00:00
|
|
|
QTC::TC("libtests", "JSON 16 low not after high");
|
|
|
|
throw std::runtime_error(
|
2023-01-27 18:58:50 +00:00
|
|
|
"JSON: offset " + std::to_string(offset) +
|
2022-05-20 00:28:13 +00:00
|
|
|
": UTF-16 low surrogate found not immediately after high surrogate");
|
|
|
|
}
|
|
|
|
high_offset = 0;
|
|
|
|
codepoint = 0x10000U + ((high_surrogate & 0x3FFU) << 10U) + (codepoint & 0x3FF);
|
|
|
|
result += QUtil::toUTF8(codepoint);
|
|
|
|
} else {
|
|
|
|
result += QUtil::toUTF8(codepoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 00:44:16 +00:00
|
|
|
void
|
2023-01-29 12:23:15 +00:00
|
|
|
JSONParser::tokenError()
|
2022-01-17 23:40:38 +00:00
|
|
|
{
|
2023-01-29 15:21:29 +00:00
|
|
|
if (done) {
|
2023-01-29 12:23:15 +00:00
|
|
|
QTC::TC("libtests", "JSON parse ls premature end of input");
|
|
|
|
throw std::runtime_error("JSON: premature end of input");
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2023-01-29 15:21:29 +00:00
|
|
|
|
2023-01-29 12:23:15 +00:00
|
|
|
if (lex_state == ls_u4) {
|
|
|
|
QTC::TC("libtests", "JSON parse bad hex after u");
|
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset - u_count - 1) +
|
|
|
|
": \\u must be followed by four hex digits");
|
|
|
|
} else if (lex_state == ls_alpha) {
|
|
|
|
QTC::TC("libtests", "JSON parse keyword bad character");
|
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset) + ": keyword: unexpected character " +
|
|
|
|
std::string(p, 1));
|
2023-01-29 15:21:29 +00:00
|
|
|
} else if (lex_state == ls_string) {
|
|
|
|
QTC::TC("libtests", "JSON parse control char in string");
|
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset) +
|
|
|
|
": control character in string (missing \"?)");
|
2023-01-29 12:23:15 +00:00
|
|
|
} else if (lex_state == ls_backslash) {
|
|
|
|
QTC::TC("libtests", "JSON parse backslash bad character");
|
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset) +
|
|
|
|
": invalid character after backslash: " + std::string(p, 1));
|
|
|
|
}
|
|
|
|
|
2023-01-24 00:44:16 +00:00
|
|
|
if (*p == '.') {
|
2023-01-25 19:46:53 +00:00
|
|
|
if (lex_state == ls_number || lex_state == ls_number_e || lex_state == ls_number_e_sign) {
|
2023-01-25 19:14:36 +00:00
|
|
|
QTC::TC("libtests", "JSON parse point after e");
|
2023-01-24 00:44:16 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset) +
|
|
|
|
": numeric literal: decimal point after e");
|
2022-01-17 23:40:38 +00:00
|
|
|
} else {
|
2023-01-24 00:34:22 +00:00
|
|
|
QTC::TC("libtests", "JSON parse duplicate point");
|
2023-01-24 00:44:16 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset) +
|
|
|
|
": numeric literal: decimal point already seen");
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2023-01-24 01:36:38 +00:00
|
|
|
} else if (*p == 'e' || *p == 'E') {
|
2023-01-25 19:14:36 +00:00
|
|
|
QTC::TC("libtests", "JSON parse duplicate e");
|
2023-01-24 00:44:16 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset) + ": numeric literal: e already seen");
|
|
|
|
} else if ((*p == '+') || (*p == '-')) {
|
2023-01-23 19:51:27 +00:00
|
|
|
QTC::TC("libtests", "JSON parse unexpected sign");
|
2023-01-24 00:44:16 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset) + ": numeric literal: unexpected sign");
|
2023-01-23 20:08:11 +00:00
|
|
|
} else if (QUtil::is_space(*p) || strchr("{}[]:,", *p)) {
|
|
|
|
QTC::TC("libtests", "JSON parse incomplete number");
|
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset) + ": numeric literal: incomplete number");
|
2023-01-29 12:23:15 +00:00
|
|
|
|
2023-01-24 00:44:16 +00:00
|
|
|
} else {
|
|
|
|
QTC::TC("libtests", "JSON parse numeric bad character");
|
2022-05-20 00:28:13 +00:00
|
|
|
throw std::runtime_error(
|
2023-01-24 00:44:16 +00:00
|
|
|
"JSON: offset " + std::to_string(offset) + ": numeric literal: unexpected character " +
|
|
|
|
std::string(p, 1));
|
2022-05-20 00:28:13 +00:00
|
|
|
}
|
2023-01-29 15:21:29 +00:00
|
|
|
throw std::logic_error("JSON::tokenError : unhandled error");
|
2023-01-24 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 10:29:32 +00:00
|
|
|
// Append current character to token and advance to next input character.
|
|
|
|
inline void
|
|
|
|
JSONParser::append()
|
|
|
|
{
|
|
|
|
token += *p;
|
|
|
|
++p;
|
|
|
|
++offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append current character to token, advance to next input character and transition to 'next' lexer
|
|
|
|
// state.
|
|
|
|
inline void
|
|
|
|
JSONParser::append(lex_state_e next)
|
|
|
|
{
|
|
|
|
lex_state = next;
|
|
|
|
token += *p;
|
|
|
|
++p;
|
|
|
|
++offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to next input character without appending the current character to token.
|
|
|
|
inline void
|
|
|
|
JSONParser::ignore()
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
++offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to next input character without appending the current character to token and transition
|
|
|
|
// to 'next' lexer state.
|
|
|
|
inline void
|
|
|
|
JSONParser::ignore(lex_state_e next)
|
|
|
|
{
|
|
|
|
lex_state = next;
|
|
|
|
++p;
|
|
|
|
++offset;
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSONParser::getToken()
|
|
|
|
{
|
2022-05-04 13:12:57 +00:00
|
|
|
token.clear();
|
2023-01-27 18:58:50 +00:00
|
|
|
|
|
|
|
// Keep track of UTF-16 surrogate pairs.
|
|
|
|
unsigned long high_surrogate = 0;
|
|
|
|
qpdf_offset_t high_offset = 0;
|
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
while (true) {
|
2022-05-04 13:12:57 +00:00
|
|
|
if (p == (buf + bytes)) {
|
|
|
|
p = buf;
|
|
|
|
bytes = is.read(buf, sizeof(buf));
|
|
|
|
if (bytes == 0) {
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 11:39:15 +00:00
|
|
|
if ((*p < 32 && *p >= 0)) {
|
|
|
|
if (*p == '\t' || *p == '\n' || *p == '\r') {
|
|
|
|
// Legal white space not permitted in strings. This will always end the current
|
|
|
|
// token (unless we are still before the start of the token).
|
|
|
|
if (lex_state == ls_top) {
|
2023-02-01 10:29:32 +00:00
|
|
|
ignore();
|
2023-01-29 11:39:15 +00:00
|
|
|
} else {
|
2023-01-29 15:21:29 +00:00
|
|
|
break;
|
2023-01-29 11:39:15 +00:00
|
|
|
}
|
2023-01-29 15:21:29 +00:00
|
|
|
|
2022-01-17 23:40:38 +00:00
|
|
|
} else {
|
2023-01-29 11:39:15 +00:00
|
|
|
QTC::TC("libtests", "JSON parse null character");
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::runtime_error(
|
2023-01-29 11:39:15 +00:00
|
|
|
"JSON: control or null character at offset " + std::to_string(offset));
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2023-01-30 13:17:09 +00:00
|
|
|
} else if (*p == ',') {
|
|
|
|
if (lex_state == ls_top) {
|
2023-02-01 10:29:32 +00:00
|
|
|
ignore(ls_comma);
|
2023-01-30 13:17:09 +00:00
|
|
|
return;
|
|
|
|
} else if (lex_state == ls_string) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append();
|
2023-01-30 13:17:09 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (*p == ':') {
|
|
|
|
if (lex_state == ls_top) {
|
2023-02-01 10:29:32 +00:00
|
|
|
ignore(ls_colon);
|
2023-01-30 13:17:09 +00:00
|
|
|
return;
|
|
|
|
} else if (lex_state == ls_string) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append();
|
2023-01-30 13:17:09 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (*p == ' ') {
|
|
|
|
if (lex_state == ls_top) {
|
2023-02-01 10:29:32 +00:00
|
|
|
ignore();
|
2023-01-30 13:17:09 +00:00
|
|
|
} else if (lex_state == ls_string) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append();
|
2023-01-30 13:17:09 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (*p == '{') {
|
|
|
|
if (lex_state == ls_top) {
|
|
|
|
token_start = offset;
|
2023-02-01 10:29:32 +00:00
|
|
|
ignore(ls_begin_dict);
|
2023-01-30 13:17:09 +00:00
|
|
|
return;
|
|
|
|
} else if (lex_state == ls_string) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append();
|
2023-01-30 13:17:09 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (*p == '}') {
|
|
|
|
if (lex_state == ls_top) {
|
2023-02-01 10:29:32 +00:00
|
|
|
ignore(ls_end_dict);
|
2023-01-30 13:17:09 +00:00
|
|
|
return;
|
|
|
|
} else if (lex_state == ls_string) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append();
|
2023-01-30 13:17:09 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (*p == '[') {
|
|
|
|
if (lex_state == ls_top) {
|
|
|
|
token_start = offset;
|
2023-02-01 10:29:32 +00:00
|
|
|
ignore(ls_begin_array);
|
2023-01-30 13:17:09 +00:00
|
|
|
return;
|
|
|
|
} else if (lex_state == ls_string) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append();
|
2023-01-30 13:17:09 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (*p == ']') {
|
|
|
|
if (lex_state == ls_top) {
|
2023-02-01 10:29:32 +00:00
|
|
|
ignore(ls_end_array);
|
2023-01-30 13:17:09 +00:00
|
|
|
return;
|
|
|
|
} else if (lex_state == ls_string) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append();
|
2023-01-30 13:17:09 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2023-01-29 15:21:29 +00:00
|
|
|
} else {
|
|
|
|
switch (lex_state) {
|
|
|
|
case ls_top:
|
|
|
|
token_start = offset;
|
|
|
|
if (*p == '"') {
|
2023-02-01 10:29:32 +00:00
|
|
|
ignore(ls_string);
|
2023-01-29 15:21:29 +00:00
|
|
|
} else if ((*p >= 'a') && (*p <= 'z')) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_alpha);
|
2023-01-29 15:21:29 +00:00
|
|
|
} else if (*p == '-') {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_number_minus);
|
2023-01-29 15:21:29 +00:00
|
|
|
} else if ((*p >= '1') && (*p <= '9')) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_number_before_point);
|
2023-01-29 15:21:29 +00:00
|
|
|
} else if (*p == '0') {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_number_leading_zero);
|
2022-01-17 23:40:38 +00:00
|
|
|
} else {
|
2023-01-29 15:21:29 +00:00
|
|
|
QTC::TC("libtests", "JSON parse bad character");
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::runtime_error(
|
2022-09-21 16:49:21 +00:00
|
|
|
"JSON: offset " + std::to_string(offset) + ": unexpected character " +
|
2023-01-29 15:21:29 +00:00
|
|
|
std::string(p, 1));
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2023-01-29 15:21:29 +00:00
|
|
|
break;
|
2023-01-24 01:36:38 +00:00
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
case ls_number_minus:
|
|
|
|
if ((*p >= '1') && (*p <= '9')) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_number_before_point);
|
2023-01-29 15:21:29 +00:00
|
|
|
} else if (*p == '0') {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_number_leading_zero);
|
2023-01-29 15:21:29 +00:00
|
|
|
} else {
|
|
|
|
QTC::TC("libtests", "JSON parse number minus no digits");
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::runtime_error(
|
2022-09-21 16:49:21 +00:00
|
|
|
"JSON: offset " + std::to_string(offset) +
|
2023-01-29 15:21:29 +00:00
|
|
|
": numeric literal: no digit after minus sign");
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2023-01-29 15:21:29 +00:00
|
|
|
break;
|
2023-01-24 13:04:31 +00:00
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
case ls_number_leading_zero:
|
|
|
|
if (*p == '.') {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_number_point);
|
2023-01-29 15:21:29 +00:00
|
|
|
} else if (*p == 'e' || *p == 'E') {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_number_e);
|
2022-01-17 23:40:38 +00:00
|
|
|
} else {
|
2023-01-29 15:21:29 +00:00
|
|
|
QTC::TC("libtests", "JSON parse leading zero");
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::runtime_error(
|
2022-09-21 16:49:21 +00:00
|
|
|
"JSON: offset " + std::to_string(offset) + ": number with leading zero");
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2023-01-29 15:21:29 +00:00
|
|
|
break;
|
2022-01-17 23:40:38 +00:00
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
case ls_number_before_point:
|
|
|
|
if ((*p >= '0') && (*p <= '9')) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append();
|
2023-01-29 15:21:29 +00:00
|
|
|
} else if (*p == '.') {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_number_point);
|
2023-01-29 15:21:29 +00:00
|
|
|
} else if (*p == 'e' || *p == 'E') {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_number_e);
|
2023-01-29 15:21:29 +00:00
|
|
|
} else {
|
|
|
|
tokenError();
|
|
|
|
}
|
|
|
|
break;
|
2022-01-17 23:40:38 +00:00
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
case ls_number_point:
|
|
|
|
if ((*p >= '0') && (*p <= '9')) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_number_after_point);
|
2023-01-29 15:21:29 +00:00
|
|
|
} else {
|
|
|
|
tokenError();
|
|
|
|
}
|
|
|
|
break;
|
2022-01-17 23:40:38 +00:00
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
case ls_number_after_point:
|
|
|
|
if ((*p >= '0') && (*p <= '9')) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append();
|
2023-01-29 15:21:29 +00:00
|
|
|
} else if (*p == 'e' || *p == 'E') {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_number_e);
|
2023-01-29 15:21:29 +00:00
|
|
|
} else {
|
|
|
|
tokenError();
|
2023-01-27 18:58:50 +00:00
|
|
|
}
|
2023-01-29 15:21:29 +00:00
|
|
|
break;
|
2022-05-04 13:12:57 +00:00
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
case ls_number_e:
|
|
|
|
if ((*p >= '0') && (*p <= '9')) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_number);
|
2023-01-29 15:21:29 +00:00
|
|
|
} else if ((*p == '+') || (*p == '-')) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_number_e_sign);
|
2023-01-29 15:21:29 +00:00
|
|
|
} else {
|
|
|
|
tokenError();
|
|
|
|
}
|
2023-01-27 18:02:35 +00:00
|
|
|
break;
|
2023-01-29 15:21:29 +00:00
|
|
|
|
|
|
|
case ls_number_e_sign:
|
|
|
|
if ((*p >= '0') && (*p <= '9')) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append(ls_number);
|
2023-01-29 15:21:29 +00:00
|
|
|
} else {
|
|
|
|
tokenError();
|
|
|
|
}
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ls_number:
|
2023-01-29 15:21:29 +00:00
|
|
|
// We only get here after we have seen an exponent.
|
|
|
|
if ((*p >= '0') && (*p <= '9')) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append();
|
2023-01-29 15:21:29 +00:00
|
|
|
} else {
|
|
|
|
tokenError();
|
|
|
|
}
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
case ls_alpha:
|
|
|
|
if ((*p >= 'a') && (*p <= 'z')) {
|
2023-02-01 10:29:32 +00:00
|
|
|
append();
|
2023-01-29 15:21:29 +00:00
|
|
|
} else {
|
|
|
|
tokenError();
|
|
|
|
}
|
2023-01-27 18:02:35 +00:00
|
|
|
break;
|
2022-05-04 13:12:57 +00:00
|
|
|
|
2022-01-17 23:40:38 +00:00
|
|
|
case ls_string:
|
2023-01-29 15:21:29 +00:00
|
|
|
if (*p == '"') {
|
|
|
|
if (high_offset) {
|
|
|
|
QTC::TC("libtests", "JSON 16 dangling high");
|
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(high_offset) +
|
|
|
|
": UTF-16 high surrogate not followed by low surrogate");
|
|
|
|
}
|
2024-02-06 20:30:29 +00:00
|
|
|
ignore(ls_after_string);
|
2023-02-01 10:29:32 +00:00
|
|
|
return;
|
2023-01-29 15:21:29 +00:00
|
|
|
} else if (*p == '\\') {
|
2023-02-01 10:29:32 +00:00
|
|
|
ignore(ls_backslash);
|
|
|
|
} else {
|
|
|
|
append();
|
2023-01-29 15:21:29 +00:00
|
|
|
}
|
2023-01-27 18:02:35 +00:00
|
|
|
break;
|
2023-01-29 15:21:29 +00:00
|
|
|
|
2022-01-17 23:40:38 +00:00
|
|
|
case ls_backslash:
|
2023-01-29 15:21:29 +00:00
|
|
|
lex_state = ls_string;
|
|
|
|
switch (*p) {
|
|
|
|
case '\\':
|
|
|
|
case '\"':
|
|
|
|
case '/':
|
|
|
|
// \/ is allowed in json input, but so is /, so we don't map / to \/ in output.
|
|
|
|
token += *p;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
token += '\b';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
token += '\f';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
token += '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
token += '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
token += '\t';
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
lex_state = ls_u4;
|
|
|
|
u_count = 0;
|
|
|
|
u_value = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lex_state = ls_backslash;
|
|
|
|
tokenError();
|
|
|
|
}
|
2023-02-01 10:29:32 +00:00
|
|
|
ignore();
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
2023-01-29 15:21:29 +00:00
|
|
|
|
|
|
|
case ls_u4:
|
|
|
|
using ui = unsigned int;
|
2023-03-22 12:10:24 +00:00
|
|
|
if (ui val = ui(QUtil::hex_decode_char(*p)); val < 16) {
|
|
|
|
u_value = 16 * u_value + val;
|
2023-01-29 15:21:29 +00:00
|
|
|
} else {
|
|
|
|
tokenError();
|
|
|
|
}
|
|
|
|
if (++u_count == 4) {
|
|
|
|
handle_u_code(u_value, offset - 5, high_surrogate, high_offset, token);
|
|
|
|
lex_state = ls_string;
|
|
|
|
}
|
2023-02-01 10:29:32 +00:00
|
|
|
ignore();
|
2023-01-27 18:02:35 +00:00
|
|
|
break;
|
2023-01-29 15:21:29 +00:00
|
|
|
|
2023-01-27 18:02:35 +00:00
|
|
|
default:
|
2023-01-29 15:21:29 +00:00
|
|
|
throw std::logic_error("JSONParser::getToken : trying to handle delimiter state");
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
// We only get here if on end of input or if the last character was a control character or other
|
2023-01-30 13:17:09 +00:00
|
|
|
// delimiter.
|
2022-01-17 23:40:38 +00:00
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
if (!token.empty()) {
|
|
|
|
switch (lex_state) {
|
|
|
|
case ls_top:
|
|
|
|
// Can't happen
|
|
|
|
throw std::logic_error("tok_start set in ls_top while parsing");
|
|
|
|
break;
|
2023-01-26 09:55:34 +00:00
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
case ls_number_leading_zero:
|
|
|
|
case ls_number_before_point:
|
|
|
|
case ls_number_after_point:
|
|
|
|
lex_state = ls_number;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ls_number:
|
|
|
|
case ls_alpha:
|
|
|
|
// terminal state
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
tokenError();
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSONParser::handleToken()
|
|
|
|
{
|
2023-01-26 13:21:45 +00:00
|
|
|
if (lex_state == ls_top) {
|
2022-01-17 23:40:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parser_state == ps_done) {
|
|
|
|
QTC::TC("libtests", "JSON parse junk after object");
|
|
|
|
throw std::runtime_error(
|
2022-09-21 16:49:21 +00:00
|
|
|
"JSON: offset " + std::to_string(offset) +
|
2022-05-04 13:12:57 +00:00
|
|
|
": material follows end of object: " + token);
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
const static JSON null_item = JSON::makeNull();
|
|
|
|
JSON item;
|
|
|
|
auto tos = stack.empty() ? null_item : stack.back().item;
|
2023-01-27 13:35:02 +00:00
|
|
|
auto ls = lex_state;
|
|
|
|
lex_state = ls_top;
|
2022-01-17 23:40:38 +00:00
|
|
|
|
2023-01-27 13:35:02 +00:00
|
|
|
switch (ls) {
|
2023-01-26 13:21:45 +00:00
|
|
|
case ls_begin_dict:
|
2023-01-29 15:21:29 +00:00
|
|
|
item = JSON::makeDictionary();
|
2023-01-26 13:21:45 +00:00
|
|
|
break;
|
2022-01-17 23:40:38 +00:00
|
|
|
|
2023-01-26 13:21:45 +00:00
|
|
|
case ls_begin_array:
|
2023-01-29 15:21:29 +00:00
|
|
|
item = JSON::makeArray();
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
|
2023-01-26 13:21:45 +00:00
|
|
|
case ls_colon:
|
2023-01-26 14:10:48 +00:00
|
|
|
if (parser_state != ps_dict_after_key) {
|
|
|
|
QTC::TC("libtests", "JSON parse unexpected :");
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::runtime_error(
|
2022-09-21 16:49:21 +00:00
|
|
|
"JSON: offset " + std::to_string(offset) + ": unexpected colon");
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2023-01-27 12:26:27 +00:00
|
|
|
parser_state = ps_dict_after_colon;
|
|
|
|
return;
|
2023-01-26 14:10:48 +00:00
|
|
|
|
2023-01-26 13:21:45 +00:00
|
|
|
case ls_comma:
|
2023-01-26 14:10:48 +00:00
|
|
|
if (!((parser_state == ps_dict_after_item) || (parser_state == ps_array_after_item))) {
|
|
|
|
QTC::TC("libtests", "JSON parse unexpected ,");
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::runtime_error(
|
2022-09-21 16:49:21 +00:00
|
|
|
"JSON: offset " + std::to_string(offset) + ": unexpected comma");
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2023-01-27 12:26:27 +00:00
|
|
|
if (parser_state == ps_dict_after_item) {
|
|
|
|
parser_state = ps_dict_after_comma;
|
|
|
|
} else if (parser_state == ps_array_after_item) {
|
|
|
|
parser_state = ps_array_after_comma;
|
|
|
|
} else {
|
|
|
|
throw std::logic_error("JSONParser::handleToken: unexpected parser state for comma");
|
|
|
|
}
|
|
|
|
return;
|
2023-01-26 14:10:48 +00:00
|
|
|
|
2023-01-26 13:21:45 +00:00
|
|
|
case ls_end_array:
|
2023-01-27 12:50:14 +00:00
|
|
|
if (!(parser_state == ps_array_begin || parser_state == ps_array_after_item)) {
|
2023-01-26 14:10:48 +00:00
|
|
|
QTC::TC("libtests", "JSON parse unexpected ]");
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::runtime_error(
|
2022-09-21 16:49:21 +00:00
|
|
|
"JSON: offset " + std::to_string(offset) + ": unexpected array end delimiter");
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2023-01-27 14:37:25 +00:00
|
|
|
parser_state = stack.back().state;
|
2023-01-29 15:21:29 +00:00
|
|
|
tos.setEnd(offset);
|
2023-01-27 12:50:14 +00:00
|
|
|
if (reactor) {
|
2023-01-29 15:21:29 +00:00
|
|
|
reactor->containerEnd(tos);
|
2023-01-27 12:50:14 +00:00
|
|
|
}
|
|
|
|
if (parser_state != ps_done) {
|
|
|
|
stack.pop_back();
|
|
|
|
}
|
|
|
|
return;
|
2023-01-26 14:10:48 +00:00
|
|
|
|
2023-01-26 13:21:45 +00:00
|
|
|
case ls_end_dict:
|
2023-01-26 14:10:48 +00:00
|
|
|
if (!((parser_state == ps_dict_begin) || (parser_state == ps_dict_after_item))) {
|
|
|
|
QTC::TC("libtests", "JSON parse unexpected }");
|
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset) + ": unexpected dictionary end delimiter");
|
|
|
|
}
|
2023-01-27 14:37:25 +00:00
|
|
|
parser_state = stack.back().state;
|
2023-01-29 15:21:29 +00:00
|
|
|
tos.setEnd(offset);
|
2023-01-27 12:50:14 +00:00
|
|
|
if (reactor) {
|
2023-01-29 15:21:29 +00:00
|
|
|
reactor->containerEnd(tos);
|
2023-01-27 12:50:14 +00:00
|
|
|
}
|
|
|
|
if (parser_state != ps_done) {
|
|
|
|
stack.pop_back();
|
|
|
|
}
|
|
|
|
return;
|
2022-01-17 23:40:38 +00:00
|
|
|
|
|
|
|
case ls_number:
|
2023-01-29 15:21:29 +00:00
|
|
|
item = JSON::makeNumber(token);
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ls_alpha:
|
2022-05-04 13:12:57 +00:00
|
|
|
if (token == "true") {
|
2023-01-29 15:21:29 +00:00
|
|
|
item = JSON::makeBool(true);
|
2022-05-04 13:12:57 +00:00
|
|
|
} else if (token == "false") {
|
2023-01-29 15:21:29 +00:00
|
|
|
item = JSON::makeBool(false);
|
2022-05-04 13:12:57 +00:00
|
|
|
} else if (token == "null") {
|
2023-01-29 15:21:29 +00:00
|
|
|
item = JSON::makeNull();
|
2022-01-17 23:40:38 +00:00
|
|
|
} else {
|
|
|
|
QTC::TC("libtests", "JSON parse invalid keyword");
|
|
|
|
throw std::runtime_error(
|
2022-09-21 16:49:21 +00:00
|
|
|
"JSON: offset " + std::to_string(offset) + ": invalid keyword " + token);
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2024-02-06 20:30:29 +00:00
|
|
|
case ls_after_string:
|
2023-01-27 13:35:02 +00:00
|
|
|
if (parser_state == ps_dict_begin || parser_state == ps_dict_after_comma) {
|
2023-01-27 17:03:24 +00:00
|
|
|
dict_key = token;
|
2023-01-27 13:35:02 +00:00
|
|
|
dict_key_offset = token_start;
|
|
|
|
parser_state = ps_dict_after_key;
|
|
|
|
return;
|
|
|
|
} else {
|
2023-01-29 15:21:29 +00:00
|
|
|
item = JSON::makeString(token);
|
2023-01-27 13:35:02 +00:00
|
|
|
}
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
|
2023-01-26 09:55:34 +00:00
|
|
|
default:
|
2024-02-04 22:27:43 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset) + ": premature end of input");
|
2022-01-17 23:40:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
item.setStart(token_start);
|
|
|
|
item.setEnd(offset);
|
2022-01-17 23:40:38 +00:00
|
|
|
|
2023-01-27 12:50:14 +00:00
|
|
|
switch (parser_state) {
|
|
|
|
case ps_dict_begin:
|
|
|
|
case ps_dict_after_comma:
|
2023-01-27 13:35:02 +00:00
|
|
|
QTC::TC("libtests", "JSON parse string as dict key");
|
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset) + ": expect string as dictionary key");
|
2023-01-27 12:50:14 +00:00
|
|
|
break;
|
2022-01-17 23:40:38 +00:00
|
|
|
|
2023-01-27 12:50:14 +00:00
|
|
|
case ps_dict_after_colon:
|
2023-01-29 15:21:29 +00:00
|
|
|
if (tos.checkDictionaryKeySeen(dict_key)) {
|
2023-01-27 12:50:14 +00:00
|
|
|
QTC::TC("libtests", "JSON parse duplicate key");
|
2022-01-17 23:40:38 +00:00
|
|
|
throw std::runtime_error(
|
2023-01-27 12:50:14 +00:00
|
|
|
"JSON: offset " + std::to_string(dict_key_offset) + ": duplicated dictionary key");
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2023-01-29 15:21:29 +00:00
|
|
|
if (!reactor || !reactor->dictionaryItem(dict_key, item)) {
|
|
|
|
tos.addDictionaryMember(dict_key, item);
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2023-01-27 15:39:16 +00:00
|
|
|
parser_state = ps_dict_after_item;
|
2023-01-27 12:50:14 +00:00
|
|
|
break;
|
2022-01-17 23:40:38 +00:00
|
|
|
|
2023-01-27 12:50:14 +00:00
|
|
|
case ps_array_begin:
|
|
|
|
case ps_array_after_comma:
|
2023-01-29 15:21:29 +00:00
|
|
|
if (!reactor || !reactor->arrayItem(item)) {
|
|
|
|
tos.addArrayElement(item);
|
2022-05-01 18:06:31 +00:00
|
|
|
}
|
2023-01-27 15:39:16 +00:00
|
|
|
parser_state = ps_array_after_item;
|
2023-01-27 12:50:14 +00:00
|
|
|
break;
|
2022-05-01 18:06:31 +00:00
|
|
|
|
2023-01-27 12:50:14 +00:00
|
|
|
case ps_top:
|
2023-01-29 15:21:29 +00:00
|
|
|
if (!(item.isDictionary() || item.isArray())) {
|
2023-05-27 22:49:18 +00:00
|
|
|
stack.emplace_back(ps_done, item);
|
2023-01-27 15:39:16 +00:00
|
|
|
parser_state = ps_done;
|
|
|
|
return;
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2023-01-27 15:39:16 +00:00
|
|
|
parser_state = ps_done;
|
2023-01-27 12:50:14 +00:00
|
|
|
break;
|
2022-01-17 23:40:38 +00:00
|
|
|
|
2023-01-27 12:50:14 +00:00
|
|
|
case ps_dict_after_key:
|
2023-01-27 13:08:55 +00:00
|
|
|
QTC::TC("libtests", "JSON parse expected colon");
|
|
|
|
throw std::runtime_error("JSON: offset " + std::to_string(offset) + ": expected ':'");
|
|
|
|
break;
|
2022-01-17 23:40:38 +00:00
|
|
|
|
2023-01-27 12:50:14 +00:00
|
|
|
case ps_dict_after_item:
|
2023-01-27 13:08:55 +00:00
|
|
|
QTC::TC("libtests", "JSON parse expected , or }");
|
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset) + ": expected ',' or '}'");
|
|
|
|
break;
|
2022-01-17 23:40:38 +00:00
|
|
|
|
2023-01-27 12:50:14 +00:00
|
|
|
case ps_array_after_item:
|
2023-01-27 13:08:55 +00:00
|
|
|
QTC::TC("libtests", "JSON parse expected, or ]");
|
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset) + ": expected ',' or ']'");
|
|
|
|
break;
|
2022-01-17 23:40:38 +00:00
|
|
|
|
2023-01-27 12:50:14 +00:00
|
|
|
case ps_done:
|
|
|
|
throw std::logic_error("JSONParser::handleToken: unexpected parser state");
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
if (item.isDictionary() || item.isArray()) {
|
2023-05-27 22:49:18 +00:00
|
|
|
stack.emplace_back(parser_state, item);
|
2022-05-14 14:11:52 +00:00
|
|
|
// 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.
|
2023-01-29 15:21:29 +00:00
|
|
|
if (item.isDictionary()) {
|
2023-01-27 14:37:25 +00:00
|
|
|
if (reactor) {
|
|
|
|
reactor->dictionaryStart();
|
|
|
|
}
|
2023-01-27 15:39:16 +00:00
|
|
|
parser_state = ps_dict_begin;
|
2023-01-29 15:21:29 +00:00
|
|
|
} else if (item.isArray()) {
|
2023-01-27 14:37:25 +00:00
|
|
|
if (reactor) {
|
|
|
|
reactor->arrayStart();
|
|
|
|
}
|
2023-01-27 15:39:16 +00:00
|
|
|
parser_state = ps_array_begin;
|
2022-05-14 14:11:52 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 14:37:25 +00:00
|
|
|
if (stack.size() > 500) {
|
2023-01-27 14:37:25 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"JSON: offset " + std::to_string(offset) + ": maximum object depth exceeded");
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 15:21:29 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
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");
|
|
|
|
}
|
2023-01-27 14:37:25 +00:00
|
|
|
auto const& tos = stack.back().item;
|
2023-01-29 15:21:29 +00:00
|
|
|
if (reactor && !(tos.isArray() || tos.isDictionary())) {
|
2022-05-01 18:06:31 +00:00
|
|
|
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);
|
2023-01-29 15:21:29 +00:00
|
|
|
return jp.parse();
|
2022-05-04 13:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2023-01-29 15:21:29 +00:00
|
|
|
return jp.parse();
|
2022-01-17 23:40:38 +00:00
|
|
|
}
|
2022-05-01 18:06:31 +00:00
|
|
|
|
|
|
|
void
|
2022-05-21 13:05:54 +00:00
|
|
|
JSON::setStart(qpdf_offset_t start)
|
2022-05-01 18:06:31 +00:00
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
if (m) {
|
|
|
|
m->start = start;
|
|
|
|
}
|
2022-05-01 18:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-05-21 13:05:54 +00:00
|
|
|
JSON::setEnd(qpdf_offset_t end)
|
2022-05-01 18:06:31 +00:00
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
if (m) {
|
|
|
|
m->end = end;
|
|
|
|
}
|
2022-05-01 18:06:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 13:05:54 +00:00
|
|
|
qpdf_offset_t
|
2022-05-01 18:06:31 +00:00
|
|
|
JSON::getStart() const
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
return m ? m->start : 0;
|
2022-05-01 18:06:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 13:05:54 +00:00
|
|
|
qpdf_offset_t
|
2022-05-01 18:06:31 +00:00
|
|
|
JSON::getEnd() const
|
|
|
|
{
|
2024-01-14 16:38:29 +00:00
|
|
|
return m ? m->end : 0;
|
2022-05-01 18:06:31 +00:00
|
|
|
}
|