diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc index 36c5aaea..2cb4bce5 100644 --- a/libqpdf/QPDF_Array.cc +++ b/libqpdf/QPDF_Array.cc @@ -151,20 +151,33 @@ QPDF_Array::unparse() JSON QPDF_Array::getJSON(int json_version) { + static const JSON j_null = JSON::makeNull(); + JSON j_array = JSON::makeArray(); if (sparse) { - JSON j = JSON::makeArray(); - for (int i = 0; i < sp_size; ++i) { - j.addArrayElement(at(i).getJSON(json_version)); + int next = 0; + for (auto& item: sp_elements) { + int key = item.first; + for (int j = next; j < key; ++j) { + j_array.addArrayElement(j_null); + } + auto og = item.second->getObjGen(); + j_array.addArrayElement( + og.isIndirect() ? JSON::makeString(og.unparse(' ') + " R") + : item.second->getJSON(json_version)); + next = ++key; + } + for (int j = next; j < sp_size; ++j) { + j_array.addArrayElement(j_null); } - return j; } else { - JSON j = JSON::makeArray(); - size_t size = elements.size(); - for (int i = 0; i < int(size); ++i) { - j.addArrayElement(at(i).getJSON(json_version)); + for (auto const& item: elements) { + auto og = item->getObjGen(); + j_array.addArrayElement( + og.isIndirect() ? JSON::makeString(og.unparse(' ') + " R") + : item->getJSON(json_version)); } - return j; } + return j_array; } QPDFObjectHandle diff --git a/libqpdf/QPDF_Null.cc b/libqpdf/QPDF_Null.cc index a82f23c0..0b59f5c9 100644 --- a/libqpdf/QPDF_Null.cc +++ b/libqpdf/QPDF_Null.cc @@ -50,5 +50,6 @@ QPDF_Null::unparse() JSON QPDF_Null::getJSON(int json_version) { + // If this is updated, QPDF_Array::getJSON must also be updated. return JSON::makeNull(); }