Skip to content

C++ operator sugar - a C++ way of looping through array #14

Description

@cztomczak

Hi,

The C++ wrapper is great, but I'm missing one thing: a way to loop through array in a c++ fool-proof way. I know that I can do this:

json_value arr = (*settings)["web_server"]["cgi_extensions"];
if (arr.type == json_array) {
    int length = arr.u.array.length;
    for (int i = 0; i < length; i++) {
    ...
    }
}

But, a newbie might forget the type checking and just go with:

json_value arr = (*settings)["web_server"]["cgi_extensions"];
int length = arr.u.array.length;
for (int i = 0; i < length; i++) {
...
}

If someone makes a mistake in the json file, so that array is missing or becomes a string, then this code will crash the application.

I love the C++ wrapper mostly that it is fool-proof, I can reference to a non-existing key and it just returns an empty string, I will mistake the types and it will just return 0, nothing bad happens. But when it comes to looping through arrays it's easy to make a mistake, I've done it already and I'm a little scared of writing such code.

What I'm looking for is some method like GetArrayLength() that will return array length or 0 when value is not an array.

Czarek.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions