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_String.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_String.cc')
-rw-r--r-- | libqpdf/QPDF_String.cc | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/libqpdf/QPDF_String.cc b/libqpdf/QPDF_String.cc index 3886b399..d3fcaaef 100644 --- a/libqpdf/QPDF_String.cc +++ b/libqpdf/QPDF_String.cc @@ -1,5 +1,6 @@ #include <qpdf/QPDF_String.hh> +#include <qpdf/JSON_writer.hh> #include <qpdf/QUtil.hh> // DO NOT USE ctype -- it is locale dependent for some things, and it's not worth the risk of @@ -74,6 +75,30 @@ QPDF_String::getJSON(int json_version) return JSON::makeString(result); } +void +QPDF_String::writeJSON(int json_version, JSON::Writer& p) +{ + auto candidate = getUTF8Val(); + if (json_version == 1) { + + p << "\"" << JSON::Writer::encode_string(candidate) << "\""; + } else { + // See if we can unambiguously represent as Unicode. + if (QUtil::is_utf16(this->val) || QUtil::is_explicit_utf8(this->val)) { + p << "\"u:" << JSON::Writer::encode_string(candidate) <<"\""; + return; + } else if (!useHexString()) { + std::string test; + if (QUtil::utf8_to_pdf_doc(candidate, test, '?') && (test == this->val)) { + // This is a PDF-doc string that can be losslessly encoded as Unicode. + p << "\"u:" << JSON::Writer::encode_string(candidate) <<"\""; + return; + } + } + p << "\"b:" << QUtil::hex_encode(val) <<"\""; + } +} + bool QPDF_String::useHexString() const { |