Skip to content
Merged
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
7 changes: 7 additions & 0 deletions libutils/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,8 @@ const char *JsonParseErrorToString(const JsonParseError error)
"Unable to parse json data as object, ':' seen without having specified an l-value",
[JSON_PARSE_ERROR_OBJECT_COMMA] =
"Unable to parse json data as object, ',' seen without having specified an r-value",
[JSON_PARSE_ERROR_OBJECT_COMMA_MISSING] =
"Unable to parse json data as object, new key started without preceding ','",
[JSON_PARSE_ERROR_OBJECT_ARRAY_LVAL] =
"Unable to parse json data as object, array not allowed as l-value",
[JSON_PARSE_ERROR_OBJECT_OBJECT_LVAL] =
Expand Down Expand Up @@ -2670,6 +2672,11 @@ static JsonParseError JsonParseAsObject(
}
else
{
if (prev_char != '{' && prev_char != ',')
{
JsonDestroy(object);
return JSON_PARSE_ERROR_OBJECT_COMMA_MISSING;
}
property_name = NULL;
JsonParseError err = JsonParseAsString(data, &property_name);
if (err != JSON_PARSE_OK)
Expand Down
1 change: 1 addition & 0 deletions libutils/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ typedef enum
JSON_PARSE_ERROR_OBJECT_END,
JSON_PARSE_ERROR_OBJECT_COLON,
JSON_PARSE_ERROR_OBJECT_COMMA,
JSON_PARSE_ERROR_OBJECT_COMMA_MISSING,
JSON_PARSE_ERROR_OBJECT_ARRAY_LVAL,
JSON_PARSE_ERROR_OBJECT_OBJECT_LVAL,
JSON_PARSE_ERROR_OBJECT_OPEN_LVAL,
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/json_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,44 @@ static void test_json_get_type_as_string()
}
}

static void test_json_parse_object_missing_comma(void)
{
{
const char *data =
"{ \"first\": \"one\" \"second\": \"two\" }";
JsonElement *json = NULL;
assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json));
assert_false(json);
}

{
const char *data =
"{\"first\":\"one\"\"second\":\"two\"}";
JsonElement *json = NULL;
assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json));
assert_false(json);
}

{
const char *data =
"{ \"first\": \"one\", \"second\": \"two\" \"third\": \"three\" }";
JsonElement *json = NULL;
assert_int_not_equal(JSON_PARSE_OK, JsonParse(&data, &json));
assert_false(json);
}

{
const char *data =
"{ \"first\": \"one\", \"second\": \"two\" }";
JsonElement *json = NULL;
assert_int_equal(JSON_PARSE_OK, JsonParse(&data, &json));
assert_true(json);
assert_string_equal(JsonObjectGetAsString(json, "first"), "one");
assert_string_equal(JsonObjectGetAsString(json, "second"), "two");
JsonDestroy(json);
}
}

int main()
{
PRINT_TEST_BANNER();
Expand Down Expand Up @@ -2521,6 +2559,7 @@ int main()
unit_test(test_json_object_merge_deep),
unit_test(test_compare_container_type_mismatch),
unit_test(test_json_get_type_as_string),
unit_test(test_json_parse_object_missing_comma),
};

return run_tests(tests);
Expand Down
Loading