2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-06-01 01:40:51 +00:00

Allow "n:/pdf-syntax" JSON syntax for dictionary keys

This commit is contained in:
m-holger 2024-01-29 13:22:58 +00:00
parent 4660e23262
commit 8ff20b0089
5 changed files with 28 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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

View File

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