aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDF_String.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2024-02-17 20:15:48 +0100
committerJay Berkenbilt <ejb@ql.org>2024-02-17 20:15:48 +0100
commite362bce8e86f4912eaa008bac06f9e2c19b72d3f (patch)
tree42fcd9eed699714f98ddee634763e4d670a2652b /libqpdf/QPDF_String.cc
parent5a29b7f9dd353c7baf2ca738bd2a56582a6525ea (diff)
parent413aba5bf2ef239d3abf024de3c819e153171035 (diff)
downloadqpdf-e362bce8e86f4912eaa008bac06f9e2c19b72d3f.tar.zst
Merge branch 'jw' from #1146 into work
Diffstat (limited to 'libqpdf/QPDF_String.cc')
-rw-r--r--libqpdf/QPDF_String.cc42
1 files changed, 19 insertions, 23 deletions
diff --git a/libqpdf/QPDF_String.cc b/libqpdf/QPDF_String.cc
index 3886b399..f425b313 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
@@ -45,33 +46,28 @@ QPDF_String::unparse()
return unparse(false);
}
-JSON
-QPDF_String::getJSON(int json_version)
+void
+QPDF_String::writeJSON(int json_version, JSON::Writer& p)
{
+ auto candidate = getUTF8Val();
if (json_version == 1) {
- return JSON::makeString(getUTF8Val());
- }
- // See if we can unambiguously represent as Unicode.
- bool is_unicode = false;
- std::string result;
- std::string candidate = getUTF8Val();
- if (QUtil::is_utf16(this->val) || QUtil::is_explicit_utf8(this->val)) {
- is_unicode = true;
- result = candidate;
- } 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.
- is_unicode = true;
- result = candidate;
- }
- }
- if (is_unicode) {
- result = "u:" + result;
+
+ p << "\"" << JSON::Writer::encode_string(candidate) << "\"";
} else {
- result = "b:" + QUtil::hex_encode(this->val);
+ // 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) <<"\"";
}
- return JSON::makeString(result);
}
bool