Commit 8ff20b00897acecc6908c98fab72db1c57692abb
1 parent
4660e232
Allow "n:/pdf-syntax" JSON syntax for dictionary keys
Showing
5 changed files
with
28 additions
and
12 deletions
libqpdf/QPDF_Dictionary.cc
| @@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
| 3 | #include <qpdf/QPDFObject_private.hh> | 3 | #include <qpdf/QPDFObject_private.hh> |
| 4 | #include <qpdf/QPDF_Name.hh> | 4 | #include <qpdf/QPDF_Name.hh> |
| 5 | #include <qpdf/QPDF_Null.hh> | 5 | #include <qpdf/QPDF_Null.hh> |
| 6 | +#include <qpdf/QUtil.hh> | ||
| 6 | 7 | ||
| 7 | using namespace std::literals; | 8 | using namespace std::literals; |
| 8 | 9 | ||
| @@ -72,9 +73,19 @@ QPDF_Dictionary::getJSON(int json_version) | @@ -72,9 +73,19 @@ QPDF_Dictionary::getJSON(int json_version) | ||
| 72 | JSON j = JSON::makeDictionary(); | 73 | JSON j = JSON::makeDictionary(); |
| 73 | for (auto& iter: this->items) { | 74 | for (auto& iter: this->items) { |
| 74 | if (!iter.second.isNull()) { | 75 | if (!iter.second.isNull()) { |
| 75 | - std::string key = | ||
| 76 | - (json_version == 1 ? QPDF_Name::normalizeName(iter.first) : iter.first); | ||
| 77 | - j.addDictionaryMember(key, iter.second.getJSON(json_version)); | 76 | + if (json_version == 1) { |
| 77 | + j.addDictionaryMember( | ||
| 78 | + QPDF_Name::normalizeName(iter.first), iter.second.getJSON(json_version)); | ||
| 79 | + } else { | ||
| 80 | + bool has_8bit_chars; | ||
| 81 | + bool is_valid_utf8; | ||
| 82 | + bool is_utf16; | ||
| 83 | + QUtil::analyze_encoding(iter.first, has_8bit_chars, is_valid_utf8, is_utf16); | ||
| 84 | + std::string key = !has_8bit_chars || is_valid_utf8 | ||
| 85 | + ? iter.first | ||
| 86 | + : "n:" + QPDF_Name::normalizeName(iter.first); | ||
| 87 | + j.addDictionaryMember(key, iter.second.getJSON(json_version)); | ||
| 88 | + } | ||
| 78 | } | 89 | } |
| 79 | } | 90 | } |
| 80 | return j; | 91 | return j; |
libqpdf/QPDF_json.cc
| @@ -666,7 +666,9 @@ QPDF::JSONReactor::dictionaryItem(std::string const& key, JSON const& value) | @@ -666,7 +666,9 @@ QPDF::JSONReactor::dictionaryItem(std::string const& key, JSON const& value) | ||
| 666 | if (dict.isStream()) { | 666 | if (dict.isStream()) { |
| 667 | dict = dict.getDict(); | 667 | dict = dict.getDict(); |
| 668 | } | 668 | } |
| 669 | - dict.replaceKey(key, makeObject(value)); | 669 | + dict.replaceKey( |
| 670 | + is_pdf_name(key) ? QPDFObjectHandle::parse(key.substr(2)).getName() : key, | ||
| 671 | + makeObject(value)); | ||
| 670 | } | 672 | } |
| 671 | } else { | 673 | } else { |
| 672 | throw std::logic_error("QPDF_json: unknown state " + std::to_string(state)); | 674 | throw std::logic_error("QPDF_json: unknown state " + std::to_string(state)); |
qpdf/qtest/qpdf/weird-tokens-alt.json
qpdf/qtest/qpdf/weird-tokens.json
qpdf/qtest/qpdf/weird-tokens.pdf
| @@ -21,6 +21,7 @@ | @@ -21,6 +21,7 @@ | ||
| 21 | ] | 21 | ] |
| 22 | /Pages 2 0 R | 22 | /Pages 2 0 R |
| 23 | /Type /Catalog | 23 | /Type /Catalog |
| 24 | + /WeirdKey+#ba#da#cc#e5 42 | ||
| 24 | >> | 25 | >> |
| 25 | endobj | 26 | endobj |
| 26 | 27 | ||
| @@ -85,16 +86,16 @@ xref | @@ -85,16 +86,16 @@ xref | ||
| 85 | 0 7 | 86 | 0 7 |
| 86 | 0000000000 65535 f | 87 | 0000000000 65535 f |
| 87 | 0000000025 00000 n | 88 | 0000000025 00000 n |
| 88 | -0000000361 00000 n | ||
| 89 | -0000000443 00000 n | ||
| 90 | -0000000639 00000 n | ||
| 91 | -0000000738 00000 n | ||
| 92 | -0000000757 00000 n | 89 | +0000000389 00000 n |
| 90 | +0000000471 00000 n | ||
| 91 | +0000000667 00000 n | ||
| 92 | +0000000766 00000 n | ||
| 93 | +0000000785 00000 n | ||
| 93 | trailer << | 94 | trailer << |
| 94 | /Root 1 0 R | 95 | /Root 1 0 R |
| 95 | /Size 7 | 96 | /Size 7 |
| 96 | /ID [<42841c13bbf709d79a200fa1691836f8><728c020f464c3cf7e02c12605fa7d88b>] | 97 | /ID [<42841c13bbf709d79a200fa1691836f8><728c020f464c3cf7e02c12605fa7d88b>] |
| 97 | >> | 98 | >> |
| 98 | startxref | 99 | startxref |
| 99 | -863 | 100 | +891 |
| 100 | %%EOF | 101 | %%EOF |