On Tue, 2026-02-03 at 01:49 +0800, Yangyu Chen wrote:
> Currently, the object class in json.h does not provide a way to
> iterate
> over its keys.
FWIW there are these member functions:
size_t get_num_keys () const { return m_keys.length (); }
const char *get_key (size_t i) const { return m_keys[i]; }
and a begin/end pair could be added to support C++-style "for" loops,
but I don't object to the patch as is.
Dave
> This commit adds an `iterate` method to the object class,
> allowing users to iterate over the keys of a JSON object.
>
> Signed-off-by: Yangyu Chen <[email protected]>
>
> gcc/ChangeLog:
>
> * json.h (class object): Add iterate method to object class.
> ---
> gcc/json.h | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/gcc/json.h b/gcc/json.h
> index fabe2fe0886a..0f01ee19a109 100644
> --- a/gcc/json.h
> +++ b/gcc/json.h
> @@ -239,6 +239,11 @@ class object : public value
> void set_enum (const enum_property<EnumType> &property,
> EnumType value);
>
> + bool iterate (unsigned i, const char **key) const
> + {
> + return m_keys.iterate (i, key);
> + }
> +
> static int compare (const json::object &obj_a, const json::object
> &obj_b);
>
> size_t get_num_keys () const { return m_keys.length (); }