2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-05-29 08:20:53 +00:00

Prepare code for JSON v2

Update getJSON() methods and calls to them
This commit is contained in:
Jay Berkenbilt 2022-05-07 07:53:45 -04:00
parent a9fbbd5dca
commit 16f4f94cd9
55 changed files with 177 additions and 99 deletions

2
TODO
View File

@ -50,8 +50,6 @@ Output JSON v2
General things to remember: General things to remember:
* deprecate getJSON without a version
* Make sure all the information from --check and other informational * Make sure all the information from --check and other informational
options (--show-linearization, --show-encryption, --show-xref, options (--show-linearization, --show-encryption, --show-xref,
--list-attachments, --show-npages) is available in the json output. --list-attachments, --show-npages) is available in the json output.

View File

@ -142,7 +142,7 @@ FuzzHelper::testPages()
page.getImages(); page.getImages();
pldh.getLabelForPage(pageno); pldh.getLabelForPage(pageno);
QPDFObjectHandle page_obj(page.getObjectHandle()); QPDFObjectHandle page_obj(page.getObjectHandle());
page_obj.getJSON(true).unparse(); page_obj.getJSON(JSON::LATEST, true).unparse();
odh.getOutlinesForPage(page_obj.getObjGen()); odh.getOutlinesForPage(page_obj.getObjGen());
std::vector<QPDFAnnotationObjectHelper> annotations = std::vector<QPDFAnnotationObjectHelper> annotations =

View File

@ -51,6 +51,8 @@ class InputSource;
class JSON class JSON
{ {
public: public:
static int constexpr LATEST = 2;
QPDF_DLL QPDF_DLL
std::string unparse() const; std::string unparse() const;

View File

@ -26,6 +26,7 @@
#include <qpdf/DLL.h> #include <qpdf/DLL.h>
#include <qpdf/PDFVersion.hh> #include <qpdf/PDFVersion.hh>
#include <qpdf/QPDF.hh> #include <qpdf/QPDF.hh>
#include <qpdf/QPDFOutlineObjectHelper.hh>
#include <qpdf/QPDFPageObjectHelper.hh> #include <qpdf/QPDFPageObjectHelper.hh>
#include <functional> #include <functional>
@ -520,6 +521,10 @@ class QPDFJob
void doJSONAcroform(Pipeline* p, bool& first, QPDF& pdf); void doJSONAcroform(Pipeline* p, bool& first, QPDF& pdf);
void doJSONEncrypt(Pipeline* p, bool& first, QPDF& pdf); void doJSONEncrypt(Pipeline* p, bool& first, QPDF& pdf);
void doJSONAttachments(Pipeline* p, bool& first, QPDF& pdf); void doJSONAttachments(Pipeline* p, bool& first, QPDF& pdf);
void addOutlinesToJson(
std::vector<QPDFOutlineObjectHelper> outlines,
JSON& j,
std::map<QPDFObjGen, int>& page_numbers);
enum remove_unref_e { re_auto, re_yes, re_no }; enum remove_unref_e { re_auto, re_yes, re_no };

View File

@ -64,7 +64,7 @@ class QPDFObject
virtual ~QPDFObject() = default; virtual ~QPDFObject() = default;
virtual std::string unparse() = 0; virtual std::string unparse() = 0;
virtual JSON getJSON() = 0; virtual JSON getJSON(int json_version) = 0;
// Return a unique type code for the object // Return a unique type code for the object
virtual object_type_e getTypeCode() const = 0; virtual object_type_e getTypeCode() const = 0;

View File

@ -1304,15 +1304,40 @@ class QPDFObjectHandle
QPDF_DLL QPDF_DLL
std::string unparseBinary(); std::string unparseBinary();
// Return encoded as JSON. For most object types, there is an // Return encoded as JSON. The constant JSON::LATEST can be used
// obvious mapping. The JSON is generated as follows: // to specify the latest available JSON version. The JSON is
// * Names are encoded as strings representing the normalized name // generated as follows:
// in PDF syntax as returned by unparse() // * Arrays, dictionaries, booleans, nulls, integers, and real
// numbers are represented by their native JSON types.
// * Names are encoded as strings representing the canonical
// representation (after parsing #xx) and preceded by a slash,
// just as unparse() returns. For example, the JSON for the
// PDF-syntax name /Text#2fPlain would be "/Text/Plain".
// * Indirect references are encoded as strings containing "obj gen R" // * Indirect references are encoded as strings containing "obj gen R"
// * Strings are encoded as UTF-8 strings with unrepresentable binary // * Strings
// characters encoded as \uHHHH // * JSON v1: Strings are encoded as UTF-8 strings with
// * Encoding streams just encodes the stream's dictionary; the stream // unrepresentable binary characters encoded as \uHHHH.
// data is not represented // Characters in PDF Doc encoding that don't have
// bidirectional unicode mappings are not reversible. There is
// no way to tell the difference between a string that looks
// like a name or indirect object from an actual name or
// indirect object.
// * JSON v2:
// * Unicode strings and strings encoded with PDF Doc encoding
// that can be bidrectionally mapped two Unicode (which is
// all strings without undefined characters) are represented
// as "u:" followed by the UTF-8 encoded string. Example:
// "u:potato".
// * All other strings are represented as "b:" followed by a
// hexadecimal encoding of the string. Example: "b:0102cacb"
// * Streams
// * JSON v1: Only the stream's dictionary is encoded. There is
// no way tell a stream from a dictionary other than context.
// * JSON v2: A stream is encoded as {"dict": {...}} with the
// value being the encoding of the stream's dictionary. Since
// "dict" does not otherwise represent anything, this is
// unambiguous. The getStreamJSON() call can be used to add
// encoding of the stream's data.
// * Object types that are only valid in content streams (inline // * Object types that are only valid in content streams (inline
// image, operator) as well as "reserved" objects are not // image, operator) as well as "reserved" objects are not
// representable and will be serialized as "null". // representable and will be serialized as "null".
@ -1321,6 +1346,12 @@ class QPDFObjectHandle
// dereference_indirect applies only to this object. It is not // dereference_indirect applies only to this object. It is not
// recursive. // recursive.
QPDF_DLL QPDF_DLL
JSON getJSON(int json_version, bool dereference_indirect = false);
// Deprecated version uses v1 for backward compatibility.
// ABI: remove for qpdf 12
[[deprecated("Use getJSON(int version)")]]
QPDF_DLL
JSON getJSON(bool dereference_indirect = false); JSON getJSON(bool dereference_indirect = false);
// Legacy helper methods for commonly performed operations on // Legacy helper methods for commonly performed operations on

View File

@ -6,12 +6,12 @@ include/qpdf/auto_job_c_enc.hh 28446f3c32153a52afa239ea40503e6cc8ac2c026813526a3
include/qpdf/auto_job_c_main.hh 940aa6f1ead18ed08ba33f11254e9f042348262c85b91de742f0427094412a80 include/qpdf/auto_job_c_main.hh 940aa6f1ead18ed08ba33f11254e9f042348262c85b91de742f0427094412a80
include/qpdf/auto_job_c_pages.hh b3cc0f21029f6d89efa043dcdbfa183cb59325b6506001c18911614fe8e568ec include/qpdf/auto_job_c_pages.hh b3cc0f21029f6d89efa043dcdbfa183cb59325b6506001c18911614fe8e568ec
include/qpdf/auto_job_c_uo.hh ae21b69a1efa9333050f4833d465f6daff87e5b38e5106e49bbef5d4132e4ed1 include/qpdf/auto_job_c_uo.hh ae21b69a1efa9333050f4833d465f6daff87e5b38e5106e49bbef5d4132e4ed1
job.yml 3c130913b3546f272c06b334ece9aa70450449539db10e39673b5ee79d863f48 job.yml e3d9752ea8810872447190b0b2b913f591ae03dfdd876945adf1bbb76942cd0f
libqpdf/qpdf/auto_job_decl.hh 74df4d7fdbdf51ecd0d58ce1e9844bb5525b9adac5a45f7c9a787ecdda2868df libqpdf/qpdf/auto_job_decl.hh 74df4d7fdbdf51ecd0d58ce1e9844bb5525b9adac5a45f7c9a787ecdda2868df
libqpdf/qpdf/auto_job_help.hh a3d1a326a3f8ff61a7d451176acde3bb6c8ad66c1ea7a0b8c5d789917ad3a9ee libqpdf/qpdf/auto_job_help.hh a3d1a326a3f8ff61a7d451176acde3bb6c8ad66c1ea7a0b8c5d789917ad3a9ee
libqpdf/qpdf/auto_job_init.hh c8477a597f037d7de5fd131b008a75f1fe435bba248261abe422e8bfb24c8755 libqpdf/qpdf/auto_job_init.hh 1dcefadac02bb4b3a5d112912483902ad46489b1cacefefd3ebe4f64fc1b8a29
libqpdf/qpdf/auto_job_json_decl.hh 06caa46eaf71db8a50c046f91866baa8087745a9474319fb7c86d92634cc8297 libqpdf/qpdf/auto_job_json_decl.hh 06caa46eaf71db8a50c046f91866baa8087745a9474319fb7c86d92634cc8297
libqpdf/qpdf/auto_job_json_init.hh e7047a7c83737adfaae49abc295a579bb9b9e0a4644e911d1656a604cb202208 libqpdf/qpdf/auto_job_json_init.hh 784ff973e7efbf589f6a9b3ad22b3aada5c5393e9c5cd31845b8fe4af6e03f98
libqpdf/qpdf/auto_job_schema.hh cbbcae166cfecbdbdeb40c5a30870e03604a019a8b4f7a217d554a82431d2e5f libqpdf/qpdf/auto_job_schema.hh cbbcae166cfecbdbdeb40c5a30870e03604a019a8b4f7a217d554a82431d2e5f
manual/_ext/qpdf.py 6add6321666031d55ed4aedf7c00e5662bba856dfcd66ccb526563bffefbb580 manual/_ext/qpdf.py 6add6321666031d55ed4aedf7c00e5662bba856dfcd66ccb526563bffefbb580
manual/cli.rst 8684ca1f601f2832cded52d1b2f74730f97b7b85b57e31a399231731fbe80d26 manual/cli.rst 8684ca1f601f2832cded52d1b2f74730f97b7b85b57e31a399231731fbe80d26

View File

@ -38,6 +38,7 @@ choices:
- screen - screen
json_version: json_version:
- 1 - 1
- 2
- latest - latest
json_key: json_key:
# The list of selectable top-level keys id duplicated in the # The list of selectable top-level keys id duplicated in the

View File

@ -1053,12 +1053,20 @@ QPDFJob::doJSONObjects(Pipeline* p, bool& first, QPDF& pdf)
for (auto& obj: objects) { for (auto& obj: objects) {
if (all_objects || wanted_og.count(obj.getObjGen())) { if (all_objects || wanted_og.count(obj.getObjGen())) {
JSON::writeDictionaryItem( JSON::writeDictionaryItem(
p, first_object, obj.unparse(), obj.getJSON(true), 1); p,
first_object,
obj.unparse(),
obj.getJSON(this->m->json_version, true),
1);
} }
} }
if (all_objects || m->json_objects.count("trailer")) { if (all_objects || m->json_objects.count("trailer")) {
JSON::writeDictionaryItem( JSON::writeDictionaryItem(
p, first_object, "trailer", pdf.getTrailer().getJSON(true), 1); p,
first_object,
"trailer",
pdf.getTrailer().getJSON(this->m->json_version, true),
1);
} }
JSON::writeDictionaryClose(p, first_object, 1); JSON::writeDictionaryClose(p, first_object, 1);
} }
@ -1080,11 +1088,13 @@ QPDFJob::doJSONObjectinfo(Pipeline* p, bool& first, QPDF& pdf)
j_stream.addDictionaryMember("is", JSON::makeBool(is_stream)); j_stream.addDictionaryMember("is", JSON::makeBool(is_stream));
j_stream.addDictionaryMember( j_stream.addDictionaryMember(
"length", "length",
(is_stream ? obj.getDict().getKey("/Length").getJSON(true) (is_stream ? obj.getDict().getKey("/Length").getJSON(
this->m->json_version, true)
: JSON::makeNull())); : JSON::makeNull()));
j_stream.addDictionaryMember( j_stream.addDictionaryMember(
"filter", "filter",
(is_stream ? obj.getDict().getKey("/Filter").getJSON(true) (is_stream ? obj.getDict().getKey("/Filter").getJSON(
this->m->json_version, true)
: JSON::makeNull())); : JSON::makeNull()));
JSON::writeDictionaryItem( JSON::writeDictionaryItem(
p, first_object, obj.unparse(), j_details, 1); p, first_object, obj.unparse(), j_details, 1);
@ -1108,7 +1118,8 @@ QPDFJob::doJSONPages(Pipeline* p, bool& first, QPDF& pdf)
++pageno; ++pageno;
JSON j_page = JSON::makeDictionary(); JSON j_page = JSON::makeDictionary();
QPDFObjectHandle page = ph.getObjectHandle(); QPDFObjectHandle page = ph.getObjectHandle();
j_page.addDictionaryMember("object", page.getJSON()); j_page.addDictionaryMember(
"object", page.getJSON(this->m->json_version));
JSON j_images = j_page.addDictionaryMember("images", JSON::makeArray()); JSON j_images = j_page.addDictionaryMember("images", JSON::makeArray());
std::map<std::string, QPDFObjectHandle> images = ph.getImages(); std::map<std::string, QPDFObjectHandle> images = ph.getImages();
for (auto const& iter2: images) { for (auto const& iter2: images) {
@ -1116,17 +1127,23 @@ QPDFJob::doJSONPages(Pipeline* p, bool& first, QPDF& pdf)
j_image.addDictionaryMember("name", JSON::makeString(iter2.first)); j_image.addDictionaryMember("name", JSON::makeString(iter2.first));
QPDFObjectHandle image = iter2.second; QPDFObjectHandle image = iter2.second;
QPDFObjectHandle dict = image.getDict(); QPDFObjectHandle dict = image.getDict();
j_image.addDictionaryMember("object", image.getJSON());
j_image.addDictionaryMember( j_image.addDictionaryMember(
"width", dict.getKey("/Width").getJSON()); "object", image.getJSON(this->m->json_version));
j_image.addDictionaryMember( j_image.addDictionaryMember(
"height", dict.getKey("/Height").getJSON()); "width", dict.getKey("/Width").getJSON(this->m->json_version));
j_image.addDictionaryMember( j_image.addDictionaryMember(
"colorspace", dict.getKey("/ColorSpace").getJSON()); "height",
dict.getKey("/Height").getJSON(this->m->json_version));
j_image.addDictionaryMember( j_image.addDictionaryMember(
"bitspercomponent", dict.getKey("/BitsPerComponent").getJSON()); "colorspace",
dict.getKey("/ColorSpace").getJSON(this->m->json_version));
j_image.addDictionaryMember(
"bitspercomponent",
dict.getKey("/BitsPerComponent")
.getJSON(this->m->json_version));
QPDFObjectHandle filters = dict.getKey("/Filter").wrapInArray(); QPDFObjectHandle filters = dict.getKey("/Filter").wrapInArray();
j_image.addDictionaryMember("filter", filters.getJSON()); j_image.addDictionaryMember(
"filter", filters.getJSON(this->m->json_version));
QPDFObjectHandle decode_parms = dict.getKey("/DecodeParms"); QPDFObjectHandle decode_parms = dict.getKey("/DecodeParms");
QPDFObjectHandle dp_array; QPDFObjectHandle dp_array;
if (decode_parms.isArray()) { if (decode_parms.isArray()) {
@ -1137,7 +1154,8 @@ QPDFJob::doJSONPages(Pipeline* p, bool& first, QPDF& pdf)
dp_array.appendItem(decode_parms); dp_array.appendItem(decode_parms);
} }
} }
j_image.addDictionaryMember("decodeparms", dp_array.getJSON()); j_image.addDictionaryMember(
"decodeparms", dp_array.getJSON(this->m->json_version));
j_image.addDictionaryMember( j_image.addDictionaryMember(
"filterable", "filterable",
JSON::makeBool( JSON::makeBool(
@ -1148,10 +1166,11 @@ QPDFJob::doJSONPages(Pipeline* p, bool& first, QPDF& pdf)
j_page.addDictionaryMember("contents", JSON::makeArray()); j_page.addDictionaryMember("contents", JSON::makeArray());
std::vector<QPDFObjectHandle> content = ph.getPageContents(); std::vector<QPDFObjectHandle> content = ph.getPageContents();
for (auto& iter2: content) { for (auto& iter2: content) {
j_contents.addArrayElement(iter2.getJSON()); j_contents.addArrayElement(iter2.getJSON(this->m->json_version));
} }
j_page.addDictionaryMember( j_page.addDictionaryMember(
"label", pldh.getLabelForPage(pageno).getJSON()); "label",
pldh.getLabelForPage(pageno).getJSON(this->m->json_version));
JSON j_outlines = JSON j_outlines =
j_page.addDictionaryMember("outlines", JSON::makeArray()); j_page.addDictionaryMember("outlines", JSON::makeArray());
std::vector<QPDFOutlineObjectHelper> outlines = std::vector<QPDFOutlineObjectHelper> outlines =
@ -1159,11 +1178,12 @@ QPDFJob::doJSONPages(Pipeline* p, bool& first, QPDF& pdf)
for (auto& oiter: outlines) { for (auto& oiter: outlines) {
JSON j_outline = j_outlines.addArrayElement(JSON::makeDictionary()); JSON j_outline = j_outlines.addArrayElement(JSON::makeDictionary());
j_outline.addDictionaryMember( j_outline.addDictionaryMember(
"object", oiter.getObjectHandle().getJSON()); "object",
oiter.getObjectHandle().getJSON(this->m->json_version));
j_outline.addDictionaryMember( j_outline.addDictionaryMember(
"title", JSON::makeString(oiter.getTitle())); "title", JSON::makeString(oiter.getTitle()));
j_outline.addDictionaryMember( j_outline.addDictionaryMember(
"dest", oiter.getDest().getJSON(true)); "dest", oiter.getDest().getJSON(this->m->json_version, true));
} }
j_page.addDictionaryMember("pageposfrom1", JSON::makeInt(1 + pageno)); j_page.addDictionaryMember("pageposfrom1", JSON::makeInt(1 + pageno));
JSON::writeArrayItem(p, first_page, j_page, 1); JSON::writeArrayItem(p, first_page, j_page, 1);
@ -1192,25 +1212,29 @@ QPDFJob::doJSONPageLabels(Pipeline* p, bool& first, QPDF& pdf)
break; break;
} }
JSON j_label = j_labels.addArrayElement(JSON::makeDictionary()); JSON j_label = j_labels.addArrayElement(JSON::makeDictionary());
j_label.addDictionaryMember("index", (*iter).getJSON()); j_label.addDictionaryMember(
"index", (*iter).getJSON(this->m->json_version));
++iter; ++iter;
j_label.addDictionaryMember("label", (*iter).getJSON()); j_label.addDictionaryMember(
"label", (*iter).getJSON(this->m->json_version));
} }
} }
JSON::writeDictionaryItem(p, first, "pagelabels", j_labels, 0); JSON::writeDictionaryItem(p, first, "pagelabels", j_labels, 0);
} }
static void void
add_outlines_to_json( QPDFJob::addOutlinesToJson(
std::vector<QPDFOutlineObjectHelper> outlines, std::vector<QPDFOutlineObjectHelper> outlines,
JSON& j, JSON& j,
std::map<QPDFObjGen, int>& page_numbers) std::map<QPDFObjGen, int>& page_numbers)
{ {
for (auto& ol: outlines) { for (auto& ol: outlines) {
JSON jo = j.addArrayElement(JSON::makeDictionary()); JSON jo = j.addArrayElement(JSON::makeDictionary());
jo.addDictionaryMember("object", ol.getObjectHandle().getJSON()); jo.addDictionaryMember(
"object", ol.getObjectHandle().getJSON(this->m->json_version));
jo.addDictionaryMember("title", JSON::makeString(ol.getTitle())); jo.addDictionaryMember("title", JSON::makeString(ol.getTitle()));
jo.addDictionaryMember("dest", ol.getDest().getJSON(true)); jo.addDictionaryMember(
"dest", ol.getDest().getJSON(this->m->json_version, true));
jo.addDictionaryMember("open", JSON::makeBool(ol.getCount() >= 0)); jo.addDictionaryMember("open", JSON::makeBool(ol.getCount() >= 0));
QPDFObjectHandle page = ol.getDestPage(); QPDFObjectHandle page = ol.getDestPage();
JSON j_destpage = JSON::makeNull(); JSON j_destpage = JSON::makeNull();
@ -1222,7 +1246,7 @@ add_outlines_to_json(
} }
jo.addDictionaryMember("destpageposfrom1", j_destpage); jo.addDictionaryMember("destpageposfrom1", j_destpage);
JSON j_kids = jo.addDictionaryMember("kids", JSON::makeArray()); JSON j_kids = jo.addDictionaryMember("kids", JSON::makeArray());
add_outlines_to_json(ol.getKids(), j_kids, page_numbers); addOutlinesToJson(ol.getKids(), j_kids, page_numbers);
} }
} }
@ -1240,7 +1264,7 @@ QPDFJob::doJSONOutlines(Pipeline* p, bool& first, QPDF& pdf)
JSON j_outlines = JSON::makeArray(); JSON j_outlines = JSON::makeArray();
QPDFOutlineDocumentHelper odh(pdf); QPDFOutlineDocumentHelper odh(pdf);
add_outlines_to_json(odh.getTopLevelOutlines(), j_outlines, page_numbers); addOutlinesToJson(odh.getTopLevelOutlines(), j_outlines, page_numbers);
JSON::writeDictionaryItem(p, first, "outlines", j_outlines, 0); JSON::writeDictionaryItem(p, first, "outlines", j_outlines, 0);
} }
@ -1265,9 +1289,11 @@ QPDFJob::doJSONAcroform(Pipeline* p, bool& first, QPDF& pdf)
QPDFFormFieldObjectHelper ffh = afdh.getFieldForAnnotation(aoh); QPDFFormFieldObjectHelper ffh = afdh.getFieldForAnnotation(aoh);
JSON j_field = j_fields.addArrayElement(JSON::makeDictionary()); JSON j_field = j_fields.addArrayElement(JSON::makeDictionary());
j_field.addDictionaryMember( j_field.addDictionaryMember(
"object", ffh.getObjectHandle().getJSON()); "object", ffh.getObjectHandle().getJSON(this->m->json_version));
j_field.addDictionaryMember( j_field.addDictionaryMember(
"parent", ffh.getObjectHandle().getKey("/Parent").getJSON()); "parent",
ffh.getObjectHandle().getKey("/Parent").getJSON(
this->m->json_version));
j_field.addDictionaryMember( j_field.addDictionaryMember(
"pageposfrom1", JSON::makeInt(pagepos1)); "pageposfrom1", JSON::makeInt(pagepos1));
j_field.addDictionaryMember( j_field.addDictionaryMember(
@ -1282,9 +1308,11 @@ QPDFJob::doJSONAcroform(Pipeline* p, bool& first, QPDF& pdf)
"alternativename", JSON::makeString(ffh.getAlternativeName())); "alternativename", JSON::makeString(ffh.getAlternativeName()));
j_field.addDictionaryMember( j_field.addDictionaryMember(
"mappingname", JSON::makeString(ffh.getMappingName())); "mappingname", JSON::makeString(ffh.getMappingName()));
j_field.addDictionaryMember("value", ffh.getValue().getJSON());
j_field.addDictionaryMember( j_field.addDictionaryMember(
"defaultvalue", ffh.getDefaultValue().getJSON()); "value", ffh.getValue().getJSON(this->m->json_version));
j_field.addDictionaryMember(
"defaultvalue",
ffh.getDefaultValue().getJSON(this->m->json_version));
j_field.addDictionaryMember( j_field.addDictionaryMember(
"quadding", JSON::makeInt(ffh.getQuadding())); "quadding", JSON::makeInt(ffh.getQuadding()));
j_field.addDictionaryMember( j_field.addDictionaryMember(
@ -1303,7 +1331,7 @@ QPDFJob::doJSONAcroform(Pipeline* p, bool& first, QPDF& pdf)
JSON j_annot = j_field.addDictionaryMember( JSON j_annot = j_field.addDictionaryMember(
"annotation", JSON::makeDictionary()); "annotation", JSON::makeDictionary());
j_annot.addDictionaryMember( j_annot.addDictionaryMember(
"object", aoh.getObjectHandle().getJSON()); "object", aoh.getObjectHandle().getJSON(this->m->json_version));
j_annot.addDictionaryMember( j_annot.addDictionaryMember(
"appearancestate", JSON::makeString(aoh.getAppearanceState())); "appearancestate", JSON::makeString(aoh.getAppearanceState()));
j_annot.addDictionaryMember( j_annot.addDictionaryMember(
@ -1621,7 +1649,8 @@ QPDFJob::doJSON(QPDF& pdf, Pipeline* p)
// change is made to the JSON format. Clients of the JSON are to // change is made to the JSON format. Clients of the JSON are to
// ignore unrecognized keys, so we only update the version of a // ignore unrecognized keys, so we only update the version of a
// key disappears or if its value changes meaning. // key disappears or if its value changes meaning.
JSON::writeDictionaryItem(p, first, "version", JSON::makeInt(1), 0); JSON::writeDictionaryItem(
p, first, "version", JSON::makeInt(this->m->json_version), 0);
JSON j_params = JSON::makeDictionary(); JSON j_params = JSON::makeDictionary();
std::string decode_level_str; std::string decode_level_str;
switch (m->decode_level) { switch (m->decode_level) {

View File

@ -235,11 +235,11 @@ QPDFJob::Config*
QPDFJob::Config::json(std::string const& parameter) QPDFJob::Config::json(std::string const& parameter)
{ {
if (parameter.empty() || (parameter == "latest")) { if (parameter.empty() || (parameter == "latest")) {
o.m->json_version = 1; o.m->json_version = JSON::LATEST;
} else { } else {
o.m->json_version = QUtil::string_to_int(parameter.c_str()); o.m->json_version = QUtil::string_to_int(parameter.c_str());
} }
if (o.m->json_version != 1) { if ((o.m->json_version < 1) || (o.m->json_version > JSON::LATEST)) {
usage(std::string("unsupported json version ") + parameter); usage(std::string("unsupported json version ") + parameter);
} }
o.m->require_outfile = false; o.m->require_outfile = false;

View File

@ -1775,8 +1775,15 @@ QPDFObjectHandle::unparseBinary()
} }
} }
// Deprecated versionless getJSON to be removed in qpdf 12
JSON JSON
QPDFObjectHandle::getJSON(bool dereference_indirect) QPDFObjectHandle::getJSON(bool dereference_indirect)
{
return getJSON(1, dereference_indirect);
}
JSON
QPDFObjectHandle::getJSON(int json_version, bool dereference_indirect)
{ {
if ((!dereference_indirect) && this->isIndirect()) { if ((!dereference_indirect) && this->isIndirect()) {
return JSON::makeString(unparse()); return JSON::makeString(unparse());
@ -1786,7 +1793,7 @@ QPDFObjectHandle::getJSON(bool dereference_indirect)
throw std::logic_error( throw std::logic_error(
"QPDFObjectHandle: attempting to unparse a reserved object"); "QPDFObjectHandle: attempting to unparse a reserved object");
} }
return this->obj->getJSON(); return this->obj->getJSON(json_version);
} }
} }

View File

@ -34,12 +34,12 @@ QPDF_Array::unparse()
} }
JSON JSON
QPDF_Array::getJSON() QPDF_Array::getJSON(int json_version)
{ {
JSON j = JSON::makeArray(); JSON j = JSON::makeArray();
size_t size = this->elements.size(); size_t size = this->elements.size();
for (size_t i = 0; i < size; ++i) { for (size_t i = 0; i < size; ++i) {
j.addArrayElement(this->elements.at(i).getJSON()); j.addArrayElement(this->elements.at(i).getJSON(json_version));
} }
return j; return j;
} }

