From a9fbbd5dca1f39e9eafc8ddf8ada30dffd68e4df Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 6 May 2022 19:08:31 -0400 Subject: Objectinfo json: write incrementally and in numeric order This script was used on 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 'objectinfo' not in data: continue trailer = None to_sort = [] for k, v in data['objectinfo'].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]) newobjectinfo = {x[1]: x[2] for x in sorted(to_sort)} if trailer is not None: newobjectinfo['trailer'] = trailer data['objectinfo'] = newobjectinfo print(json_dumps(data)) ---------- --- libqpdf/QPDFJob.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'libqpdf') diff --git a/libqpdf/QPDFJob.cc b/libqpdf/QPDFJob.cc index 63fa0c34..78c678b0 100644 --- a/libqpdf/QPDFJob.cc +++ b/libqpdf/QPDFJob.cc @@ -1066,13 +1066,14 @@ QPDFJob::doJSONObjects(Pipeline* p, bool& first, QPDF& pdf) void QPDFJob::doJSONObjectinfo(Pipeline* p, bool& first, QPDF& pdf) { + JSON::writeDictionaryKey(p, first, "objectinfo", 0); + bool first_object = true; + JSON::writeDictionaryOpen(p, first_object, 1); bool all_objects = m->json_objects.empty(); std::set wanted_og = getWantedJSONObjects(); - JSON j_objectinfo = JSON::makeDictionary(); for (auto& obj: pdf.getAllObjects()) { if (all_objects || wanted_og.count(obj.getObjGen())) { - auto j_details = j_objectinfo.addDictionaryMember( - obj.unparse(), JSON::makeDictionary()); + auto j_details = JSON::makeDictionary(); auto j_stream = j_details.addDictionaryMember("stream", JSON::makeDictionary()); bool is_stream = obj.isStream(); @@ -1085,9 +1086,11 @@ QPDFJob::doJSONObjectinfo(Pipeline* p, bool& first, QPDF& pdf) "filter", (is_stream ? obj.getDict().getKey("/Filter").getJSON(true) : JSON::makeNull())); + JSON::writeDictionaryItem( + p, first_object, obj.unparse(), j_details, 1); } } - JSON::writeDictionaryItem(p, first, "objectinfo", j_objectinfo, 0); + JSON::writeDictionaryClose(p, first_object, 1); } void -- cgit v1.2.3-54-g00ecf