Merge pull request #1138 from m-holger/j_key

Allow "n:/pdf-syntax" JSON syntax for dictionary keys
This commit is contained in:
Jay Berkenbilt 2024-02-03 13:09:31 -05:00 committed by GitHub
commit 532cc58d7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 28 additions and 12 deletions

View File

@ -3,6 +3,7 @@
#include <qpdf/QPDFObject_private.hh>
#include <qpdf/QPDF_Name.hh>
#include <qpdf/QPDF_Null.hh>
#include <qpdf/QUtil.hh>
using namespace std::literals;
@ -72,9 +73,19 @@ QPDF_Dictionary::getJSON(int json_version)
JSON j = JSON::makeDictionary();
for (auto& iter: this->items) {
if (!iter.second.isNull()) {
std::string key =
(json_version == 1 ? QPDF_Name::normalizeName(iter.first) : iter.first);
j.addDictionaryMember(key, iter.second.getJSON(json_version));
if (json_version == 1) {
j.addDictionaryMember(
QPDF_Name::normalizeName(iter.first), iter.second.getJSON(json_version));
} 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);
std::string key = !has_8bit_chars || is_valid_utf8
? iter.first
: "n:" + QPDF_Name::normalizeName(iter.first);
j.addDictionaryMember(key, iter.second.getJSON(json_version));
}
}
}
return j;

View File

@ -666,7 +666,9 @@ QPDF::JSONReactor::dictionaryItem(std::string const& key, JSON const& value)
if (dict.isStream()) {
dict = dict.getDict();
}
dict.replaceKey(key, makeObject(value));
dict.replaceKey(
is_pdf_name(key) ? QPDFObjectHandle::parse(key.substr(2)).getName() : key,
makeObject(value));
}
} else {
throw std::logic_error("QPDF_json: unknown state " + std::to_string(state));

View File

@ -26,7 +26,8 @@
1e12
],
"/Pages": "2 0 R",
"/Type": "/Catalog"
"/Type": "/Catalog",
"n:/WeirdKey+#ba#da#cc#e5": 42
}
},
"obj:2 0 R": {

View File

@ -26,7 +26,8 @@
1000000000000
],
"/Pages": "2 0 R",
"/Type": "/Catalog"
"/Type": "/Catalog",
"n:/WeirdKey+#ba#da#cc#e5": 42
}
},
"obj:2 0 R": {

View File

@ -21,6 +21,7 @@
]
/Pages 2 0 R
/Type /Catalog
/WeirdKey+#ba#da#cc#e5 42
>>
endobj
@ -85,16 +86,16 @@ xref
0 7
0000000000 65535 f
0000000025 00000 n
0000000361 00000 n
0000000443 00000 n
0000000639 00000 n
0000000738 00000 n
0000000757 00000 n
0000000389 00000 n
0000000471 00000 n
0000000667 00000 n
0000000766 00000 n
0000000785 00000 n
trailer <<
/Root 1 0 R
/Size 7
/ID [<42841c13bbf709d79a200fa1691836f8><728c020f464c3cf7e02c12605fa7d88b>]
>>
startxref
863
891
%%EOF