View File

@ -12,7 +12,7 @@ QPDF_Bool::unparse()
} }
JSON JSON
QPDF_Bool::getJSON() QPDF_Bool::getJSON(int json_version)
{ {
return JSON::makeBool(this->val); return JSON::makeBool(this->val);
} }

View File

@ -32,13 +32,14 @@ QPDF_Dictionary::unparse()
} }
JSON JSON
QPDF_Dictionary::getJSON() QPDF_Dictionary::getJSON(int json_version)
{ {
JSON j = JSON::makeDictionary(); JSON j = JSON::makeDictionary();
for (auto& iter: this->items) { for (auto& iter: this->items) {
if (!iter.second.isNull()) { if (!iter.second.isNull()) {
j.addDictionaryMember( j.addDictionaryMember(
QPDF_Name::normalizeName(iter.first), iter.second.getJSON()); QPDF_Name::normalizeName(iter.first),
iter.second.getJSON(json_version));
} }
} }
return j; return j;

View File

@ -14,7 +14,7 @@ QPDF_InlineImage::unparse()
} }
JSON JSON
QPDF_InlineImage::getJSON() QPDF_InlineImage::getJSON(int json_version)
{ {
return JSON::makeNull(); return JSON::makeNull();
} }

View File

@ -14,7 +14,7 @@ QPDF_Integer::unparse()
} }
JSON JSON
QPDF_Integer::getJSON() QPDF_Integer::getJSON(int json_version)
{ {
return JSON::makeInt(this->val); return JSON::makeInt(this->val);
} }

