diff options
author | Jay Berkenbilt <ejb@ql.org> | 2022-05-14 16:11:52 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2022-05-14 23:35:06 +0200 |
commit | 4c7cfd5cbc64c34b4532aad0d87e4c81e2277b02 (patch) | |
tree | 18619166a65f0f62f33cbb362bda6264d712db08 /libqpdf/JSON.cc | |
parent | 2a2f7f1bba3dd87bd17f8b819ddeb1a24bb742dd (diff) | |
download | qpdf-4c7cfd5cbc64c34b4532aad0d87e4c81e2277b02.tar.zst |
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.
Diffstat (limited to 'libqpdf/JSON.cc')
-rw-r--r-- | libqpdf/JSON.cc | 18 |
1 files changed, 12 insertions, 6 deletions
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>(JSON::makeDictionary()); item->setStart(offset - token.length()); - if (reactor) { - reactor->dictionaryStart(); - } break; case '[': item = std::make_shared<JSON>(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()) { |