Commit 28aa951ffe74250383aaca02c57ba442213308cb

Authored by m-holger
1 parent 0ab45001

Refactor JSON key handling in QPDFJob::doJSON

Replace repeated key checks with `want_key` lambda for clarity and maintainability. Transition `Pl_String` from shared to unique ownership to reflect intent and improve resource management. No functional changes introduced.
Showing 1 changed file with 17 additions and 11 deletions
libqpdf/QPDFJob.cc
... ... @@ -1606,9 +1606,9 @@ QPDFJob::doJSON(QPDF& pdf, Pipeline* p)
1606 1606 // are reserved for users.
1607 1607  
1608 1608 std::string captured_json;
1609   - std::shared_ptr<Pl_String> pl_str;
  1609 + std::unique_ptr<Pl_String> pl_str;
1610 1610 if (m->test_json_schema) {
1611   - pl_str = std::make_shared<Pl_String>("capture json", p, captured_json);
  1611 + pl_str = std::make_unique<Pl_String>("capture json", p, captured_json);
1612 1612 p = pl_str.get();
1613 1613 }
1614 1614  
... ... @@ -1642,43 +1642,49 @@ QPDFJob::doJSON(QPDF&amp; pdf, Pipeline* p)
1642 1642 j_params.addDictionaryMember("decodelevel", JSON::makeString(decode_level_str));
1643 1643 JSON::writeDictionaryItem(p, first, "parameters", j_params, 1);
1644 1644 }
1645   - bool all_keys = m->json_keys.empty();
  1645 +
  1646 + const bool all_keys = m->json_keys.empty();
  1647 +
  1648 + auto want_key = [&](std::string const& key) -> bool {
  1649 + return all_keys || m->json_keys.contains(key);
  1650 + };
  1651 +
1646 1652 // The list of selectable top-level keys id duplicated in the following places: job.yml,
1647 1653 // QPDFJob::json_schema, and QPDFJob::doJSON.
1648 1654  
1649 1655 // We do pages and pagelabels first since they have the side effect of repairing the pages tree,
1650 1656 // which could potentially impact object references in remaining items.
1651   - if (all_keys || m->json_keys.count("pages")) {
  1657 + if (want_key("pages")) {
1652 1658 doJSONPages(p, first, pdf);
1653 1659 }
1654   - if (all_keys || m->json_keys.count("pagelabels")) {
  1660 + if (want_key("pagelabels")) {
1655 1661 doJSONPageLabels(p, first, pdf);
1656 1662 }
1657 1663  
1658 1664 // The non-special keys are output in alphabetical order, but the order doesn't actually matter.
1659   - if (all_keys || m->json_keys.count("acroform")) {
  1665 + if (want_key("acroform")) {
1660 1666 doJSONAcroform(p, first, pdf);
1661 1667 }
1662   - if (all_keys || m->json_keys.count("attachments")) {
  1668 + if (want_key("attachments")) {
1663 1669 doJSONAttachments(p, first, pdf);
1664 1670 }
1665   - if (all_keys || m->json_keys.count("encrypt")) {
  1671 + if (want_key("encrypt")) {
1666 1672 doJSONEncrypt(p, first, pdf);
1667 1673 }
1668   - if (all_keys || m->json_keys.count("outlines")) {
  1674 + if (want_key("outlines")) {
1669 1675 doJSONOutlines(p, first, pdf);
1670 1676 }
1671 1677  
1672 1678 // We do objects last so their information is consistent with repairing the page tree. To see
1673 1679 // the original file with any page tree problems and the page tree not flattened, select
1674 1680 // qpdf/objects/objectinfo without other keys.
1675   - if (all_keys || m->json_keys.count("objects") || m->json_keys.count("qpdf")) {
  1681 + if (want_key("objects") || want_key("qpdf")) {
1676 1682 doJSONObjects(p, first, pdf);
1677 1683 }
1678 1684 if (m->json_version == 1) {
1679 1685 // "objectinfo" is not needed for version >1 since you can tell streams from other objects
1680 1686 // in "objects".
1681   - if (all_keys || m->json_keys.count("objectinfo")) {
  1687 + if (want_key("objectinfo")) {
1682 1688 doJSONObjectinfo(p, first, pdf);
1683 1689 }
1684 1690 }
... ...