diff options
author | m-holger <m-holger@kubitscheck.org> | 2024-02-09 14:09:08 +0100 |
---|---|---|
committer | m-holger <m-holger@kubitscheck.org> | 2024-02-16 11:51:25 +0100 |
commit | e2737ab646bff6aa07ba72e0cc15cc955d9afcc0 (patch) | |
tree | 0ff6b1adb00694477e23e278055d61474d6b844a /libqpdf/QPDF_Dictionary.cc | |
parent | 9e90007a4a490dd1335b63079fc3e2a74420911f (diff) | |
download | qpdf-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_Dictionary.cc')
-rw-r--r-- | libqpdf/QPDF_Dictionary.cc | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/libqpdf/QPDF_Dictionary.cc b/libqpdf/QPDF_Dictionary.cc index f7e32fc9..53d78a2b 100644 --- a/libqpdf/QPDF_Dictionary.cc +++ b/libqpdf/QPDF_Dictionary.cc @@ -1,5 +1,6 @@ #include <qpdf/QPDF_Dictionary.hh> +#include <qpdf/JSON_writer.hh> #include <qpdf/QPDFObject_private.hh> #include <qpdf/QPDF_Name.hh> #include <qpdf/QPDF_Null.hh> @@ -91,6 +92,33 @@ QPDF_Dictionary::getJSON(int json_version) return j; } +void +QPDF_Dictionary::writeJSON(int json_version, JSON::Writer& p) +{ + p.writeStart('{'); + for (auto& iter: this->items) { + if (!iter.second.isNull()) { + p.writeNext(); + if (json_version == 1) { + p << "\"" << JSON::Writer::encode_string(QPDF_Name::normalizeName(iter.first)) << "\": "; + } else { + bool has_8bit_chars; + bool is_valid_utf8; + bool is_utf16; + QUtil::analyze_encoding(iter.first, has_8bit_chars, is_valid_utf8, is_utf16); + if (!has_8bit_chars || is_valid_utf8) { + p << "\"" << JSON::Writer::encode_string(iter.first) << "\": "; + } else { + p << "\"n:" << JSON::Writer::encode_string(QPDF_Name::normalizeName(iter.first)) + << "\": "; + } + } + iter.second.writeJSON(json_version, p); + } + } + p.writeEnd('}'); +} + bool QPDF_Dictionary::hasKey(std::string const& key) { |