mirror of
https://github.com/qpdf/qpdf.git
synced 2024-10-31 19:02:30 +00:00
Reimplement QPDFObjectHandle::getJSON in terms of writeJSON
This commit is contained in:
parent
9379b76811
commit
9589fad1e5
@ -1618,7 +1618,11 @@ QPDFObjectHandle::getJSON(int json_version, bool dereference_indirect)
|
||||
} else if (!dereference()) {
|
||||
throw std::logic_error("attempted to dereference an uninitialized QPDFObjectHandle");
|
||||
} else {
|
||||
return obj->getJSON(json_version);
|
||||
Pl_Buffer p{"json"};
|
||||
JSON::Writer jw{&p, 0};
|
||||
writeJSON(json_version, jw, dereference_indirect);
|
||||
p.finish();
|
||||
return JSON::parse(p.getString());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -149,38 +149,6 @@ QPDF_Array::unparse()
|
||||
return result;
|
||||
}
|
||||
|
||||
JSON
|
||||
QPDF_Array::getJSON(int json_version)
|
||||
{
|
||||
static const JSON j_null = JSON::makeNull();
|
||||
JSON j_array = JSON::makeArray();
|
||||
if (sp) {
|
||||
int next = 0;
|
||||
for (auto& item: sp->elements) {
|
||||
int key = item.first;
|
||||
for (int j = next; j < key; ++j) {
|
||||
j_array.addArrayElement(j_null);
|
||||
}
|
||||
auto og = item.second->getObjGen();
|
||||
j_array.addArrayElement(
|
||||
og.isIndirect() ? JSON::makeString(og.unparse(' ') + " R")
|
||||
: item.second->getJSON(json_version));
|
||||
next = ++key;
|
||||
}
|
||||
for (int j = next; j < sp->size; ++j) {
|
||||
j_array.addArrayElement(j_null);
|
||||
}
|
||||
} else {
|
||||
for (auto const& item: elements) {
|
||||
auto og = item->getObjGen();
|
||||
j_array.addArrayElement(
|
||||
og.isIndirect() ? JSON::makeString(og.unparse(' ') + " R")
|
||||
: item->getJSON(json_version));
|
||||
}
|
||||
}
|
||||
return j_array;
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_Array::writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -26,12 +26,6 @@ QPDF_Bool::unparse()
|
||||
return (val ? "true" : "false");
|
||||
}
|
||||
|
||||
JSON
|
||||
QPDF_Bool::getJSON(int json_version)
|
||||
{
|
||||
return JSON::makeBool(this->val);
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_Bool::writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -28,13 +28,6 @@ QPDF_Destroyed::unparse()
|
||||
return "";
|
||||
}
|
||||
|
||||
JSON
|
||||
QPDF_Destroyed::getJSON(int json_version)
|
||||
{
|
||||
throw std::logic_error("attempted to get JSON from a QPDFObjectHandle from a destroyed QPDF");
|
||||
return JSON::makeNull();
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_Destroyed::writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -68,26 +68,6 @@ QPDF_Dictionary::unparse()
|
||||
return result;
|
||||
}
|
||||
|
||||
JSON
|
||||
QPDF_Dictionary::getJSON(int json_version)
|
||||
{
|
||||
JSON j = JSON::makeDictionary();
|
||||
for (auto& iter: this->items) {
|
||||
if (!iter.second.isNull()) {
|
||||
if (json_version == 1) {
|
||||
j.addDictionaryMember(
|
||||
QPDF_Name::normalizeName(iter.first), iter.second.getJSON(json_version));
|
||||
} else if (auto res = QPDF_Name::analyzeJSONEncoding(iter.first); res.first) {
|
||||
j.addDictionaryMember(iter.first, iter.second.getJSON(json_version));
|
||||
} else {
|
||||
j.addDictionaryMember(
|
||||
"n:" + QPDF_Name::normalizeName(iter.first), iter.second.getJSON(json_version));
|
||||
}
|
||||
}
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_Dictionary::writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -26,12 +26,6 @@ QPDF_InlineImage::unparse()
|
||||
return this->val;
|
||||
}
|
||||
|
||||
JSON
|
||||
QPDF_InlineImage::getJSON(int json_version)
|
||||
{
|
||||
return JSON::makeNull();
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_InlineImage::writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -27,12 +27,6 @@ QPDF_Integer::unparse()
|
||||
return std::to_string(this->val);
|
||||
}
|
||||
|
||||
JSON
|
||||
QPDF_Integer::getJSON(int json_version)
|
||||
{
|
||||
return JSON::makeInt(this->val);
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_Integer::writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -103,20 +103,6 @@ QPDF_Name::analyzeJSONEncoding(const std::string& name)
|
||||
return {tail == 0, !needs_escaping};
|
||||
}
|
||||
|
||||
JSON
|
||||
QPDF_Name::getJSON(int json_version)
|
||||
{
|
||||
if (json_version == 1) {
|
||||
return JSON::makeString(normalizeName(this->name));
|
||||
} else {
|
||||
if (auto res = analyzeJSONEncoding(name); res.first) {
|
||||
return JSON::makeString(name);
|
||||
} else {
|
||||
return JSON::makeString("n:" + normalizeName(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_Name::writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -44,13 +44,6 @@ QPDF_Null::unparse()
|
||||
return "null";
|
||||
}
|
||||
|
||||
JSON
|
||||
QPDF_Null::getJSON(int json_version)
|
||||
{
|
||||
// If this is updated, QPDF_Array::getJSON must also be updated.
|
||||
return JSON::makeNull();
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_Null::writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -26,12 +26,6 @@ QPDF_Operator::unparse()
|
||||
return val;
|
||||
}
|
||||
|
||||
JSON
|
||||
QPDF_Operator::getJSON(int json_version)
|
||||
{
|
||||
return JSON::makeNull();
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_Operator::writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -39,25 +39,6 @@ QPDF_Real::unparse()
|
||||
return this->val;
|
||||
}
|
||||
|
||||
JSON
|
||||
QPDF_Real::getJSON(int json_version)
|
||||
{
|
||||
// While PDF allows .x or -.x, JSON does not. Rather than converting from string to double and
|
||||
// back, just handle this as a special case for JSON.
|
||||
std::string result;
|
||||
if (this->val.length() == 0) {
|
||||
// Can't really happen...
|
||||
result = "0";
|
||||
} else if (this->val.at(0) == '.') {
|
||||
result = "0" + this->val;
|
||||
} else if ((this->val.length() >= 2) && (this->val.at(0) == '-') && (this->val.at(1) == '.')) {
|
||||
result = "-0." + this->val.substr(2);
|
||||
} else {
|
||||
result = this->val;
|
||||
}
|
||||
return JSON::makeNumber(result);
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_Real::writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -26,13 +26,6 @@ QPDF_Reserved::unparse()
|
||||
return "";
|
||||
}
|
||||
|
||||
JSON
|
||||
QPDF_Reserved::getJSON(int json_version)
|
||||
{
|
||||
throw std::logic_error("QPDFObjectHandle: attempting to get JSON from a reserved object");
|
||||
return JSON::makeNull();
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_Reserved::writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -177,15 +177,6 @@ QPDF_Stream::unparse()
|
||||
return og.unparse(' ') + " R";
|
||||
}
|
||||
|
||||
JSON
|
||||
QPDF_Stream::getJSON(int json_version)
|
||||
{
|
||||
if (json_version == 1) {
|
||||
return this->stream_dict.getJSON(json_version);
|
||||
}
|
||||
return getStreamJSON(json_version, qpdf_sj_none, qpdf_dl_none, nullptr, "");
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_Stream::writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -46,35 +46,6 @@ QPDF_String::unparse()
|
||||
return unparse(false);
|
||||
}
|
||||
|
||||
JSON
|
||||
QPDF_String::getJSON(int json_version)
|
||||
{
|
||||
if (json_version == 1) {
|
||||
return JSON::makeString(getUTF8Val());
|
||||
}
|
||||
// See if we can unambiguously represent as Unicode.
|
||||
bool is_unicode = false;
|
||||
std::string result;
|
||||
std::string candidate = getUTF8Val();
|
||||
if (QUtil::is_utf16(this->val) || QUtil::is_explicit_utf8(this->val)) {
|
||||
is_unicode = true;
|
||||
result = candidate;
|
||||
} else if (!useHexString()) {
|
||||
std::string test;
|
||||
if (QUtil::utf8_to_pdf_doc(candidate, test, '?') && (test == this->val)) {
|
||||
// This is a PDF-doc string that can be losslessly encoded as Unicode.
|
||||
is_unicode = true;
|
||||
result = candidate;
|
||||
}
|
||||
}
|
||||
if (is_unicode) {
|
||||
result = "u:" + result;
|
||||
} else {
|
||||
result = "b:" + QUtil::hex_encode(this->val);
|
||||
}
|
||||
return JSON::makeString(result);
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_String::writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -27,13 +27,6 @@ QPDF_Unresolved::unparse()
|
||||
return "";
|
||||
}
|
||||
|
||||
JSON
|
||||
QPDF_Unresolved::getJSON(int json_version)
|
||||
{
|
||||
throw std::logic_error("attempted to get JSON from an unresolved QPDFObjectHandle");
|
||||
return JSON::makeNull();
|
||||
}
|
||||
|
||||
void
|
||||
QPDF_Unresolved::writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -33,11 +33,6 @@ class QPDFObject
|
||||
{
|
||||
return value->unparse();
|
||||
}
|
||||
JSON
|
||||
getJSON(int json_version)
|
||||
{
|
||||
return value->getJSON(json_version);
|
||||
}
|
||||
void
|
||||
writeJSON(int json_version, JSON::Writer& p)
|
||||
{
|
||||
|
@ -24,7 +24,6 @@ class QPDFValue: public std::enable_shared_from_this<QPDFValue>
|
||||
|
||||
virtual std::shared_ptr<QPDFObject> copy(bool shallow = false) = 0;
|
||||
virtual std::string unparse() = 0;
|
||||
virtual JSON getJSON(int json_version) = 0;
|
||||
virtual void writeJSON(int json_version, JSON::Writer& p) = 0;
|
||||
|
||||
struct JSON_Descr
|
||||
|
@ -22,7 +22,6 @@ class QPDF_Array: public QPDFValue
|
||||
create(std::vector<std::shared_ptr<QPDFObject>>&& items, bool sparse);
|
||||
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
|
||||
std::string unparse() override;
|
||||
JSON getJSON(int json_version) override;
|
||||
void writeJSON(int json_version, JSON::Writer& p) override;
|
||||
void disconnect() override;
|
||||
|
||||
|
@ -10,7 +10,6 @@ class QPDF_Bool: public QPDFValue
|
||||
static std::shared_ptr<QPDFObject> create(bool val);
|
||||
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
|
||||
std::string unparse() override;
|
||||
JSON getJSON(int json_version) override;
|
||||
void writeJSON(int json_version, JSON::Writer& p) override;
|
||||
|
||||
bool getVal() const;
|
||||
|
@ -9,7 +9,6 @@ class QPDF_Destroyed: public QPDFValue
|
||||
~QPDF_Destroyed() override = default;
|
||||
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
|
||||
std::string unparse() override;
|
||||
JSON getJSON(int json_version) override;
|
||||
void writeJSON(int json_version, JSON::Writer& p) override;
|
||||
static std::shared_ptr<QPDFValue> getInstance();
|
||||
|
||||
|
@ -16,7 +16,6 @@ class QPDF_Dictionary: public QPDFValue
|
||||
static std::shared_ptr<QPDFObject> create(std::map<std::string, QPDFObjectHandle>&& items);
|
||||
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
|
||||
std::string unparse() override;
|
||||
JSON getJSON(int json_version) override;
|
||||
void writeJSON(int json_version, JSON::Writer& p) override;
|
||||
void disconnect() override;
|
||||
|
||||
|
@ -10,7 +10,6 @@ class QPDF_InlineImage: public QPDFValue
|
||||
static std::shared_ptr<QPDFObject> create(std::string const& val);
|
||||
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
|
||||
std::string unparse() override;
|
||||
JSON getJSON(int json_version) override;
|
||||
void writeJSON(int json_version, JSON::Writer& p) override;
|
||||
std::string
|
||||
getStringValue() const override
|
||||
|
@ -10,7 +10,6 @@ class QPDF_Integer: public QPDFValue
|
||||
static std::shared_ptr<QPDFObject> create(long long value);
|
||||
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
|
||||
std::string unparse() override;
|
||||
JSON getJSON(int json_version) override;
|
||||
void writeJSON(int json_version, JSON::Writer& p) override;
|
||||
long long getVal() const;
|
||||
|
||||
|
@ -10,7 +10,6 @@ class QPDF_Name: public QPDFValue
|
||||
static std::shared_ptr<QPDFObject> create(std::string const& name);
|
||||
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
|
||||
std::string unparse() override;
|
||||
JSON getJSON(int json_version) override;
|
||||
void writeJSON(int json_version, JSON::Writer& p) override;
|
||||
|
||||
// Put # into strings with characters unsuitable for name token
|
||||
|
@ -18,7 +18,6 @@ class QPDF_Null: public QPDFValue
|
||||
std::string var_descr);
|
||||
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
|
||||
std::string unparse() override;
|
||||
JSON getJSON(int json_version) override;
|
||||
void writeJSON(int json_version, JSON::Writer& p) override;
|
||||
|
||||
private:
|
||||
|
@ -10,7 +10,6 @@ class QPDF_Operator: public QPDFValue
|
||||
static std::shared_ptr<QPDFObject> create(std::string const& val);
|
||||
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
|
||||
std::string unparse() override;
|
||||
JSON getJSON(int json_version) override;
|
||||
void writeJSON(int json_version, JSON::Writer& p) override;
|
||||
std::string
|
||||
getStringValue() const override
|
||||
|
@ -12,7 +12,6 @@ class QPDF_Real: public QPDFValue
|
||||
create(double value, int decimal_places, bool trim_trailing_zeroes);
|
||||
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
|
||||
std::string unparse() override;
|
||||
JSON getJSON(int json_version) override;
|
||||
void writeJSON(int json_version, JSON::Writer& p) override;
|
||||
std::string
|
||||
getStringValue() const override
|
||||
|
@ -10,7 +10,6 @@ class QPDF_Reserved: public QPDFValue
|
||||
static std::shared_ptr<QPDFObject> create();
|
||||
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
|
||||
std::string unparse() override;
|
||||
JSON getJSON(int json_version) override;
|
||||
void writeJSON(int json_version, JSON::Writer& p) override;
|
||||
|
||||
private:
|
||||
|
@ -25,7 +25,6 @@ class QPDF_Stream: public QPDFValue
|
||||
size_t length);
|
||||
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
|
||||
std::string unparse() override;
|
||||
JSON getJSON(int json_version) override;
|
||||
void writeJSON(int json_version, JSON::Writer& p) override;
|
||||
void setDescription(
|
||||
QPDF*, std::shared_ptr<QPDFValue::Description>& description, qpdf_offset_t offset) override;
|
||||
|
@ -16,7 +16,6 @@ class QPDF_String: public QPDFValue
|
||||
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
|
||||
std::string unparse() override;
|
||||
std::string unparse(bool force_binary);
|
||||
JSON getJSON(int json_version) override;
|
||||
void writeJSON(int json_version, JSON::Writer& p) override;
|
||||
std::string getUTF8Val() const;
|
||||
std::string
|
||||
|
@ -10,7 +10,6 @@ class QPDF_Unresolved: public QPDFValue
|
||||
static std::shared_ptr<QPDFObject> create(QPDF* qpdf, QPDFObjGen const& og);
|
||||
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
|
||||
std::string unparse() override;
|
||||
JSON getJSON(int json_version) override;
|
||||
void writeJSON(int json_version, JSON::Writer& p) override;
|
||||
|
||||
private:
|
||||
|
Loading…
Reference in New Issue
Block a user