From e2737ab646bff6aa07ba72e0cc15cc955d9afcc0 Mon Sep 17 00:00:00 2001 From: m-holger Date: Fri, 9 Feb 2024 13:09:08 +0000 Subject: 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. --- libqpdf/QPDF_Dictionary.cc | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'libqpdf/QPDF_Dictionary.cc') 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 +#include #include #include #include @@ -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) { -- cgit v1.2.3-70-g09d2