View File

@ -40,7 +40,7 @@ QPDF_Name::unparse()
} }
JSON JSON
QPDF_Name::getJSON() QPDF_Name::getJSON(int json_version)
{ {
return JSON::makeString(normalizeName(this->name)); return JSON::makeString(normalizeName(this->name));
} }

View File

@ -7,7 +7,7 @@ QPDF_Null::unparse()
} }
JSON JSON
QPDF_Null::getJSON() QPDF_Null::getJSON(int json_version)
{ {
return JSON::makeNull(); return JSON::makeNull();
} }

View File

@ -14,7 +14,7 @@ QPDF_Operator::unparse()
} }
JSON JSON
QPDF_Operator::getJSON() QPDF_Operator::getJSON(int json_version)
{ {
return JSON::makeNull(); return JSON::makeNull();
} }

View File

@ -20,7 +20,7 @@ QPDF_Real::unparse()
} }
JSON JSON
QPDF_Real::getJSON() QPDF_Real::getJSON(int json_version)
{ {
// While PDF allows .x or -.x, JSON does not. Rather than // While PDF allows .x or -.x, JSON does not. Rather than
// converting from string to double and back, just handle this as a // converting from string to double and back, just handle this as a

View File

@ -10,7 +10,7 @@ QPDF_Reserved::unparse()
} }
JSON JSON
QPDF_Reserved::getJSON() QPDF_Reserved::getJSON(int json_version)
{ {
throw std::logic_error("attempt to generate JSON from QPDF_Reserved"); throw std::logic_error("attempt to generate JSON from QPDF_Reserved");
return JSON::makeNull(); return JSON::makeNull();

View File

@ -151,9 +151,10 @@ QPDF_Stream::unparse()
} }
JSON JSON
QPDF_Stream::getJSON() QPDF_Stream::getJSON(int json_version)
{ {
return this->stream_dict.getJSON(); // QXXXQ
return this->stream_dict.getJSON(json_version);
} }
QPDFObject::object_type_e QPDFObject::object_type_e

