Commit f0bc2f11ef4f096b1338dd5fc91e3c4d88b3b9e0

Authored by m-holger
1 parent c06653c3

Expose QPDFObjectHandle::writeJSON

include/qpdf/QPDFObjectHandle.hh
... ... @@ -1197,6 +1197,13 @@ class QPDFObjectHandle
1197 1197 QPDF_DLL
1198 1198 JSON getJSON(int json_version, bool dereference_indirect = false);
1199 1199  
  1200 + // Write the object encoded as JSON to a pipeline. This is equivalent to, but more efficient
  1201 + // than, calling getJSON(json_version, dereference_indirect).write(p, depth). See the
  1202 + // documentation for getJSON and JSON::write for further detail.
  1203 + QPDF_DLL
  1204 + void
  1205 + writeJSON(int json_version, Pipeline* p, bool dereference_indirect = false, size_t depth = 0);
  1206 +
1200 1207 // Deprecated version uses v1 for backward compatibility.
1201 1208 // ABI: remove for qpdf 12
1202 1209 [[deprecated("Use getJSON(int version)")]] QPDF_DLL JSON
... ...
libqpdf/QPDFObjectHandle.cc
... ... @@ -1638,6 +1638,13 @@ QPDFObjectHandle::writeJSON(int json_version, JSON::Writer& p, bool dereference_
1638 1638 }
1639 1639 }
1640 1640  
  1641 +void
  1642 +QPDFObjectHandle::writeJSON(int json_version, Pipeline* p, bool dereference_indirect, size_t depth)
  1643 +{
  1644 + JSON::Writer jw{p, depth};
  1645 + writeJSON(json_version, jw, dereference_indirect);
  1646 +}
  1647 +
1641 1648 JSON
1642 1649 QPDFObjectHandle::getStreamJSON(
1643 1650 int json_version,
... ...
qpdf/qtest/qpdf-json.test
... ... @@ -350,7 +350,7 @@ $td->runtest("check C API write to JSON stream",
350 350 # (using #xx) would generate invalid JSON, even though qpdf's own JSON
351 351 # parser would accept it. Also, the JSON spec allows real numbers in
352 352 # scientific notation, but the PDF spec does not.
353   -$n_tests += 6;
  353 +$n_tests += 7;
354 354 $td->runtest("handle binary names",
355 355 {$td->COMMAND =>
356 356 "qpdf --json-output weird-tokens.pdf a.json"},
... ... @@ -379,6 +379,9 @@ $td->runtest("check json",
379 379 {$td->FILE => "a.json"},
380 380 {$td->FILE => "weird-tokens-v1.json"},
381 381 $td->NORMALIZE_NEWLINES);
382   -
  382 +$td->runtest("write JSON to pipeline",
  383 + {$td->COMMAND => "test_driver 98 minimal.pdf ''"},
  384 + {$td->STRING => "test 98 done\n", $td->EXIT_STATUS => 0},
  385 + $td->NORMALIZE_NEWLINES);
383 386 cleanup();
384 387 $td->report($n_tests);
... ...
qpdf/test_driver.cc
... ... @@ -3380,6 +3380,22 @@ test_97(QPDF& pdf, char const* arg2)
3380 3380 assert(nulls.unparse() == nulls2.unparse());
3381 3381 }
3382 3382  
  3383 +static void
  3384 +test_98(QPDF& pdf, char const* arg2)
  3385 +{
  3386 + // Test QPDFObjectHandle::writeJSON. This test is built for minimal.pdf.
  3387 + for (int i = 1; i < 7; ++i) {
  3388 + auto oh = pdf.getObject(i, 0);
  3389 + Pl_Buffer bf1{"write", nullptr};
  3390 + Pl_Buffer bf2{"get", nullptr};
  3391 + oh.writeJSON(JSON::LATEST, &bf1, true, 7);
  3392 + bf1.finish();
  3393 + oh.getJSON(JSON::LATEST, true).write(&bf2, 7);
  3394 + bf2.finish();
  3395 + assert(bf1.getString() == bf2.getString());
  3396 + }
  3397 +}
  3398 +
3383 3399 void
3384 3400 runtest(int n, char const* filename1, char const* arg2)
3385 3401 {
... ... @@ -3481,7 +3497,7 @@ runtest(int n, char const* filename1, char const* arg2)
3481 3497 {78, test_78}, {79, test_79}, {80, test_80}, {81, test_81}, {82, test_82}, {83, test_83},
3482 3498 {84, test_84}, {85, test_85}, {86, test_86}, {87, test_87}, {88, test_88}, {89, test_89},
3483 3499 {90, test_90}, {91, test_91}, {92, test_92}, {93, test_93}, {94, test_94}, {95, test_95},
3484   - {96, test_96}, {97, test_97}};
  3500 + {96, test_96}, {97, test_97}, {98, test_98}};
3485 3501  
3486 3502 auto fn = test_functions.find(n);
3487 3503 if (fn == test_functions.end()) {
... ...