JR_ARR_KV

Table of Contents

JR_ARR_KV

Description

Conditionally parse a JSON array if key matches expected name.

Parameters

NameDirectionDescription
siin,outStream iterator to read from.
kinExpected key name (C-string).

Usage example (from documentation)

  JR_ARR_KV(si, "list", {
      JR_INT(si, val);
      VecPush(&arr, val);
  });

Success

Array parsed if key matched

Failure

No-op if key does not match

Usage example (Cross-references)

    }
    
    JR_ARR_KV(json, "entries", {
    EnumEntry e = {0};
    JR_OBJ_KV(json, "project", {                                                                               \
    JR_STR_KV(json, "build_dir", p.build_dir);                                                             \
    JR_ARR_KV(json, "test_directories", {                                                                  \
    Str s = StrInit();                                                                                 \
    JR_STR(json, s);                                                                                   \
    VecPushBack(&p.test_directories, s);                                                               \
    });                                                                                                    \
    JR_ARR_KV(json, "source_directories", {                                                                \
    Str s = StrInit();                                                                                 \
    JR_STR(json, s);                                                                                   \
    
    JR_OBJ(si, {
    JR_ARR_KV(si, "languages", {
    Str lang = StrInit();
    JR_STR(si, lang);
    JR_STR_KV(si, "name", product.name);
    JR_FLT_KV(si, "price", product.price);
    JR_ARR_KV(si, "tags", {
    Str tag = StrInit();
    JR_STR(si, tag);
    
    JR_OBJ(si1, {
    JR_ARR_KV(si1, "items", {
    i32 value = 0;
    JR_INT(si1, value);
    
    JR_OBJ(si2, {
    JR_ARR_KV(si2, "data", {
    Str value = StrInit();
    JR_STR(si2, value);
    JR_OBJ(si, {
    JR_OBJ_KV(si, "outer", { obj.found_outer = true; });
    JR_ARR_KV(si, "list", {
    obj.found_list = true; // Should not execute for empty array
    });
    );
    JR_OBJ_KV(si, "filled_obj", { JR_INT_KV(si, "x", obj.x_value); });
    JR_ARR_KV(
    si,
    "empty_arr",
    }
    );
    JR_ARR_KV(si, "filled_arr", {
    i32 item = 0;
    JR_INT(si, item);
    StrIter si = StrIterFromStr(json);
    JR_OBJ(si, {
    JR_ARR_KV(si, "numbers", {
    i32 num = 0;
    JR_INT(si, num);
    VecPushBack(&parsed_numbers, num);
    });
    JR_ARR_KV(si, "strings", {
    Str str = StrInit();
    JR_STR(si, str);
    JR_INT_KV(si, "timeout", parsed.config.timeout);
    JR_STR_KV(si, "log_level", parsed.config.log_level);
    JR_ARR_KV(si, "features", {
    Str feature = StrInit();
    JR_STR(si, feature);
    });
    });
    JR_ARR_KV(si, "numbers", {
    i32 num = 0;
    JR_INT(si, num);
    VecPushBack(&parsed.numbers, num);
    });
    JR_ARR_KV(si, "flags", {
    bool flag = false;
    JR_BOOL(si, flag);
    JR_OBJ(si, {
    JR_STR_KV(si, "empty_string", parsed_str);
    JR_ARR_KV(si, "empty_numbers", {
    i32 num = 0;
    JR_INT(si, num);
    VecPushBack(&parsed_numbers, num);
    });
    JR_ARR_KV(si, "empty_strings", {
    Str str = StrInit();
    JR_STR(si, str);

Share :

Related Posts

JR_OBJ

JR_OBJ Description Read a JSON object using a custom field reader expression. The macro parses the object and invokes the provided code block for each key-value pair. If the key is not recognized or parsing fails, the value is skipped.

Read More

JR_FLT_KV

JR_FLT_KV Description Read a float key-value pair if key matches.

Read More

JR_INT_KV

JR_INT_KV Description Read an integer key-value pair if key matches.

Read More