View File

@ -43,8 +43,9 @@ QPDF_String::unparse()
} }
JSON JSON
QPDF_String::getJSON() QPDF_String::getJSON(int json_version)
{ {
// QXXXQ
return JSON::makeString(getUTF8Val()); return JSON::makeString(getUTF8Val());
} }

View File

@ -14,7 +14,7 @@ class QPDF_Array: public QPDFObject
QPDF_Array(SparseOHArray const& items); QPDF_Array(SparseOHArray const& items);
virtual ~QPDF_Array() = default; virtual ~QPDF_Array() = default;
virtual std::string unparse(); virtual std::string unparse();
virtual JSON getJSON(); virtual JSON getJSON(int json_version);
virtual QPDFObject::object_type_e getTypeCode() const; virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const; virtual char const* getTypeName() const;
virtual void setDescription(QPDF*, std::string const&); virtual void setDescription(QPDF*, std::string const&);

View File

@ -9,7 +9,7 @@ class QPDF_Bool: public QPDFObject
QPDF_Bool(bool val); QPDF_Bool(bool val);
virtual ~QPDF_Bool() = default; virtual ~QPDF_Bool() = default;
virtual std::string unparse(); virtual std::string unparse();
virtual JSON getJSON(); virtual JSON getJSON(int json_version);
virtual QPDFObject::object_type_e getTypeCode() const; virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const; virtual char const* getTypeName() const;
bool getVal() const; bool getVal() const;

