Skip to content

Commit

Permalink
Merge pull request #590 from nats-io/json_empty_arrays
Browse files Browse the repository at this point in the history
[FIXED] Handling of JSON empty arrays
  • Loading branch information
kozlovic committed Sep 21, 2022
2 parents eeafb9f + 342b337 commit 0db15b9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,13 @@ _jsonGetArray(char **ptr, nats_JSONArray **newArray, int nested)
{
p = _jsonTrimSpace(p);

if ((typ == TYPE_NOT_SET) && (*p == ']'))
{
array.typ = TYPE_NULL;
end = true;
break;
}

// Initialize the field before parsing.
memset(&field, 0, sizeof(nats_JSONField));

Expand Down Expand Up @@ -1454,6 +1461,12 @@ nats_JSONGetArrayField(nats_JSON *json, const char *fieldName, int fieldType, na
return nats_setError(NATS_INVALID_ARG,
"Field '%s' is not an array, it has type: %d",
field->name, field->typ);
// If empty array, return NULL/OK
if (field->value.varr->typ == TYPE_NULL)
{
*retField = NULL;
return NATS_OK;
}
if (fieldType != field->value.varr->typ)
return nats_setError(NATS_INVALID_ARG,
"Asked for field '%s' as an array of type: %d, but it is an array of type: %d",
Expand Down
42 changes: 42 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3198,6 +3198,7 @@ test_natsJSON(void)
"{ \"test\": [\"abc,def\"]}",
"{ \"test\": [{\"a\": 1}, {\"b\": \"c\"}]}",
"{ \"test\": [[{\"a\": 1}], [{\"b\": \"c\"}]]}",
"{ \"test\": []}",
"{ \"test\": {\"inner\":\"a\",\"inner2\":2,\"inner3\":false,\"inner4\":{\"inner_inner1\" : 1.234}}}",
"{ \"test\": \"a\\\"b\\\"c\"}",
"{ \"test\": \"\\\"\\\\/\b\f\n\r\t\\uabcd\"}",
Expand Down Expand Up @@ -4113,6 +4114,47 @@ test_natsJSON(void)
&& (strstr(nats_GetLastError(NULL), "on purpose")));
nats_clearLastError();
nats_JSONDestroy(json);
json = NULL;

test("Parse empty array: ");
s = nats_JSONParse(&json, "{\"empty\":[]}", -1);
testCond(s == NATS_OK);

test("Get empty array array: ");
s = nats_JSONGetArrayArray(json, "empty", &arrArrVal, &arrCount);
testCond((s == NATS_OK) && (arrArrVal == NULL) && (arrCount == 0));

test("Get empty obj array: ");
s = nats_JSONGetArrayObject(json, "empty", &arrObjVal, &arrCount);
testCond((s == NATS_OK) && (arrObjVal == NULL) && (arrCount == 0));

test("Get empty ulong array: ");
s = nats_JSONGetArrayULong(json, "empty", &arrULongVal, &arrCount);
testCond((s == NATS_OK) && (arrULongVal == NULL) && (arrCount == 0));

test("Get empty long array: ");
s = nats_JSONGetArrayLong(json, "empty", &arrLongVal, &arrCount);
testCond((s == NATS_OK) && (arrLongVal == NULL) && (arrCount == 0));

test("Get empty int array: ");
s = nats_JSONGetArrayInt(json, "empty", &arrIntVal, &arrCount);
testCond((s == NATS_OK) && (arrIntVal == NULL) && (arrCount == 0));

test("Get empty double array: ");
s = nats_JSONGetArrayDouble(json, "empty", &arrDoubleVal, &arrCount);
testCond((s == NATS_OK) && (arrDoubleVal == NULL) && (arrCount == 0));

test("Get empty bool array: ");
s = nats_JSONGetArrayBool(json, "empty", &arrBoolVal, &arrCount);
testCond((s == NATS_OK) && (arrBoolVal == NULL) && (arrCount == 0));

test("Get empty string array: ");
s = nats_JSONGetArrayStr(json, "empty", &arrVal, &arrCount);
testCond((s == NATS_OK) && (arrVal == NULL) && (arrCount == 0));

nats_JSONDestroy(json);
json = NULL;

}

static void
Expand Down

0 comments on commit 0db15b9

Please sign in to comment.