diff --git a/include/simdjson/generic/ondemand/value_iterator-inl.h b/include/simdjson/generic/ondemand/value_iterator-inl.h index c3b182c3..1bbc1d47 100644 --- a/include/simdjson/generic/ondemand/value_iterator-inl.h +++ b/include/simdjson/generic/ondemand/value_iterator-inl.h @@ -68,7 +68,13 @@ simdjson_warn_unused simdjson_really_inline simdjson_result value_iterator // ^ (depth 0, index 2) // ``` // - if (!is_open()) { return false; } + if (!is_open()) { + // If we're past the end of the object, we're being iterated out of order. + // Note: this isn't perfect detection. It's possible the user is inside some other object; if so, + // this object iterator will blithely scan that object for fields. + if (_json_iter->depth() < depth() - 1) { return OUT_OF_ORDER_ITERATION; } + return false; + } if (at_first_field()) { has_value = true;