View File

@ -14,7 +14,7 @@ class QPDF_Dictionary: public QPDFObject
QPDF_Dictionary(std::map<std::string, QPDFObjectHandle> const& items); QPDF_Dictionary(std::map<std::string, QPDFObjectHandle> const& items);
virtual ~QPDF_Dictionary() = default; virtual ~QPDF_Dictionary() = default;
virtual std::string unparse(); virtual std::string unparse();
virtual JSON getJSON(); virtual JSON getJSON(int json_version);
virtual QPDFObject::object_type_e getTypeCode() const; virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const; virtual char const* getTypeName() const;
virtual void setDescription(QPDF*, std::string const&); virtual void setDescription(QPDF*, std::string const&);

View File

@ -9,7 +9,7 @@ class QPDF_InlineImage: public QPDFObject
QPDF_InlineImage(std::string const& val); QPDF_InlineImage(std::string const& val);
virtual ~QPDF_InlineImage() = default; virtual ~QPDF_InlineImage() = default;
virtual std::string unparse(); virtual std::string unparse();
virtual JSON getJSON(); virtual JSON getJSON(int json_version);
virtual QPDFObject::object_type_e getTypeCode() const; virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const; virtual char const* getTypeName() const;
std::string getVal() const; std::string getVal() const;

