From 4c7cfd5cbc64c34b4532aad0d87e4c81e2277b02 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 14 May 2022 10:11:52 -0400 Subject: JSON reactor: improve handling of nested containers Call the parent container's item method before calling the child item's start method so we can easily know the current nesting level when nested items are added. --- include/qpdf/JSON.hh | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/qpdf/JSON.hh b/include/qpdf/JSON.hh index 79f2a25f..e711a2df 100644 --- a/include/qpdf/JSON.hh +++ b/include/qpdf/JSON.hh @@ -220,7 +220,10 @@ class JSON // The start/end methods are called when parsing of a // dictionary or array is started or ended. The item methods // are called when an item is added to a dictionary or array. - // See important notes in "Item methods" below. + // When adding a container to another container, the item + // method is called with an empty container before the lower + // container's start method is called. See important notes in + // "Item methods" below. // During parsing of a JSON string, the parser is operating on // a single object at a time. When a dictionary or array is @@ -230,10 +233,10 @@ class JSON // following method calls // // dictionaryStart -- current object is the top-level dictionary + // dictionaryItem -- called with "a" and an empty array // arrayStart -- current object is the array // arrayItem -- called with the "1" object // containerEnd -- now current object is the dictionary again - // dictionaryItem -- called with "a" and the just-completed array // containerEnd -- current object is undefined // // If the top-level item in a JSON string is a scalar, the @@ -261,8 +264,12 @@ class JSON // NOTE: When a dictionary or an array is added to a // container, the dictionaryItem or arrayItem method is called // when the child item's start delimiter is encountered, so - // the JSON object passed in at that time will always be - // in its initial, empty state. + // the JSON object passed in at that time will always be in + // its initial, empty state. Additionally, the child item's + // start method is not called until after the parent item's + // item method is called. This makes it possible to keep track + // of the current depth level by incrementing level on start + // methods and decrementing on end methods. QPDF_DLL virtual bool -- cgit v1.2.3-54-g00ecf