Expose QPDFObjectHandle::writeJSON

This commit is contained in:
m-holger 2024-02-16 14:09:28 +00:00
parent c06653c3ab
commit f0bc2f11ef
4 changed files with 36 additions and 3 deletions

View File

@ -1197,6 +1197,13 @@ class QPDFObjectHandle
QPDF_DLL
JSON getJSON(int json_version, bool dereference_indirect = false);
// Write the object encoded as JSON to a pipeline. This is equivalent to, but more efficient
// than, calling getJSON(json_version, dereference_indirect).write(p, depth). See the
// documentation for getJSON and JSON::write for further detail.
QPDF_DLL
void
writeJSON(int json_version, Pipeline* p, bool dereference_indirect = false, size_t depth = 0);
// Deprecated version uses v1 for backward compatibility.
// ABI: remove for qpdf 12
[[deprecated("Use getJSON(int version)")]] QPDF_DLL JSON

View File

@ -1638,6 +1638,13 @@ QPDFObjectHandle::writeJSON(int json_version, JSON::Writer& p, bool dereference_
}
}
void
QPDFObjectHandle::writeJSON(int json_version, Pipeline* p, bool dereference_indirect, size_t depth)
{
JSON::Writer jw{p, depth};
writeJSON(json_version, jw, dereference_indirect);
}
JSON
QPDFObjectHandle::getStreamJSON(
int json_version,

View File

@ -350,7 +350,7 @@ $td->runtest("check C API write to JSON stream",
# (using #xx) would generate invalid JSON, even though qpdf's own JSON
# parser would accept it. Also, the JSON spec allows real numbers in
# scientific notation, but the PDF spec does not.
$n_tests += 6;
$n_tests += 7;
$td->runtest("handle binary names",
{$td->COMMAND =>
"qpdf --json-output weird-tokens.pdf a.json"},
@ -379,6 +379,9 @@ $td->runtest("check json",
{$td->FILE => "a.json"},
{$td->FILE => "weird-tokens-v1.json"},
$td->NORMALIZE_NEWLINES);
$td->runtest("write JSON to pipeline",
{$td->COMMAND => "test_driver 98 minimal.pdf ''"},
{$td->STRING => "test 98 done\n", $td->EXIT_STATUS => 0},
$td->NORMALIZE_NEWLINES);
cleanup();
$td->report($n_tests);

View File

@ -3380,6 +3380,22 @@ test_97(QPDF& pdf, char const* arg2)
assert(nulls.unparse() == nulls2.unparse());
}
static void
test_98(QPDF& pdf, char const* arg2)
{
// Test QPDFObjectHandle::writeJSON. This test is built for minimal.pdf.
for (int i = 1; i < 7; ++i) {
auto oh = pdf.getObject(i, 0);
Pl_Buffer bf1{"write", nullptr};
Pl_Buffer bf2{"get", nullptr};
oh.writeJSON(JSON::LATEST, &bf1, true, 7);
bf1.finish();
oh.getJSON(JSON::LATEST, true).write(&bf2, 7);
bf2.finish();
assert(bf1.getString() == bf2.getString());
}
}
void
runtest(int n, char const* filename1, char const* arg2)
{
@ -3481,7 +3497,7 @@ runtest(int n, char const* filename1, char const* arg2)
{78, test_78}, {79, test_79}, {80, test_80}, {81, test_81}, {82, test_82}, {83, test_83},
{84, test_84}, {85, test_85}, {86, test_86}, {87, test_87}, {88, test_88}, {89, test_89},
{90, test_90}, {91, test_91}, {92, test_92}, {93, test_93}, {94, test_94}, {95, test_95},
{96, test_96}, {97, test_97}};
{96, test_96}, {97, test_97}, {98, test_98}};
auto fn = test_functions.find(n);
if (fn == test_functions.end()) {