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. --- libqpdf/JSON.cc | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'libqpdf') diff --git a/libqpdf/JSON.cc b/libqpdf/JSON.cc index 1c49f9ee..a2aff78b 100644 --- a/libqpdf/JSON.cc +++ b/libqpdf/JSON.cc @@ -949,17 +949,11 @@ JSONParser::handleToken() case '{': item = std::make_shared(JSON::makeDictionary()); item->setStart(offset - token.length()); - if (reactor) { - reactor->dictionaryStart(); - } break; case '[': item = std::make_shared(JSON::makeArray()); item->setStart(offset - token.length()); - if (reactor) { - reactor->arrayStart(); - } break; default: @@ -1187,6 +1181,18 @@ JSONParser::handleToken() "JSONParser::handleToken: unexpected null item in transition"); } + if (reactor && item.get()) { + // Calling container start method is postponed until after + // adding the containers to their parent containers, if any. + // This makes it much easier to keep track of the current + // nesting level. + if (item->isDictionary()) { + reactor->dictionaryStart(); + } else if (item->isArray()) { + reactor->arrayStart(); + } + } + // Prepare for next token if (item.get()) { if (item->isDictionary()) { -- cgit v1.2.3-54-g00ecf