View File

@ -9,7 +9,7 @@ class QPDF_Integer: public QPDFObject
QPDF_Integer(long long val); QPDF_Integer(long long val);
virtual ~QPDF_Integer() = default; virtual ~QPDF_Integer() = default;
virtual std::string unparse(); virtual std::string unparse();
virtual JSON getJSON(); virtual JSON getJSON(int json_version);
virtual QPDFObject::object_type_e getTypeCode() const; virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const; virtual char const* getTypeName() const;
long long getVal() const; long long getVal() const;

View File

@ -9,7 +9,7 @@ class QPDF_Name: public QPDFObject
QPDF_Name(std::string const& name); QPDF_Name(std::string const& name);
virtual ~QPDF_Name() = default; virtual ~QPDF_Name() = default;
virtual std::string unparse(); virtual std::string unparse();
virtual JSON getJSON(); virtual JSON getJSON(int json_version);
virtual QPDFObject::object_type_e getTypeCode() const; virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const; virtual char const* getTypeName() const;
std::string getName() const; std::string getName() const;

View File

@ -8,7 +8,7 @@ class QPDF_Null: public QPDFObject
public: public:
virtual ~QPDF_Null() = default; virtual ~QPDF_Null() = default;
virtual std::string unparse(); virtual std::string unparse();
virtual JSON getJSON(); virtual JSON getJSON(int json_version);
virtual QPDFObject::object_type_e getTypeCode() const; virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const; virtual char const* getTypeName() const;
}; };

View File

@ -9,7 +9,7 @@ class QPDF_Operator: public QPDFObject
QPDF_Operator(std::string const& val); QPDF_Operator(std::string const& val);
virtual ~QPDF_Operator() = default; virtual ~QPDF_Operator() = default;
virtual std::string unparse(); virtual std::string unparse();
virtual JSON getJSON(); virtual JSON getJSON(int json_version);
virtual QPDFObject::object_type_e getTypeCode() const; virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const; virtual char const* getTypeName() const;
std::string getVal() const; std::string getVal() const;

View File

@ -10,7 +10,7 @@ class QPDF_Real: public QPDFObject
QPDF_Real(double value, int decimal_places, bool trim_trailing_zeroes); QPDF_Real(double value, int decimal_places, bool trim_trailing_zeroes);
virtual ~QPDF_Real() = default; virtual ~QPDF_Real() = default;
virtual std::string unparse(); virtual std::string unparse();
virtual JSON getJSON(); virtual JSON getJSON(int json_version);
virtual QPDFObject::object_type_e getTypeCode() const; virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const; virtual char const* getTypeName() const;
std::string getVal(); std::string getVal();

View File

@ -8,7 +8,7 @@ class QPDF_Reserved: public QPDFObject
public: public:
virtual ~QPDF_Reserved() = default; virtual ~QPDF_Reserved() = default;
virtual std::string unparse(); virtual std::string unparse();
virtual JSON getJSON(); virtual JSON getJSON(int json_version);
virtual QPDFObject::object_type_e getTypeCode() const; virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const; virtual char const* getTypeName() const;
}; };

View File

@ -25,7 +25,7 @@ class QPDF_Stream: public QPDFObject
size_t length); size_t length);
virtual ~QPDF_Stream() = default; virtual ~QPDF_Stream() = default;
virtual std::string unparse(); virtual std::string unparse();
virtual JSON getJSON(); virtual JSON getJSON(int json_version);
virtual QPDFObject::object_type_e getTypeCode() const; virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const; virtual char const* getTypeName() const;
virtual void setDescription(QPDF*, std::string const&); virtual void setDescription(QPDF*, std::string const&);

View File

@ -15,7 +15,7 @@ class QPDF_String: public QPDFObject
virtual QPDFObject::object_type_e getTypeCode() const; virtual QPDFObject::object_type_e getTypeCode() const;
virtual char const* getTypeName() const; virtual char const* getTypeName() const;
std::string unparse(bool force_binary); std::string unparse(bool force_binary);
virtual JSON getJSON(); virtual JSON getJSON(int json_version);
std::string getVal() const; std::string getVal() const;
std::string getUTF8Val() const; std::string getUTF8Val() const;

View File

