Skip to content

Commit 24889ce

Browse files
author
Luke Robison
committed
opal/util/json: Add opal_json_get_key_by_index function
Retrieves values from a json object according to their index. Signed-off-by: Luke Robison <[email protected]>
1 parent dd7c935 commit 24889ce

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

opal/util/json/opal_json.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Amazon.com, Inc. or its affiliates.
2+
* Copyright (c) 2024-2025 Amazon.com, Inc. or its affiliates.
33
* All Rights reserved.
44
* $COPYRIGHT$
55
*
@@ -186,6 +186,33 @@ int opal_json_get_key(const opal_json_t *json, const char *key, const opal_json_
186186
return ret;
187187
}
188188

189+
int opal_json_get_key_by_index(const opal_json_t *json, const size_t index, const char **key, const opal_json_t **out)
190+
{
191+
int ret = OPAL_ERROR;
192+
193+
CHECK_OBJ_TYPE(json, OPAL_JSON_OBJECT);
194+
195+
opal_json_internal_t *in = (opal_json_internal_t *) json;
196+
opal_json_internal_t *result = NULL;
197+
198+
json_object_entry entry = {0};
199+
200+
if (index < 0 || index >= in->value->u.object.length) {
201+
opal_show_help("help-json.txt", "Index out of bound", true, index,
202+
in->value->u.array.length);
203+
return ret;
204+
}
205+
entry = in->value->u.object.values[index];
206+
*key = entry.name;
207+
ret = opal_json_internal_new(entry.value, &result);
208+
if (OPAL_SUCCESS == ret) {
209+
*out = (opal_json_t *) result;
210+
} else if (result) {
211+
opal_json_internal_free(result);
212+
}
213+
return ret;
214+
}
215+
189216
void opal_json_free(const opal_json_t **json)
190217
{
191218
opal_json_internal_free((struct opal_json_internal_t *) *json);

opal/util/json/opal_json.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Amazon.com, Inc. or its affiliates.
2+
* Copyright (c) 2024-2025 Amazon.com, Inc. or its affiliates.
33
* All Rights reserved.
44
* $COPYRIGHT$
55
*
@@ -123,6 +123,22 @@ OPAL_DECLSPEC int opal_json_get_key(const opal_json_t *json, const char *key,
123123
OPAL_DECLSPEC int opal_json_get_index(const opal_json_t *json, const size_t index,
124124
const opal_json_t **out);
125125

126+
/**
127+
* Get the JSON object and key at index from a JSON object
128+
*
129+
* @param[in] json Parent JSON array
130+
* @param[in] index Index value
131+
* @param[out] key The name of the key at the given index. This pointer
132+
* remains valid for the lifetime of the parent JSON
133+
* object. The caller should not free it.
134+
* @param[out] out Output JSON object at the specified index. This object
135+
* should be freed by caller with opal_json_free.
136+
*/
137+
OPAL_DECLSPEC int
138+
opal_json_get_key_by_index( const opal_json_t *json, const size_t index,
139+
const char **key, const opal_json_t **out);
140+
141+
126142
/**
127143
* Get the number of objects in a container-type value, i.e. object, array.
128144
*

test/util/opal_json.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Amazon.com, Inc. or its affiliates.
2+
* Copyright (c) 2024-2025 Amazon.com, Inc. or its affiliates.
33
* All Rights reserved.
44
* $COPYRIGHT$
55
*
@@ -129,7 +129,9 @@ static void test_valid_json(void)
129129
int ret = 0;
130130
size_t size;
131131
const opal_json_t *json = NULL, *a = NULL, *b = NULL, *b0 = NULL, *b1 = NULL, *c = NULL,
132-
*d = NULL;
132+
*d = NULL, *dummy = NULL;
133+
const char *found_key;
134+
133135
/**
134136
* Human readable form:
135137
* {
@@ -208,6 +210,21 @@ static void test_valid_json(void)
208210
test_json_double_val(d, 3.456);
209211
}
210212

213+
ret = opal_json_get_key_by_index(json, 3, &found_key, &d);
214+
if (ret) {
215+
test_failure("opal_json_get_key_by_index failed when called with a valid index");
216+
} else {
217+
if ( 0 != strcmp(found_key, "d") ) {
218+
test_failure("opal_json_get_key_by_index returned the wrong key value");
219+
}
220+
test_json_double_val(d, 3.456);
221+
}
222+
223+
ret = opal_json_get_key_by_index(json, 4, &found_key, &dummy);
224+
if (ret == OPAL_SUCCESS) {
225+
test_failure("opal_json_get_key_by_index returned OPAL_SUCCESS when passed invalid index.");
226+
}
227+
211228
/* JSON objects can be released in any order */
212229
FREE_JSON(a);
213230
FREE_JSON(b);

0 commit comments

Comments
 (0)