Commit b9bc05356ac6823e3e105ea25908e826d7b731fd
1 parent
b15d0bf6
Refactor QPDF::writeJSONStream
Use QPDF_Stream::writeStreamJSON. Factor out the json_stream_data == qpdf_sj_file case.
Showing
2 changed files
with
41 additions
and
38 deletions
include/qpdf/QPDF.hh
| ... | ... | @@ -1411,19 +1411,6 @@ class QPDF |
| 1411 | 1411 | // JSON import |
| 1412 | 1412 | void importJSON(std::shared_ptr<InputSource>, bool must_be_complete); |
| 1413 | 1413 | |
| 1414 | - // JSON write | |
| 1415 | - void writeJSONStream( | |
| 1416 | - int version, | |
| 1417 | - Pipeline* p, | |
| 1418 | - bool& first, | |
| 1419 | - std::string const& key, | |
| 1420 | - QPDFObjectHandle&, | |
| 1421 | - qpdf_stream_decode_level_e, | |
| 1422 | - qpdf_json_stream_data_e, | |
| 1423 | - std::string const& file_prefix); | |
| 1424 | - void writeJSONObject( | |
| 1425 | - int version, Pipeline* p, bool& first, std::string const& key, QPDFObjectHandle&); | |
| 1426 | - | |
| 1427 | 1414 | // Type conversion helper methods |
| 1428 | 1415 | template <typename T> |
| 1429 | 1416 | static qpdf_offset_t | ... | ... |
libqpdf/QPDF_json.cc
| ... | ... | @@ -8,6 +8,7 @@ |
| 8 | 8 | #include <qpdf/QPDFObject_private.hh> |
| 9 | 9 | #include <qpdf/QPDFValue.hh> |
| 10 | 10 | #include <qpdf/QPDF_Null.hh> |
| 11 | +#include <qpdf/QPDF_Stream.hh> | |
| 11 | 12 | #include <qpdf/QTC.hh> |
| 12 | 13 | #include <qpdf/QUtil.hh> |
| 13 | 14 | #include <algorithm> |
| ... | ... | @@ -443,7 +444,9 @@ void |
| 443 | 444 | QPDF::JSONReactor::replaceObject(QPDFObjectHandle&& replacement, JSON const& value) |
| 444 | 445 | { |
| 445 | 446 | if (replacement.isIndirect()) { |
| 446 | - error(replacement.getParsedOffset(), "the value of an object may not be an indirect object reference"); | |
| 447 | + error( | |
| 448 | + replacement.getParsedOffset(), | |
| 449 | + "the value of an object may not be an indirect object reference"); | |
| 447 | 450 | return; |
| 448 | 451 | } |
| 449 | 452 | auto& tos = stack.back(); |
| ... | ... | @@ -829,40 +832,51 @@ QPDF::importJSON(std::shared_ptr<InputSource> is, bool must_be_complete) |
| 829 | 832 | } |
| 830 | 833 | |
| 831 | 834 | void |
| 832 | -QPDF::writeJSONStream( | |
| 835 | +writeJSONStreamFile( | |
| 836 | + int version, | |
| 837 | + JSON::Writer& jw, | |
| 838 | + QPDF_Stream& stream, | |
| 839 | + int id, | |
| 840 | + qpdf_stream_decode_level_e decode_level, | |
| 841 | + std::string const& file_prefix) | |
| 842 | +{ | |
| 843 | + auto filename = file_prefix + "-" + std::to_string(id); | |
| 844 | + auto* f = QUtil::safe_fopen(filename.c_str(), "wb"); | |
| 845 | + Pl_StdioFile f_pl{"stream data", f}; | |
| 846 | + stream.writeStreamJSON(version, jw, qpdf_sj_file, decode_level, &f_pl, filename); | |
| 847 | + f_pl.finish(); | |
| 848 | + fclose(f); | |
| 849 | +} | |
| 850 | + | |
| 851 | +void | |
| 852 | +writeJSONStream( | |
| 833 | 853 | int version, |
| 834 | 854 | Pipeline* p, |
| 835 | 855 | bool& first, |
| 836 | 856 | std::string const& key, |
| 837 | - QPDFObjectHandle& obj, | |
| 857 | + QPDF_Stream& stream, | |
| 858 | + int id, | |
| 838 | 859 | qpdf_stream_decode_level_e decode_level, |
| 839 | 860 | qpdf_json_stream_data_e json_stream_data, |
| 840 | 861 | std::string const& file_prefix) |
| 841 | 862 | { |
| 842 | - Pipeline* stream_p = nullptr; | |
| 843 | - FILE* f = nullptr; | |
| 844 | - std::shared_ptr<Pl_StdioFile> f_pl; | |
| 845 | - std::string filename; | |
| 846 | - if (json_stream_data == qpdf_sj_file) { | |
| 847 | - filename = file_prefix + "-" + std::to_string(obj.getObjectID()); | |
| 848 | - f = QUtil::safe_fopen(filename.c_str(), "wb"); | |
| 849 | - f_pl = std::make_shared<Pl_StdioFile>("stream data", f); | |
| 850 | - stream_p = f_pl.get(); | |
| 863 | + if (first) { | |
| 864 | + *p << "\n \"" << key << "\": {\n \"stream\": "; | |
| 865 | + first = false; | |
| 866 | + } else { | |
| 867 | + *p << ",\n \"" << key << "\": {\n \"stream\": "; | |
| 851 | 868 | } |
| 852 | - auto j = JSON::makeDictionary(); | |
| 853 | - j.addDictionaryMember( | |
| 854 | - "stream", obj.getStreamJSON(version, json_stream_data, decode_level, stream_p, filename)); | |
| 855 | - | |
| 856 | - JSON::writeDictionaryItem(p, first, key, j, 3); | |
| 857 | - if (f) { | |
| 858 | - f_pl->finish(); | |
| 859 | - f_pl = nullptr; | |
| 860 | - fclose(f); | |
| 869 | + JSON::Writer jw{p, 4}; | |
| 870 | + if (json_stream_data == qpdf_sj_file) { | |
| 871 | + writeJSONStreamFile(version, jw, stream, id, decode_level, file_prefix); | |
| 872 | + } else { | |
| 873 | + stream.writeStreamJSON(version, jw, json_stream_data, decode_level, nullptr, ""); | |
| 861 | 874 | } |
| 875 | + *p << "\n }"; | |
| 862 | 876 | } |
| 863 | 877 | |
| 864 | 878 | void |
| 865 | -QPDF::writeJSONObject( | |
| 879 | +writeJSONObject( | |
| 866 | 880 | int version, Pipeline* p, bool& first, std::string const& key, QPDFObjectHandle& obj) |
| 867 | 881 | { |
| 868 | 882 | if (first) { |
| ... | ... | @@ -947,15 +961,17 @@ QPDF::writeJSON( |
| 947 | 961 | JSON::writeDictionaryOpen(p, first_qpdf_inner, depth_qpdf); |
| 948 | 962 | bool all_objects = wanted_objects.empty(); |
| 949 | 963 | for (auto& obj: getAllObjects()) { |
| 950 | - std::string key = "obj:" + obj.unparse(); | |
| 964 | + auto const og = obj.getObjGen(); | |
| 965 | + std::string key = "obj:" + og.unparse(' ') + " R"; | |
| 951 | 966 | if (all_objects || wanted_objects.count(key)) { |
| 952 | - if (obj.isStream()) { | |
| 967 | + if (auto* stream = obj.getObjectPtr()->as<QPDF_Stream>()) { | |
| 953 | 968 | writeJSONStream( |
| 954 | 969 | version, |
| 955 | 970 | p, |
| 956 | 971 | first_qpdf_inner, |
| 957 | 972 | key, |
| 958 | - obj, | |
| 973 | + *stream, | |
| 974 | + og.getObj(), | |
| 959 | 975 | decode_level, |
| 960 | 976 | json_stream_data, |
| 961 | 977 | file_prefix); | ... | ... |