@ -19,7 +19,7 @@ static char const* decode_level_choices[] = {"none", "generalized", "specialized
static char const* object_streams_choices[] = {"disable", "preserve", "generate", 0}; static char const* object_streams_choices[] = {"disable", "preserve", "generate", 0};
static char const* remove_unref_choices[] = {"auto", "yes", "no", 0}; static char const* remove_unref_choices[] = {"auto", "yes", "no", 0};
static char const* flatten_choices[] = {"all", "print", "screen", 0}; static char const* flatten_choices[] = {"all", "print", "screen", 0};
static char const* json_version_choices[] = {"1", "latest", 0}; static char const* json_version_choices[] = {"1", "2", "latest", 0};
static char const* json_key_choices[] = {"acroform", "attachments", "encrypt", "objectinfo", "objects", "outlines", "pagelabels", "pages", 0}; static char const* json_key_choices[] = {"acroform", "attachments", "encrypt", "objectinfo", "objects", "outlines", "pagelabels", "pages", 0};
static char const* print128_choices[] = {"full", "low", "none", 0}; static char const* print128_choices[] = {"full", "low", "none", 0};
static char const* modify128_choices[] = {"all", "annotate", "form", "assembly", "none", 0}; static char const* modify128_choices[] = {"all", "annotate", "form", "assembly", "none", 0};

View File

@ -12,7 +12,7 @@ static char const* decode_level_choices[] = {"none", "generalized", "specialized
static char const* object_streams_choices[] = {"disable", "preserve", "generate", 0}; static char const* object_streams_choices[] = {"disable", "preserve", "generate", 0};
static char const* remove_unref_choices[] = {"auto", "yes", "no", 0}; static char const* remove_unref_choices[] = {"auto", "yes", "no", 0};
static char const* flatten_choices[] = {"all", "print", "screen", 0}; static char const* flatten_choices[] = {"all", "print", "screen", 0};
static char const* json_version_choices[] = {"1", "latest", 0}; static char const* json_version_choices[] = {"1", "2", "latest", 0};
static char const* json_key_choices[] = {"acroform", "attachments", "encrypt", "objectinfo", "objects", "outlines", "pagelabels", "pages", 0}; static char const* json_key_choices[] = {"acroform", "attachments", "encrypt", "objectinfo", "objects", "outlines", "pagelabels", "pages", 0};
static char const* print128_choices[] = {"full", "low", "none", 0}; static char const* print128_choices[] = {"full", "low", "none", 0};
static char const* modify128_choices[] = {"all", "annotate", "form", "assembly", "none", 0}; static char const* modify128_choices[] = {"all", "annotate", "form", "assembly", "none", 0};

View File

@ -100,10 +100,12 @@ test_main()
" ],\n" " ],\n"
" \"yes\": false\n" " \"yes\": false\n"
"}"); "}");
check(QPDFObjectHandle::newReal("0.12").getJSON(), "0.12"); for (int i = 1; i <= JSON::LATEST; ++i) {
check(QPDFObjectHandle::newReal(".34").getJSON(), "0.34"); check(QPDFObjectHandle::newReal("0.12").getJSON(i), "0.12");
check(QPDFObjectHandle::newReal("-0.56").getJSON(), "-0.56"); check(QPDFObjectHandle::newReal(".34").getJSON(i), "0.34");
check(QPDFObjectHandle::newReal("-.78").getJSON(), "-0.78"); check(QPDFObjectHandle::newReal("-0.56").getJSON(i), "-0.56");
check(QPDFObjectHandle::newReal("-.78").getJSON(i), "-0.78");
}
JSON jmap2 = JSON::parse(R"({"a": 1, "b": "two", "c": [true]})"); JSON jmap2 = JSON::parse(R"({"a": 1, "b": "two", "c": [true]})");
std::map<std::string, std::string> dvalue; std::map<std::string, std::string> dvalue;
assert(jmap2.forEachDictItem( assert(jmap2.forEachDictItem(

View File

@ -4279,7 +4279,7 @@ foreach my $d (@encrypted_files)
my $enc_details = ""; my $enc_details = "";
my $enc_json = my $enc_json =
"{\n" . "{\n" .
" \"version\": 1,\n" . " \"version\": 2,\n" .
" \"parameters\": {\n" . " \"parameters\": {\n" .
" \"decodelevel\": \"generalized\"\n" . " \"decodelevel\": \"generalized\"\n" .
" },\n" . " },\n" .

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1,5 +1,5 @@
{ {
"version": 1, "version": 2,
"parameters": { "parameters": {
"decodelevel": "generalized" "decodelevel": "generalized"
}, },

View File

@ -1922,7 +1922,7 @@ test_50(QPDF& pdf, char const* arg2)
QPDFObjectHandle d1 = pdf.getTrailer().getKey("/Dict1"); QPDFObjectHandle d1 = pdf.getTrailer().getKey("/Dict1");
QPDFObjectHandle d2 = pdf.getTrailer().getKey("/Dict2"); QPDFObjectHandle d2 = pdf.getTrailer().getKey("/Dict2");
d1.mergeResources(d2); d1.mergeResources(d2);
std::cout << d1.getJSON().unparse() << std::endl; std::cout << d1.getJSON(JSON::LATEST).unparse() << std::endl;
// Top-level type mismatch // Top-level type mismatch
d1.mergeResources(d2.getKey("/k1")); d1.mergeResources(d2.getKey("/k1"));
for (auto const& name: d1.getResourceNames()) { for (auto const& name: d1.getResourceNames()) {
@ -3114,7 +3114,7 @@ test_87(QPDF& pdf, char const* arg2)
}); });
assert(dict.unparse() == "<< /A 2 >>"); assert(dict.unparse() == "<< /A 2 >>");
assert(dict.getKeys() == std::set<std::string>({"/A"})); assert(dict.getKeys() == std::set<std::string>({"/A"}));
assert(dict.getJSON().unparse() == "{\n \"/A\": 2\n}"); assert(dict.getJSON(JSON::LATEST).unparse() == "{\n \"/A\": 2\n}");
} }
static void static void