JR_OBJ_KV

Table of Contents

JR_OBJ_KV

Description

Conditionally parse a JSON object if key matches expected name.

Parameters

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

Usage example (from documentation)

  JR_OBJ_KV(si, "config", {
      JR_BOOL_KV(si, "debug", flags.debug_mode);
  });

Success

Object parsed if key matched

Failure

No-op if key does not match

Usage example (Cross-references)

    JR_BOOL_KV(json, "to_from_str", to_from_str);
    
    JR_OBJ_KV(json, "invalid_enum", {
    JR_STR_KV(json, "name", invalid_enum.name);
    JR_INT_KV(json, "value", invalid_enum.value);
    p.source_directories = VecInitT(p.source_directories);                                                         \
    JR_OBJ(json, {                                                                                                 \
    JR_OBJ_KV(json, "project", {                                                                               \
    JR_STR_KV(json, "build_dir", p.build_dir);                                                             \
    JR_ARR_KV(json, "test_directories", {                                                                  \
    
    JR_OBJ(si, {
    JR_OBJ_KV(si, "user", {
    JR_STR_KV(si, "name", data.user.name);
    JR_STR_KV(si, "email", data.user.email);
    
    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
    obj.found_list = true; // Should not execute for empty array
    });
    JR_OBJ_KV(si, "deep", {
    obj.found_deep = true;
    JR_OBJ_KV(si, "inner", { obj.found_inner = true; });
    JR_OBJ_KV(si, "deep", {
    obj.found_deep = true;
    JR_OBJ_KV(si, "inner", { obj.found_inner = true; });
    });
    });
    
    JR_OBJ(si, {
    JR_OBJ_KV(
    si,
    "empty_obj",
    }
    );
    JR_OBJ_KV(si, "filled_obj", { JR_INT_KV(si, "x", obj.x_value); });
    JR_ARR_KV(
    si,
    StrIter si = StrIterFromStr(json);
    JR_OBJ(si, {
    JR_OBJ_KV(si, "user", {
    JR_INT_KV(si, "id", parsed_person.id);
    JR_STR_KV(si, "name", parsed_person.name);
    StrIter si = StrIterFromStr(json);
    JR_OBJ(si, {
    JR_OBJ_KV(si, "user", {
    JR_INT_KV(si, "id", parsed.user.id);
    JR_STR_KV(si, "name", parsed.user.name);
    JR_FLT_KV(si, "salary", parsed.user.salary);
    });
    JR_OBJ_KV(si, "config", {
    JR_BOOL_KV(si, "debug_mode", parsed.config.debug_mode);
    JR_INT_KV(si, "timeout", parsed.config.timeout);
    VecPushBack(&parsed_strings, str);
    });
    JR_OBJ_KV(si, "empty_object", { found_empty_object = true; });
    });
    
    JR_OBJ(si, {
    JR_OBJ_KV(si, "user", {
    JR_INT_KV(si, "id", data.user.id);
    JR_OBJ_KV(si, "profile", {
    JR_OBJ_KV(si, "user", {
    JR_INT_KV(si, "id", data.user.id);
    JR_OBJ_KV(si, "profile", {
    JR_STR_KV(si, "name", data.user.profile.name);
    JR_INT_KV(si, "age", data.user.profile.age);
    
    JR_OBJ(si, {
    JR_OBJ_KV(si, "company", {
    JR_OBJ_KV(si, "departments", {
    JR_OBJ_KV(si, "engineering", {
    JR_OBJ(si, {
    JR_OBJ_KV(si, "company", {
    JR_OBJ_KV(si, "departments", {
    JR_OBJ_KV(si, "engineering", {
    JR_STR_KV(si, "head", data.company.departments.engineering.head);
    JR_OBJ_KV(si, "company", {
    JR_OBJ_KV(si, "departments", {
    JR_OBJ_KV(si, "engineering", {
    JR_STR_KV(si, "head", data.company.departments.engineering.head);
    JR_INT_KV(si, "count", data.company.departments.engineering.count);
    
    JR_OBJ(si, {
    JR_OBJ_KV(si, "functions", {
    // First level: source function ID from key
    u64 source_function_id = strtoull(key.data, NULL, 10);
    
    if (response.status) {
    JR_OBJ_KV(si, "data", {
    u64 source_function_id = strtoull(key.data, NULL, 10);
    JR_OBJ(si, {
    if (response.status) {
    printf("[DEBUG] Status is true, parsing data...\n");
    JR_OBJ_KV(si, "data", {
    u64 source_function_id = strtoull(key.data, NULL, 10);
    printf("[DEBUG] Source function ID from key: %llu\n", source_function_id);
    if (response.status) {
    printf("[DEBUG] Status is true, parsing data...\n");
    JR_OBJ_KV(si, "data", {
    u64 source_function_id = strtoull(key.data, NULL, 10);
    printf("[DEBUG] Source function ID from key: %llu\n", source_function_id);

Share :

Related Posts

JR_FLT_KV

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

Read More

JR_INT

JR_INT Description Read a JSON integer value from stream and assign to target.

Read More

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