aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDF_Array.cc
diff options
context:
space:
mode:
authorm-holger <m-holger@kubitscheck.org>2024-02-09 14:09:08 +0100
committerm-holger <m-holger@kubitscheck.org>2024-02-16 11:51:25 +0100
commite2737ab646bff6aa07ba72e0cc15cc955d9afcc0 (patch)
tree0ff6b1adb00694477e23e278055d61474d6b844a /libqpdf/QPDF_Array.cc
parent9e90007a4a490dd1335b63079fc3e2a74420911f (diff)
downloadqpdf-e2737ab646bff6aa07ba72e0cc15cc955d9afcc0.tar.zst
Add new writeJSON methods
Create an alternative to getJSON to allow an object handle to be written as JSON without the overhead of creating a JSON object.
Diffstat (limited to 'libqpdf/QPDF_Array.cc')
-rw-r--r--libqpdf/QPDF_Array.cc38
1 files changed, 38 insertions, 0 deletions
diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc
index 789acc35..6e50a781 100644
--- a/libqpdf/QPDF_Array.cc
+++ b/libqpdf/QPDF_Array.cc
@@ -1,5 +1,6 @@
#include <qpdf/QPDF_Array.hh>
+#include <qpdf/JSON_writer.hh>
#include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/QPDFObject_private.hh>
#include <qpdf/QTC.hh>
@@ -180,6 +181,43 @@ QPDF_Array::getJSON(int json_version)
return j_array;
}
+void
+QPDF_Array::writeJSON(int json_version, JSON::Writer& p)
+{
+ p.writeStart('[');
+ if (sp) {
+ int next = 0;
+ for (auto& item: sp->elements) {
+ int key = item.first;
+ for (int j = next; j < key; ++j) {
+ p.writeNext() << "null";
+ }
+ p.writeNext();
+ auto og = item.second->getObjGen();
+ if (og.isIndirect()) {
+ p << "\"" << og.unparse(' ') << " R\"";
+ } else {
+ item.second->writeJSON(json_version, p);
+ }
+ next = ++key;
+ }
+ for (int j = next; j < sp->size; ++j) {
+ p.writeNext() << "null";
+ }
+ } else {
+ for (auto const& item: elements) {
+ p.writeNext();
+ auto og = item->getObjGen();
+ if (og.isIndirect()) {
+ p << "\"" << og.unparse(' ') << " R\"";
+ } else {
+ item->writeJSON(json_version, p);
+ }
+ }
+ }
+ p.writeEnd(']');
+}
+
QPDFObjectHandle
QPDF_Array::at(int n) const noexcept
{