Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow parsing empty, spaced JSON data

  • Loading branch information...
commit c92f2a07d31d13b5417ae1e83c7e69cabe33ee9d 1 parent 5c22718
@sigurdteigen sigurdteigen authored
Showing with 43 additions and 15 deletions.
  1. +23 −15 src/json.c
  2. +20 −0 tests/unit/json_test.c
View
38 src/json.c
@@ -802,23 +802,31 @@ static JsonElement *JsonParseAsObject(const char **data)
JsonElement *JsonParse(const char **data)
{
- assert(**data && "Cannot parse NULL data");
-
- if (**data == '{')
- {
- return JsonParseAsObject(data);
- }
- else if (**data == '[')
- {
- return JsonParseAsArray(data);
- }
- else if (**data == '"')
+ assert(data && *data);
+ if (data == NULL || *data == NULL)
{
- return JsonParseAsObject(data);
+ return NULL;
}
- else
+
+ while (**data)
{
- CfDebug("Don't know how to parse JSON input: %s", *data);
- return NULL;
+ if (**data == '{')
+ {
+ return JsonParseAsObject(data);
+ }
+ else if (**data == '[')
+ {
+ return JsonParseAsArray(data);
+ }
+ else if (**data == '"')
+ {
+ return JsonParseAsObject(data);
+ }
+ else
+ {
+ (*data)++;
+ }
}
+
+ return NULL;
}
View
20 tests/unit/json_test.c
@@ -297,6 +297,24 @@ static void test_parse_array_object(void **state)
JsonElementDestroy(arr);
}
+static void test_parse_empty(void **state)
+{
+ const char *data = "";
+ JsonElement *json = JsonParse(&data);
+
+ assert_false(json);
+}
+
+static void test_parse_trim(void **state)
+{
+ const char *data = " [] ";
+ JsonElement *json = JsonParse(&data);
+
+ assert_true(json);
+
+ JsonElementDestroy(json);
+}
+
static void test_array_remove_range(void **state)
{
{
@@ -381,6 +399,8 @@ int main()
unit_test(test_parse_array_simple),
unit_test(test_parse_object_compound),
unit_test(test_parse_array_object),
+ unit_test(test_parse_empty),
+ unit_test(test_parse_trim),
unit_test(test_array_remove_range)
};
Please sign in to comment.
Something went wrong with that request. Please try again.