filter.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #define ARDUINOJSON_ENABLE_COMMENTS 1
  5. #include <ArduinoJson.h>
  6. #include <catch.hpp>
  7. #include <sstream>
  8. #include <string>
  9. #include "Allocators.hpp"
  10. #include "Literals.hpp"
  11. using ArduinoJson::detail::sizeofArray;
  12. using ArduinoJson::detail::sizeofObject;
  13. TEST_CASE("Filtering") {
  14. struct TestCase {
  15. const char* description;
  16. const char* input;
  17. const char* filter;
  18. uint8_t nestingLimit;
  19. DeserializationError error;
  20. const char* output;
  21. size_t memoryUsage;
  22. };
  23. TestCase testCases[] = {
  24. {
  25. "Input is object, filter is null", // description
  26. "{\"hello\":\"world\"}", // input
  27. "null", // filter
  28. 10, // nestingLimit
  29. DeserializationError::Ok, // error
  30. "null", // output
  31. 0, // memoryUsage
  32. },
  33. {
  34. "Input is object, filter is false",
  35. "{\"hello\":\"world\"}",
  36. "false",
  37. 10,
  38. DeserializationError::Ok,
  39. "null",
  40. 0,
  41. },
  42. {
  43. "Input is object, filter is true",
  44. "{\"abcdefg\":\"hijklmn\"}",
  45. "true",
  46. 10,
  47. DeserializationError::Ok,
  48. "{\"abcdefg\":\"hijklmn\"}",
  49. sizeofObject(1) + sizeofString("abcdefg") + sizeofString("hijklmn"),
  50. },
  51. {
  52. "Input is object, filter is empty object",
  53. "{\"hello\":\"world\"}",
  54. "{}",
  55. 10,
  56. DeserializationError::Ok,
  57. "{}",
  58. sizeofObject(0),
  59. },
  60. {
  61. "Input in an object, but filter wants an array",
  62. "{\"hello\":\"world\"}",
  63. "[]",
  64. 10,
  65. DeserializationError::Ok,
  66. "null",
  67. 0,
  68. },
  69. {
  70. "Member is a string, but filter wants an array",
  71. "{\"example\":\"example\"}",
  72. "{\"example\":[true]}",
  73. 10,
  74. DeserializationError::Ok,
  75. "{\"example\":null}",
  76. sizeofObject(1) + sizeofString("example"),
  77. },
  78. {
  79. "Member is a number, but filter wants an array",
  80. "{\"example\":42}",
  81. "{\"example\":[true]}",
  82. 10,
  83. DeserializationError::Ok,
  84. "{\"example\":null}",
  85. sizeofObject(1) + sizeofString("example"),
  86. },
  87. {
  88. "Input is an array, but filter wants an object",
  89. "[\"hello\",\"world\"]",
  90. "{}",
  91. 10,
  92. DeserializationError::Ok,
  93. "null",
  94. 0,
  95. },
  96. {
  97. "Input is a bool, but filter wants an object",
  98. "true",
  99. "{}",
  100. 10,
  101. DeserializationError::Ok,
  102. "null",
  103. 0,
  104. },
  105. {
  106. "Input is a string, but filter wants an object",
  107. "\"hello\"",
  108. "{}",
  109. 10,
  110. DeserializationError::Ok,
  111. "null",
  112. 0,
  113. },
  114. {
  115. "Skip an integer",
  116. "{\"an_integer\":666,example:42}",
  117. "{\"example\":true}",
  118. 10,
  119. DeserializationError::Ok,
  120. "{\"example\":42}",
  121. sizeofObject(1) + sizeofString("example"),
  122. },
  123. {
  124. "Skip a float",
  125. "{\"a_float\":12.34e-6,example:42}",
  126. "{\"example\":true}",
  127. 10,
  128. DeserializationError::Ok,
  129. "{\"example\":42}",
  130. sizeofObject(1) + sizeofString("example"),
  131. },
  132. {
  133. "Skip false",
  134. "{\"a_bool\":false,example:42}",
  135. "{\"example\":true}",
  136. 10,
  137. DeserializationError::Ok,
  138. "{\"example\":42}",
  139. sizeofObject(1) + sizeofString("example"),
  140. },
  141. {
  142. "Skip true",
  143. "{\"a_bool\":true,example:42}",
  144. "{\"example\":true}",
  145. 10,
  146. DeserializationError::Ok,
  147. "{\"example\":42}",
  148. sizeofObject(1) + sizeofString("example"),
  149. },
  150. {
  151. "Skip null",
  152. "{\"a_bool\":null,example:42}",
  153. "{\"example\":true}",
  154. 10,
  155. DeserializationError::Ok,
  156. "{\"example\":42}",
  157. sizeofObject(1) + sizeofString("example"),
  158. },
  159. {
  160. "Skip a double-quoted string",
  161. "{\"a_double_quoted_string\":\"hello\",example:42}",
  162. "{\"example\":true}",
  163. 10,
  164. DeserializationError::Ok,
  165. "{\"example\":42}",
  166. sizeofObject(1) + sizeofString("example"),
  167. },
  168. {
  169. "Skip a single-quoted string",
  170. "{\"a_single_quoted_string\":'hello',example:42}",
  171. "{\"example\":true}",
  172. 10,
  173. DeserializationError::Ok,
  174. "{\"example\":42}",
  175. sizeofObject(1) + sizeofString("example"),
  176. },
  177. {
  178. "Skip an empty array",
  179. "{\"an_empty_array\":[],example:42}",
  180. "{\"example\":true}",
  181. 10,
  182. DeserializationError::Ok,
  183. "{\"example\":42}",
  184. sizeofObject(1) + sizeofString("example"),
  185. },
  186. {
  187. "Skip an empty array with spaces in it",
  188. "{\"an_empty_array\":[\t],example:42}",
  189. "{\"example\":true}",
  190. 10,
  191. DeserializationError::Ok,
  192. "{\"example\":42}",
  193. sizeofObject(1) + sizeofString("example"),
  194. },
  195. {
  196. "Skip an array",
  197. "{\"an_array\":[1,2,3],example:42}",
  198. "{\"example\":true}",
  199. 10,
  200. DeserializationError::Ok,
  201. "{\"example\":42}",
  202. sizeofObject(1) + sizeofString("example"),
  203. },
  204. {
  205. "Skip an array with spaces in it",
  206. "{\"an_array\": [ 1 , 2 , 3 ] ,example:42}",
  207. "{\"example\":true}",
  208. 10,
  209. DeserializationError::Ok,
  210. "{\"example\":42}",
  211. sizeofObject(1) + sizeofString("example"),
  212. },
  213. {
  214. "Skip an empty nested object",
  215. "{\"an_empty_object\":{},example:42}",
  216. "{\"example\":true}",
  217. 10,
  218. DeserializationError::Ok,
  219. "{\"example\":42}",
  220. sizeofObject(1) + sizeofString("example"),
  221. },
  222. {
  223. "Skip an empty nested object with spaces in it",
  224. "{\"an_empty_object\":{ },example:42}",
  225. "{\"example\":true}",
  226. 10,
  227. DeserializationError::Ok,
  228. "{\"example\":42}",
  229. sizeofObject(1) + sizeofString("example"),
  230. },
  231. {
  232. "Skip a nested object",
  233. "{\"an_object\":{a:1,'b':2,\"c\":3},example:42}",
  234. "{\"example\":true}",
  235. 10,
  236. DeserializationError::Ok,
  237. "{\"example\":42}",
  238. sizeofObject(1) + sizeofString("example"),
  239. },
  240. {
  241. "Skip an object with spaces in it",
  242. "{\"an_object\" : { a : 1 , 'b' : 2 , \"c\" : 3 } ,example:42}",
  243. "{\"example\":true}",
  244. 10,
  245. DeserializationError::Ok,
  246. "{\"example\":42}",
  247. sizeofObject(1) + sizeofString("example"),
  248. },
  249. {
  250. "Skip a string in a nested object",
  251. "{\"an_integer\": 0,\"example\":{\"type\":\"int\",\"outcome\":42}}",
  252. "{\"example\":{\"outcome\":true}}",
  253. 10,
  254. DeserializationError::Ok,
  255. "{\"example\":{\"outcome\":42}}",
  256. 2 * sizeofObject(1) + 2 * sizeofString("example"),
  257. },
  258. {
  259. "wildcard",
  260. "{\"example\":{\"type\":\"int\",\"outcome\":42}}",
  261. "{\"*\":{\"outcome\":true}}",
  262. 10,
  263. DeserializationError::Ok,
  264. "{\"example\":{\"outcome\":42}}",
  265. 2 * sizeofObject(1) + 2 * sizeofString("example"),
  266. },
  267. {
  268. "exclusion filter (issue #1628)",
  269. "{\"example\":1,\"ignored\":2}",
  270. "{\"*\":true,\"ignored\":false}",
  271. 10,
  272. DeserializationError::Ok,
  273. "{\"example\":1}",
  274. sizeofObject(1) + sizeofString("example"),
  275. },
  276. {
  277. "only the first element of array counts",
  278. "[1,2,3]",
  279. "[true, false]",
  280. 10,
  281. DeserializationError::Ok,
  282. "[1,2,3]",
  283. sizeofArray(3),
  284. },
  285. {
  286. "only the first element of array counts",
  287. "[1,2,3]",
  288. "[false, true]",
  289. 10,
  290. DeserializationError::Ok,
  291. "[]",
  292. sizeofArray(0),
  293. },
  294. {
  295. "filter members of object in array",
  296. "[{\"example\":1,\"ignore\":2},{\"example\":3,\"ignore\":4}]",
  297. "[{\"example\":true}]",
  298. 10,
  299. DeserializationError::Ok,
  300. "[{\"example\":1},{\"example\":3}]",
  301. sizeofArray(2) + 2 * sizeofObject(1) + sizeofString("example"),
  302. },
  303. {
  304. "Unclosed single quote in skipped element",
  305. "[',2,3]",
  306. "[false,true]",
  307. 10,
  308. DeserializationError::IncompleteInput,
  309. "[]",
  310. sizeofArray(0),
  311. },
  312. {
  313. "Unclosed double quote in skipped element",
  314. "[\",2,3]",
  315. "[false,true]",
  316. 10,
  317. DeserializationError::IncompleteInput,
  318. "[]",
  319. sizeofArray(0),
  320. },
  321. {
  322. "Detect errors in skipped value",
  323. "[!,2,\\]",
  324. "[false]",
  325. 10,
  326. DeserializationError::InvalidInput,
  327. "[]",
  328. sizeofArray(0),
  329. },
  330. {
  331. "Detect incomplete string event if it's skipped",
  332. "\"ABC",
  333. "false",
  334. 10,
  335. DeserializationError::IncompleteInput,
  336. "null",
  337. 0,
  338. },
  339. {
  340. "Detect incomplete string event if it's skipped",
  341. "'ABC",
  342. "false",
  343. 10,
  344. DeserializationError::IncompleteInput,
  345. "null",
  346. 0,
  347. },
  348. {
  349. "Handle escaped quotes",
  350. "'A\\'BC'",
  351. "false",
  352. 10,
  353. DeserializationError::Ok,
  354. "null",
  355. 0,
  356. },
  357. {
  358. "Handle escaped quotes",
  359. "\"A\\\"BC\"",
  360. "false",
  361. 10,
  362. DeserializationError::Ok,
  363. "null",
  364. 0,
  365. },
  366. {
  367. "Detect incomplete string in presence of escaped quotes",
  368. "'A\\'BC",
  369. "false",
  370. 10,
  371. DeserializationError::IncompleteInput,
  372. "null",
  373. 0,
  374. },
  375. {
  376. "Detect incomplete string in presence of escaped quotes",
  377. "\"A\\\"BC",
  378. "false",
  379. 10,
  380. DeserializationError::IncompleteInput,
  381. "null",
  382. 0,
  383. },
  384. {
  385. "skip empty array",
  386. "[]",
  387. "false",
  388. 10,
  389. DeserializationError::Ok,
  390. "null",
  391. 0,
  392. },
  393. {
  394. "Skip empty array with spaces",
  395. " [ ] ",
  396. "false",
  397. 10,
  398. DeserializationError::Ok,
  399. "null",
  400. 0,
  401. },
  402. {
  403. "Bubble up element error even if array is skipped",
  404. "[1,'2,3]",
  405. "false",
  406. 10,
  407. DeserializationError::IncompleteInput,
  408. "null",
  409. 0,
  410. },
  411. {
  412. "Bubble up member error even if object is skipped",
  413. "{'hello':'worl}",
  414. "false",
  415. 10,
  416. DeserializationError::IncompleteInput,
  417. "null",
  418. 0,
  419. },
  420. {
  421. "Bubble up colon error even if object is skipped",
  422. "{'hello','world'}",
  423. "false",
  424. 10,
  425. DeserializationError::InvalidInput,
  426. "null",
  427. 0,
  428. },
  429. {
  430. "Bubble up key error even if object is skipped",
  431. "{'hello:1}",
  432. "false",
  433. 10,
  434. DeserializationError::IncompleteInput,
  435. "null",
  436. 0,
  437. },
  438. {
  439. "Detect invalid value in skipped object",
  440. "{'hello':!}",
  441. "false",
  442. 10,
  443. DeserializationError::InvalidInput,
  444. "null",
  445. 0,
  446. },
  447. {
  448. "Ignore invalid value in skipped object",
  449. "{'hello':\\}",
  450. "false",
  451. 10,
  452. DeserializationError::InvalidInput,
  453. "null",
  454. 0,
  455. },
  456. {
  457. "Check nesting limit even for ignored objects",
  458. "{}",
  459. "false",
  460. 0,
  461. DeserializationError::TooDeep,
  462. "null",
  463. 0,
  464. },
  465. {
  466. "Check nesting limit even for ignored objects",
  467. "{'hello':{}}",
  468. "false",
  469. 1,
  470. DeserializationError::TooDeep,
  471. "null",
  472. 0,
  473. },
  474. {
  475. "Check nesting limit even for ignored values in objects",
  476. "{'hello':{}}",
  477. "{}",
  478. 1,
  479. DeserializationError::TooDeep,
  480. "{}",
  481. sizeofObject(0),
  482. },
  483. {
  484. "Check nesting limit even for ignored arrays",
  485. "[]",
  486. "false",
  487. 0,
  488. DeserializationError::TooDeep,
  489. "null",
  490. 0,
  491. },
  492. {
  493. "Check nesting limit even for ignored arrays",
  494. "[[]]",
  495. "false",
  496. 1,
  497. DeserializationError::TooDeep,
  498. "null",
  499. 0,
  500. },
  501. {
  502. "Check nesting limit even for ignored values in arrays",
  503. "[[]]",
  504. "[]",
  505. 1,
  506. DeserializationError::TooDeep,
  507. "[]",
  508. sizeofArray(0),
  509. },
  510. {
  511. "Supports back-slash at the end of skipped string",
  512. "\"hell\\",
  513. "false",
  514. 1,
  515. DeserializationError::IncompleteInput,
  516. "null",
  517. 0,
  518. },
  519. {
  520. "Invalid comment at after an element in a skipped array",
  521. "[1/]",
  522. "false",
  523. 10,
  524. DeserializationError::InvalidInput,
  525. "null",
  526. 0,
  527. },
  528. {
  529. "Incomplete comment at after an element in a skipped array",
  530. "[1/*]",
  531. "false",
  532. 10,
  533. DeserializationError::IncompleteInput,
  534. "null",
  535. 0,
  536. },
  537. {
  538. "Missing comma in a skipped array",
  539. "[1 2]",
  540. "false",
  541. 10,
  542. DeserializationError::InvalidInput,
  543. "null",
  544. 0,
  545. },
  546. {
  547. "Invalid comment at the beginning of array",
  548. "[/1]",
  549. "[false]",
  550. 10,
  551. DeserializationError::InvalidInput,
  552. "[]",
  553. sizeofArray(0),
  554. },
  555. {
  556. "Incomplete comment at the begining of an array",
  557. "[/*]",
  558. "[false]",
  559. 10,
  560. DeserializationError::IncompleteInput,
  561. "[]",
  562. sizeofArray(0),
  563. },
  564. {
  565. "Invalid comment before key",
  566. "{/1:2}",
  567. "{}",
  568. 10,
  569. DeserializationError::InvalidInput,
  570. "{}",
  571. sizeofObject(0),
  572. },
  573. {
  574. "Incomplete comment before key",
  575. "{/*:2}",
  576. "{}",
  577. 10,
  578. DeserializationError::IncompleteInput,
  579. "{}",
  580. sizeofObject(0),
  581. },
  582. {
  583. "Invalid comment after key",
  584. "{\"example\"/1:2}",
  585. "{}",
  586. 10,
  587. DeserializationError::InvalidInput,
  588. "{}",
  589. sizeofObject(0),
  590. },
  591. {
  592. "Incomplete comment after key",
  593. "{\"example\"/*:2}",
  594. "{}",
  595. 10,
  596. DeserializationError::IncompleteInput,
  597. "{}",
  598. sizeofObject(0),
  599. },
  600. {
  601. "Invalid comment after colon",
  602. "{\"example\":/12}",
  603. "{}",
  604. 10,
  605. DeserializationError::InvalidInput,
  606. "{}",
  607. sizeofObject(0),
  608. },
  609. {
  610. "Incomplete comment after colon",
  611. "{\"example\":/*2}",
  612. "{}",
  613. 10,
  614. DeserializationError::IncompleteInput,
  615. "{}",
  616. sizeofObject(0),
  617. },
  618. {
  619. "Comment next to an integer",
  620. "{\"ignore\":1//,\"example\":2\n}",
  621. "{\"example\":true}",
  622. 10,
  623. DeserializationError::Ok,
  624. "{}",
  625. sizeofObject(0),
  626. },
  627. {
  628. "Invalid comment after opening brace of a skipped object",
  629. "{/1:2}",
  630. "false",
  631. 10,
  632. DeserializationError::InvalidInput,
  633. "null",
  634. 0,
  635. },
  636. {
  637. "Incomplete after opening brace of a skipped object",
  638. "{/*:2}",
  639. "false",
  640. 10,
  641. DeserializationError::IncompleteInput,
  642. "null",
  643. 0,
  644. },
  645. {
  646. "Invalid comment after key of a skipped object",
  647. "{\"example\"/:2}",
  648. "false",
  649. 10,
  650. DeserializationError::InvalidInput,
  651. "null",
  652. 0,
  653. },
  654. {
  655. "Incomplete comment after key of a skipped object",
  656. "{\"example\"/*:2}",
  657. "false",
  658. 10,
  659. DeserializationError::IncompleteInput,
  660. "null",
  661. 0,
  662. },
  663. {
  664. "Invalid comment after value in a skipped object",
  665. "{\"example\":2/}",
  666. "false",
  667. 10,
  668. DeserializationError::InvalidInput,
  669. "null",
  670. 0,
  671. },
  672. {
  673. "Incomplete comment after value of a skipped object",
  674. "{\"example\":2/*}",
  675. "false",
  676. 10,
  677. DeserializationError::IncompleteInput,
  678. "null",
  679. 0,
  680. },
  681. {
  682. "Incomplete comment after comma in skipped object",
  683. "{\"example\":2,/*}",
  684. "false",
  685. 10,
  686. DeserializationError::IncompleteInput,
  687. "null",
  688. 0,
  689. },
  690. {
  691. "NUL character in key",
  692. "{\"x\":0,\"x\\u0000a\":1,\"x\\u0000b\":2}",
  693. "{\"x\\u0000a\":true}",
  694. 10,
  695. DeserializationError::Ok,
  696. "{\"x\\u0000a\":1}",
  697. sizeofObject(1) + sizeofString("x?a"),
  698. },
  699. };
  700. for (auto& tc : testCases) {
  701. SECTION(tc.description) {
  702. SpyingAllocator spy;
  703. JsonDocument filter;
  704. JsonDocument doc(&spy);
  705. REQUIRE(deserializeJson(filter, tc.filter) == DeserializationError::Ok);
  706. CHECK(deserializeJson(
  707. doc, tc.input, DeserializationOption::Filter(filter),
  708. DeserializationOption::NestingLimit(tc.nestingLimit)) ==
  709. tc.error);
  710. CHECK(doc.as<std::string>() == tc.output);
  711. doc.shrinkToFit();
  712. CHECK(spy.allocatedBytes() == tc.memoryUsage);
  713. }
  714. }
  715. }
  716. TEST_CASE("Overloads") {
  717. JsonDocument doc;
  718. JsonDocument filter;
  719. using namespace DeserializationOption;
  720. // deserializeJson(..., Filter)
  721. SECTION("const char*, Filter") {
  722. deserializeJson(doc, "{}", Filter(filter));
  723. }
  724. SECTION("const char*, size_t, Filter") {
  725. deserializeJson(doc, "{}", 2, Filter(filter));
  726. }
  727. SECTION("const std::string&, Filter") {
  728. deserializeJson(doc, "{}"_s, Filter(filter));
  729. }
  730. SECTION("std::istream&, Filter") {
  731. std::stringstream s("{}");
  732. deserializeJson(doc, s, Filter(filter));
  733. }
  734. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  735. SECTION("char[n], Filter") {
  736. size_t i = 4;
  737. char vla[i];
  738. strcpy(vla, "{}");
  739. deserializeJson(doc, vla, Filter(filter));
  740. }
  741. #endif
  742. // deserializeJson(..., Filter, NestingLimit)
  743. SECTION("const char*, Filter, NestingLimit") {
  744. deserializeJson(doc, "{}", Filter(filter), NestingLimit(5));
  745. }
  746. SECTION("const char*, size_t, Filter, NestingLimit") {
  747. deserializeJson(doc, "{}", 2, Filter(filter), NestingLimit(5));
  748. }
  749. SECTION("const std::string&, Filter, NestingLimit") {
  750. deserializeJson(doc, "{}"_s, Filter(filter), NestingLimit(5));
  751. }
  752. SECTION("std::istream&, Filter, NestingLimit") {
  753. std::stringstream s("{}");
  754. deserializeJson(doc, s, Filter(filter), NestingLimit(5));
  755. }
  756. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  757. SECTION("char[n], Filter, NestingLimit") {
  758. size_t i = 4;
  759. char vla[i];
  760. strcpy(vla, "{}");
  761. deserializeJson(doc, vla, Filter(filter), NestingLimit(5));
  762. }
  763. #endif
  764. // deserializeJson(..., NestingLimit, Filter)
  765. SECTION("const char*, NestingLimit, Filter") {
  766. deserializeJson(doc, "{}", NestingLimit(5), Filter(filter));
  767. }
  768. SECTION("const char*, size_t, NestingLimit, Filter") {
  769. deserializeJson(doc, "{}", 2, NestingLimit(5), Filter(filter));
  770. }
  771. SECTION("const std::string&, NestingLimit, Filter") {
  772. deserializeJson(doc, "{}"_s, NestingLimit(5), Filter(filter));
  773. }
  774. SECTION("std::istream&, NestingLimit, Filter") {
  775. std::stringstream s("{}");
  776. deserializeJson(doc, s, NestingLimit(5), Filter(filter));
  777. }
  778. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  779. SECTION("char[n], NestingLimit, Filter") {
  780. size_t i = 4;
  781. char vla[i];
  782. strcpy(vla, "{}");
  783. deserializeJson(doc, vla, NestingLimit(5), Filter(filter));
  784. }
  785. #endif
  786. }
  787. TEST_CASE("shrink filter") {
  788. JsonDocument doc;
  789. SpyingAllocator spy;
  790. JsonDocument filter(&spy);
  791. filter["a"] = true;
  792. spy.clearLog();
  793. deserializeJson(doc, "{}", DeserializationOption::Filter(filter));
  794. REQUIRE(spy.log() == AllocatorLog{
  795. Reallocate(sizeofPool(), sizeofObject(1)),
  796. });
  797. }