Skip to content

Add mode for returning last item for duplicate keys #932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -1906,36 +1906,58 @@ CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index)
return get_array_item(array, (size_t)index);
}

static int cJSON_DuplicateKeyMode = 0;

CJSON_PUBLIC(void) cJSON_SetDuplicateKeyMode(int mode)
{
cJSON_DuplicateKeyMode = mode;
}

static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive)
{
cJSON *current_element = NULL;
cJSON *last_match = NULL;

if ((object == NULL) || (name == NULL))
{
return NULL;
}

current_element = object->child;
if (case_sensitive)
while (current_element != NULL)
{
while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0))
if (case_sensitive)
{
current_element = current_element->next;
if ((current_element->string != NULL) && (strcmp(name, current_element->string) == 0))
{
if (cJSON_DuplicateKeyMode == 0)
{
return current_element;
}
else
{
last_match = current_element;
}
}
}
}
else
{
while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0))
else
{
current_element = current_element->next;
if ((current_element->string != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) == 0))
{
if (cJSON_DuplicateKeyMode == 0)
{
return current_element;
}
else
{
last_match = current_element;
}
}
}
current_element = current_element->next;
}

if ((current_element == NULL) || (current_element->string == NULL)) {
return NULL;
}

return current_element;
return last_match;
}

CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string)
Expand Down
33 changes: 32 additions & 1 deletion tests/misc_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,36 @@ static void cjson_set_bool_value_must_not_break_objects(void)
cJSON_Delete(sobj);
}

static void cjson_duplicate_keys_should_return_last_when_mode_is_set(void)
{
cJSON *obj;
cJSON *item;
cJSON_SetDuplicateKeyMode(1);

obj = cJSON_Parse("{\"key\":\"first\",\"key\":\"second\"}");
TEST_ASSERT_NOT_NULL_MESSAGE(obj, "Failed to parse JSON with duplicate keys.");

item = cJSON_GetObjectItem(obj, "key");
TEST_ASSERT_NOT_NULL_MESSAGE(item, "Failed to get object item for duplicate key.");
TEST_ASSERT_EQUAL_STRING_MESSAGE("second", item->valuestring, "Duplicate key mode did not return last item.");
cJSON_Delete(obj);
}

static void cjson_duplicate_keys_case_insensitive_should_return_last(void)
{
cJSON *obj;
cJSON *item;
cJSON_SetDuplicateKeyMode(1);

obj = cJSON_Parse("{\"Key\":\"first\",\"key\":\"second\"}");
TEST_ASSERT_NOT_NULL_MESSAGE(obj, "Failed to parse JSON with duplicate keys (case insensitive).");

item = cJSON_GetObjectItem(obj, "key");
TEST_ASSERT_NOT_NULL_MESSAGE(item, "Failed to get object item for duplicate key (case insensitive).");
TEST_ASSERT_EQUAL_STRING_MESSAGE("second", item->valuestring, "Duplicate key mode (case insensitive) did not return last item.");
cJSON_Delete(obj);
}

int CJSON_CDECL main(void)
{
UNITY_BEGIN();
Expand Down Expand Up @@ -815,6 +845,7 @@ int CJSON_CDECL main(void)
RUN_TEST(cjson_delete_item_from_array_should_not_broken_list_structure);
RUN_TEST(cjson_set_valuestring_to_object_should_not_leak_memory);
RUN_TEST(cjson_set_bool_value_must_not_break_objects);

RUN_TEST(cjson_duplicate_keys_should_return_last_when_mode_is_set);
RUN_TEST(cjson_duplicate_keys_case_insensitive_should_return_last);
return UNITY_END();
}