aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/JSON.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-05-07 01:04:20 +0200
committerJay Berkenbilt <ejb@ql.org>2022-05-07 14:26:31 +0200
commit948de609900745835656d4566a90fee64c5343da (patch)
treee4862655453cc6213f2a1713fc1548462a38a8df /libqpdf/JSON.cc
parentf50274ef4660cb21177937ff49c9d11675cef8a9 (diff)
downloadqpdf-948de609900745835656d4566a90fee64c5343da.tar.zst
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)) ----------
Diffstat (limited to 'libqpdf/JSON.cc')
-rw-r--r--libqpdf/JSON.cc11
1 files changed, 9 insertions, 2 deletions
diff --git a/libqpdf/JSON.cc b/libqpdf/JSON.cc
index 31675a42..1c49f9ee 100644
--- a/libqpdf/JSON.cc
+++ b/libqpdf/JSON.cc
@@ -78,6 +78,14 @@ JSON::writeArrayClose(Pipeline* p, bool first, size_t depth)
}
void
+JSON::writeDictionaryKey(
+ Pipeline* p, bool& first, std::string const& key, size_t depth)
+{
+ writeNext(p, first, depth);
+ *p << "\"" << key << "\": ";
+}
+
+void
JSON::writeDictionaryItem(
Pipeline* p,
bool& first,
@@ -85,8 +93,7 @@ JSON::writeDictionaryItem(
JSON const& value,
size_t depth)
{
- writeNext(p, first, depth);
- *p << "\"" << key << "\": ";
+ writeDictionaryKey(p, first, key, depth);
value.write(p, 1 + depth);
}