Skip to contentSkip to conversation

json: add support for primitive types

PATCH New James Prestwood 1 participant 8 messages Jan 5, 2022

lore
Share & tools
lore

James Prestwood

author
#0 lore raw

PATCH 1/8 json: add support for primitive types

This adds support for boolean, (unsigned) integers, and null types. JSON_PRIMITIVE should be used as the type when parsing and the value should be struct json_iter.

Once parsed the actual value can be obtained using one of the primitive getters. If the type does not match they will return false.

If using JSON_OPTIONAL with JSON_PRIMITIVE the resulting iterator can be checked with json_iter_is_valid. If false the key/value was not found or the type was not matching.

---
 src/json.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 src/json.h |  13 ++++-
 2 files changed, 143 insertions(+), 8 deletions(-)
src/json.c +131 -7
index 39570e7e..a83ba464 100644
@@ -85,14 +85,26 @@ static int find_object_tokens(struct json_iter *iter, jsmntok_t *object)
 	return next - object - 1;
 }
 
-static void iter_recurse(struct json_iter *iter, jsmntok_t *object,
+static void iter_recurse(struct json_iter *iter, jsmntok_t *token,
 				struct json_iter *child)
 {
 	struct json_contents *c = iter->contents;
 
 	child->contents = c;
-	child->start = object - c->tokens;
-	child->count = find_object_tokens(iter, object);
+	child->start = token - c->tokens;
+
+	/*
+	* For objects iterating all tokens with the object as the parent
+	* parent should give the total number of tokens for this object.
+	*
+	* For strings/primitives the value is always going to one token which
+	* will not be iteratable. Because of this the count is set to zero to
+	* disallow any iteration on this child iterator.
+	*/
+	if (token->type == JSMN_OBJECT)
+		child->count = find_object_tokens(iter, token);
+	else
+		child->count = 0;
 }
 
 struct json_contents *json_contents_new(const char *json, size_t json_len)
@@ -153,7 +165,7 @@ static void assign_arg(void *data, void *user_data)
 	struct json_arg *arg = data;
 	struct json_contents *c = iter->contents;
 	char **sval;
-	struct json_iter *oval;
+	struct json_iter *iter_val;
 
 	switch (arg->type) {
 	case JSON_STRING:
@@ -163,12 +175,13 @@ static void assign_arg(void *data, void *user_data)
 
 		break;
 	case JSON_OBJECT:
-		oval = arg->value;
+	case JSON_PRIMITIVE:
+		iter_val = arg->value;
 
 		if (!arg->v)
-			oval->start = -1;
+			iter_val->start = -1;
 		else
-			iter_recurse(iter, arg->v, oval);
+			iter_recurse(iter, arg->v, iter_val);
 
 		break;
 	default:
@@ -209,6 +222,7 @@ bool json_iter_parse(struct json_iter *iter, enum json_type type, ...)
 			goto done;
 		case JSON_STRING:
 		case JSON_OBJECT:
+		case JSON_PRIMITIVE:
 			break;
 		default:
 			goto error;
@@ -268,3 +282,113 @@ error:
 	l_queue_destroy(args, l_free);
 	return false;
 }
+
+static bool iter_get_primitive_data(struct json_iter *iter, void **ptr,
+					size_t *len)
+{
+	struct json_contents *c = iter->contents;
+	jsmntok_t *t = c->tokens + iter->start;
+
+	if (t->type != JSMN_PRIMITIVE)
+		return false;
+
+	*ptr = TOK_PTR(c->json, t);
+	*len = TOK_LEN(t);
+
+	return true;
+}
+
+bool json_iter_get_int(struct json_iter *iter, int *i)
+{
+	void *ptr;
+	size_t len;
+	long int r;
+	int t;
+	char *endp;
+
+	if (json_iter_get_null(iter) || json_iter_get_boolean(iter, NULL))
+		return false;
+
+	if (!iter_get_primitive_data(iter, &ptr, &len))
+		return false;
+
+	errno = 0;
+
+	t = r = strtol(ptr, &endp, 0);
+	if (endp == ptr)
+		return false;
+
+	if (errno == ERANGE || r != t)
+		return false;
+
+	if (i)
+		*i = r;
+
+	return true;
+}
+
+bool json_iter_get_uint(struct json_iter *iter, unsigned int *i)
+{
+	void *ptr;
+	size_t len;
+	unsigned long int r;
+	unsigned int t;
+	char *endp;
+
+	if (json_iter_get_null(iter) || json_iter_get_boolean(iter, NULL))
+		return false;
+
+	if (!iter_get_primitive_data(iter, &ptr, &len))
+		return false;
+
+	errno = 0;
+
+	t = r = strtoul(ptr, &endp, 0);
+	if (endp == ptr)
+		return false;
+
+	if (errno == ERANGE || r != t)
+		return false;
+
+	if (i)
+		*i = r;
+
+	return true;
+}
+
+bool json_iter_get_boolean(struct json_iter *iter, bool *b)
+{
+	void *ptr;
+	size_t len;
+
+	if (!iter_get_primitive_data(iter, &ptr, &len))
+		return false;
+
+	if (len == 4 && !memcmp(ptr, "true", 4)) {
+		if (b)
+			*b = true;
+
+		return true;
+	} else if (len == 5 && !memcmp(ptr, "false", 5)) {
+		if (b)
+			*b = false;
+
+		return true;
+	}
+
+	return false;
+}
+
+bool json_iter_get_null(struct json_iter *iter)
+{
+	void *ptr;
+	size_t len;
+
+	if (!iter_get_primitive_data(iter, &ptr, &len))
+		return false;
+
+	if (len == 4 && !memcmp(ptr, "null", 4))
+		return true;
+
+	return false;
+}
src/json.h +12 -1
index 915b675b..9f00a9d5 100644
@@ -73,11 +73,22 @@ void json_iter_init(struct json_iter *iter, struct json_contents *c);
  *
  * String values should be of type char ** and must be freed
  * Object values should be of type struct json_iter *
+ * Primitive types (numbers, booleans, null) should be of type
+ * 		struct json_iter *. This is to allow the caller to distinguish
+ * 		between the actual value type after parsing using a getter for
+ * 		the expected type (get_uint/get_int/get_boolean etc.). In
+ * 		addition this lets the caller use JSON_OPTIONAL and check post
+ * 		json_iter_parse if the iterator is valid (json_iter_is_valid).
  *
  * No other types are supported at this time, and json_iter_parse will fail if
  * other types are encountered.
  *
  * JSON_OPTIONAL string values will point to NULL if not found
- * JSON_OPTIONAL objects can be checked with json_object_not_found.
+ * JSON_OPTIONAL objects/primitives can be checked with json_object_is_valid.
  */
 bool json_iter_parse(struct json_iter *iter, enum json_type type, ...);
+
+bool json_iter_get_int(struct json_iter *iter, int *i);
+bool json_iter_get_uint(struct json_iter *iter, unsigned int *i);
+bool json_iter_get_boolean(struct json_iter *iter, bool *b);
+bool json_iter_get_null(struct json_iter *iter);
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH 2/8 unit: add primitive json test

---
 unit/test-json.c | 111 ++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 95 insertions(+), 16 deletions(-)
unit/test-json.c +95 -16
index 854b92dd..fe8f756f 100644
@@ -84,8 +84,6 @@ static void test_json_unsupported_types(const void *data)
 	 * Valid JSON objects but currently unsupported types
 	 */
 	char arrays[] = "{\"test\":[1, 2, 3, 4]}";
-	char integers[] = "{\"test\": 10 }";
-	char bools[] = "{\"test\": true}";
 
 	struct json_iter iter;
 	struct json_contents *c = json_contents_new(arrays, strlen(arrays));
@@ -95,20 +93,6 @@ static void test_json_unsupported_types(const void *data)
 				JSON_MANDATORY("test", JSON_ARRAY, NULL),
 				JSON_UNDEFINED));
 	json_contents_free(c);
-
-	c = json_contents_new(integers, strlen(integers));
-	json_iter_init(&iter, c);
-	assert(!json_iter_parse(&iter,
-				JSON_MANDATORY("test", JSON_PRIMITIVE, NULL),
-				JSON_UNDEFINED));
-	json_contents_free(c);
-
-	c = json_contents_new(bools, strlen(bools));
-	json_iter_init(&iter, c);
-	assert(!json_iter_parse(&iter,
-				JSON_MANDATORY("test", JSON_PRIMITIVE, NULL),
-				JSON_UNDEFINED));
-	json_contents_free(c);
 }
 
 /*
@@ -248,6 +232,100 @@ static void test_json_larger_object(const void *data)
 	json_contents_free(c);
 }
 
+static void check_primitives(struct json_iter *i, struct json_iter *ui,
+				struct json_iter *t, struct json_iter *f,
+				struct json_iter *null, struct json_iter *obj)
+{
+
+	int i_val;
+	unsigned int ui_val;
+	bool b_val;
+
+	assert(json_iter_is_valid(i));
+	assert(!json_iter_get_uint(i, NULL));
+	assert(!json_iter_get_boolean(i, NULL));
+	assert(!json_iter_get_null(i));
+	assert(json_iter_get_int(i, &i_val));
+	assert(i_val == -10);
+
+	assert(json_iter_is_valid(ui));
+	assert(!json_iter_get_boolean(ui, NULL));
+	assert(!json_iter_get_null(ui));
+	assert(json_iter_get_int(ui, &i_val));
+	assert(json_iter_get_uint(ui, &ui_val));
+	assert(i_val == 10 && ui_val == 10);
+
+
+	assert(json_iter_is_valid(t));
+	assert(!json_iter_get_null(t));
+	assert(!json_iter_get_int(t, NULL));
+	assert(!json_iter_get_uint(t, NULL));
+	assert(json_iter_get_boolean(t, &b_val));
+	assert(b_val == true);
+
+	assert(json_iter_is_valid(f));
+	assert(!json_iter_get_null(f));
+	assert(!json_iter_get_int(f, NULL));
+	assert(!json_iter_get_uint(f, NULL));
+	assert(json_iter_get_boolean(f, &b_val));
+	assert(b_val == false);
+
+	assert(json_iter_is_valid(null));
+	assert(!json_iter_get_int(null, NULL));
+	assert(!json_iter_get_uint(null, NULL));
+	assert(!json_iter_get_boolean(null, NULL));
+	assert(json_iter_get_null(null));
+
+	if (obj) {
+		assert(json_iter_is_valid(obj));
+		assert(json_iter_parse(obj,
+			JSON_MANDATORY("null_val", JSON_PRIMITIVE, null),
+			JSON_MANDATORY("false_val", JSON_PRIMITIVE, f),
+			JSON_MANDATORY("true_val", JSON_PRIMITIVE, t),
+			JSON_MANDATORY("int_val", JSON_PRIMITIVE, i),
+			JSON_MANDATORY("uint_val", JSON_PRIMITIVE, ui),
+			JSON_UNDEFINED));
+
+		check_primitives(i, ui, t, f, null, NULL);
+	}
+}
+
+static void test_json_primitives(const void *data)
+{
+	char json[] = "{\"int_val\": -10,"
+			"\"uint_val\": 10,"
+			"\"true_val\": true,"
+			"\"false_val\": false,"
+			"\"null_val\": null,"
+			"\"obj_val\":{"
+				"\"int_val\": -10,"
+				"\"uint_val\": 10,"
+				"\"true_val\": true,"
+				"\"false_val\": false,"
+				"\"null_val\": null}}";
+	struct json_contents *c = json_contents_new(json, strlen(json));
+	struct json_iter outer, inner, null, f, t, i, ui;
+	struct json_iter not_found;
+
+	json_iter_init(&outer, c);
+	assert(json_iter_parse(&outer,
+			JSON_MANDATORY("obj_val", JSON_OBJECT, &inner),
+			JSON_MANDATORY("null_val", JSON_PRIMITIVE, &null),
+			JSON_MANDATORY("false_val", JSON_PRIMITIVE, &f),
+			JSON_MANDATORY("true_val", JSON_PRIMITIVE, &t),
+			JSON_MANDATORY("int_val", JSON_PRIMITIVE, &i),
+			JSON_MANDATORY("uint_val", JSON_PRIMITIVE, &ui),
+			JSON_OPTIONAL("not_found", JSON_PRIMITIVE, &not_found),
+			JSON_UNDEFINED));
+
+	assert(!json_iter_is_valid(&not_found));
+
+	check_primitives(&i, &ui, &t, &f, &null, &inner);
+
+	json_contents_free(c);
+}
+
+
 int main(int argc, char *argv[])
 {
 	l_test_init(&argc, &argv);
@@ -259,6 +337,7 @@ int main(int argc, char *argv[])
 	l_test_add("json empty objects", test_json_empty_objects, NULL);
 	l_test_add("json parse out of order", test_json_out_of_order, NULL);
 	l_test_add("json larger object", test_json_larger_object, NULL);
+	l_test_add("json test primitives", test_json_primitives, NULL);
 
 	return l_test_run();
 }
Signature · 1 line
2.31.1
author
#2 lore raw

PATCH 3/8 json: add support for array iteration

In reply to James Prestwood

Arrays can now be parsed using the JSON_ARRAY type (stored in a struct json_iter) then iterated using json_iter_next. When iterating the type can be checked with json_iter_get_type. For each iteration the value can be obtained using any of the type getters (int/uint/boolean/null).

For now only primitive type arrays are supported.

Several internal APIs were renamed/altered to make them more general purpose for both arrays and objects.

---
 src/json.c | 64 ++++++++++++++++++++++++++++++++++++++++++------------
 src/json.h |  5 +++++
 2 files changed, 55 insertions(+), 14 deletions(-)
src/json.c +50 -14
index a83ba464..879d43be 100644
@@ -56,7 +56,8 @@ struct json_contents {
 	jsmn_parser *p;
 };
 
-static jsmntok_t *next_key_in_parent(struct json_iter *iter, jsmntok_t *current)
+static jsmntok_t *next_token_in_parent(struct json_iter *iter,
+					jsmntok_t *current)
 {
 	int parent = current->parent;
 
@@ -69,20 +70,14 @@ static jsmntok_t *next_key_in_parent(struct json_iter *iter, jsmntok_t *current)
 	return NULL;
 }
 
-/*
- * 'object' is expected to be a value, so object - 1 is its key. Find
- * the next key who's parent matches the parent of object - 1. The
- * token preceeding this next key will mark the end of 'object'.
- */
-static int find_object_tokens(struct json_iter *iter, jsmntok_t *object)
+static int find_parent_tokens(struct json_iter *iter, jsmntok_t *token)
 {
-	jsmntok_t *next = next_key_in_parent(iter, object - 1);
+	jsmntok_t *next = next_token_in_parent(iter, token);
 
-	/* End of token list */
 	if (!next)
 		next = ITER_END(iter);
 
-	return next - object - 1;
+	return next - token;
 }
 
 static void iter_recurse(struct json_iter *iter, jsmntok_t *token,
@@ -92,18 +87,29 @@ static void iter_recurse(struct json_iter *iter, jsmntok_t *token,
 
 	child->contents = c;
 	child->start = token - c->tokens;
+	child->current = child->start;
 
 	/*
 	* For objects iterating all tokens with the object as the parent
 	* parent should give the total number of tokens for this object.
 	*
+	* For arrays we want to count the tokens inside the array itself. We
+	* cannot simply count until we reach a different parent than the array
+	* due to the possibility of nested objects. Instead we find the next
+	* token with the same parent as the array (the next key in the object).
+	* This offset will include both the arrays key, and the array itself,
+	* hence subtracting 2.
+	*
 	* For strings/primitives the value is always going to one token which
 	* will not be iteratable. Because of this the count is set to zero to
 	* disallow any iteration on this child iterator.
 	*/
 	if (token->type == JSMN_OBJECT)
-		child->count = find_object_tokens(iter, token);
-	else
+		child->count = find_parent_tokens(iter, token);
+	else if (token->type == JSMN_ARRAY) {
+		child->count = find_parent_tokens(iter, token - 1) - 2;
+		child->array = true;
+	} else
 		child->count = 0;
 }
 
@@ -176,6 +182,7 @@ static void assign_arg(void *data, void *user_data)
 		break;
 	case JSON_OBJECT:
 	case JSON_PRIMITIVE:
+	case JSON_ARRAY:
 		iter_val = arg->value;
 
 		if (!arg->v)
@@ -223,6 +230,7 @@ bool json_iter_parse(struct json_iter *iter, enum json_type type, ...)
 		case JSON_STRING:
 		case JSON_OBJECT:
 		case JSON_PRIMITIVE:
+		case JSON_ARRAY:
 			break;
 		default:
 			goto error;
@@ -252,7 +260,7 @@ bool json_iter_parse(struct json_iter *iter, enum json_type type, ...)
 				break;
 			}
 
-			next = next_key_in_parent(iter, next);
+			next = next_token_in_parent(iter, next);
 			if (!next)
 				break;
 		}
@@ -287,7 +295,7 @@ static bool iter_get_primitive_data(struct json_iter *iter, void **ptr,
 					size_t *len)
 {
 	struct json_contents *c = iter->contents;
-	jsmntok_t *t = c->tokens + iter->start;
+	jsmntok_t *t = c->tokens + iter->current;
 
 	if (t->type != JSMN_PRIMITIVE)
 		return false;
@@ -392,3 +400,31 @@ bool json_iter_get_null(struct json_iter *iter)
 
 	return false;
 }
+
+enum json_type json_iter_get_type(struct json_iter *iter)
+{
+	struct json_contents *c = iter->contents;
+	jsmntok_t *t = c->tokens + iter->current;
+
+	return (enum json_type) t->type;
+}
+
+bool json_iter_next(struct json_iter *iter)
+{
+	struct json_contents *c = iter->contents;
+
+	/* For now only allow json_iter_next() on arrays */
+	if (!iter->array)
+		return false;
+
+	if (c->tokens + iter->current + 1 > ITER_END(iter))
+		return false;
+
+	iter->current++;
+
+	/* TODO: Add support for nested array iteration */
+	if ((c->tokens + iter->current)->type == JSMN_ARRAY)
+		return false;
+
+	return true;
+}
src/json.h +5 -0
index 9f00a9d5..75ac9853 100644
@@ -42,6 +42,8 @@ struct json_iter {
 	struct json_contents *contents;
 	int start;
 	int count;
+	int current;
+	bool array : 1;
 };
 
 #define JSON_MANDATORY(key, type, out) \
@@ -92,3 +94,6 @@ bool json_iter_get_int(struct json_iter *iter, int *i);
 bool json_iter_get_uint(struct json_iter *iter, unsigned int *i);
 bool json_iter_get_boolean(struct json_iter *iter, bool *b);
 bool json_iter_get_null(struct json_iter *iter);
+
+enum json_type json_iter_get_type(struct json_iter *iter);
+bool json_iter_next(struct json_iter *iter);
Signature · 1 line
2.31.1
author
#3 lore raw

PATCH 4/8 json: increase default token size to 60

In reply to James Prestwood
---
 src/json.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/json.c +1 -1
index 879d43be..4e4c004e 100644
@@ -33,7 +33,7 @@
 #include "shared/jsmn.h"
 
 /* Max number of tokens supported. Increase if larger objects are expected */
-#define JSON_DEFAULT_TOKENS 30
+#define JSON_DEFAULT_TOKENS 60
 
 #define TOK_LEN(token) (token)->end - (token)->start
 #define TOK_PTR(json, token) (void *)((json) + (token)->start)
Signature · 1 line
2.31.1
author
#4 lore raw

PATCH 5/8 unit: add test for json arrays

In reply to James Prestwood
---
 unit/test-json.c | 113 ++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 92 insertions(+), 21 deletions(-)
unit/test-json.c +92 -21
index fe8f756f..1a7daaff 100644
@@ -75,26 +75,6 @@ static void test_json_escaped_unicode(const void *data)
 	json_contents_free(c);
 }
 
-/*
- * Tests that unsupported types will not parse
- */
-static void test_json_unsupported_types(const void *data)
-{
-	/*
-	 * Valid JSON objects but currently unsupported types
-	 */
-	char arrays[] = "{\"test\":[1, 2, 3, 4]}";
-
-	struct json_iter iter;
-	struct json_contents *c = json_contents_new(arrays, strlen(arrays));
-
-	json_iter_init(&iter, c);
-	assert(!json_iter_parse(&iter,
-				JSON_MANDATORY("test", JSON_ARRAY, NULL),
-				JSON_UNDEFINED));
-	json_contents_free(c);
-}
-
 /*
  * Basic test string values and nested objects
  */
@@ -325,6 +305,97 @@ static void test_json_primitives(const void *data)
 	json_contents_free(c);
 }
 
+static void test_json_arrays(const void *data)
+{
+	unsigned int ui;
+	int i;
+	bool b;
+	int count;
+	char json[] = "{\"uint_array\":[1, 2, 3, 4, 5, 6],"
+			"\"int_array\":[-1, -2, -3, -4, -5, -6],"
+			"\"bool_array\":[true, false, true, false],"
+			"\"null_array\":[null, null, null, null],"
+			"\"mixed_array\":[1, -1, true, false, null]}";
+
+	struct json_iter iter;
+	struct json_iter i_array, ui_array, b_array, n_array, m_array;
+	struct json_contents *c = json_contents_new(json, strlen(json));
+
+	json_iter_init(&iter, c);
+	assert(json_iter_parse(&iter,
+			JSON_MANDATORY("mixed_array", JSON_ARRAY, &m_array),
+			JSON_MANDATORY("null_array", JSON_ARRAY, &n_array),
+			JSON_MANDATORY("bool_array", JSON_ARRAY, &b_array),
+			JSON_MANDATORY("int_array", JSON_ARRAY, &i_array),
+			JSON_MANDATORY("uint_array", JSON_ARRAY, &ui_array),
+			JSON_UNDEFINED));
+
+	count = 1;
+
+	while (json_iter_next(&ui_array)) {
+		assert(json_iter_get_type(&ui_array) == JSON_PRIMITIVE);
+		assert(json_iter_get_uint(&ui_array, &ui));
+		assert(ui == (unsigned int) count);
+		count++;
+	}
+
+	count = -1;
+
+	while (json_iter_next(&i_array)) {
+		assert(json_iter_get_type(&i_array) == JSON_PRIMITIVE);
+		assert(json_iter_get_int(&i_array, &i));
+		assert(i == count);
+		count--;
+	}
+
+	count = 0;
+
+	while (json_iter_next(&b_array)) {
+		assert(json_iter_get_type(&b_array) == JSON_PRIMITIVE);
+		assert(json_iter_get_boolean(&b_array, &b));
+		assert(b == count % 2 ? false : true);
+		count++;
+	}
+
+	count = 0;
+
+	while (json_iter_next(&n_array)) {
+		assert(json_iter_get_type(&n_array) == JSON_PRIMITIVE);
+		assert(json_iter_get_null(&n_array));
+		count++;
+	}
+
+	assert(count == 4);
+
+	count = 0;
+
+	while (json_iter_next(&m_array)) {
+		assert(json_iter_get_type(&m_array) == JSON_PRIMITIVE);
+
+		switch (count) {
+		case 0:
+			assert(json_iter_get_uint(&m_array, &ui));
+			assert(ui == 1);
+			break;
+		case 1:
+			assert(json_iter_get_int(&m_array, &i));
+			assert(i == -1);
+			break;
+		case 2:
+		case 3:
+			assert(json_iter_get_boolean(&m_array, &b));
+			assert(b == count % 2 ? false : true);
+			break;
+		case 4:
+			assert(json_iter_get_null(&m_array));
+			break;
+		}
+
+		count++;
+	}
+
+	json_contents_free(c);
+}
 
 int main(int argc, char *argv[])
 {
@@ -333,11 +404,11 @@ int main(int argc, char *argv[])
 	l_test_add("json unicode", test_json_unicode, NULL);
 	l_test_add("json escaped unicode", test_json_escaped_unicode, NULL);
 	l_test_add("json nested objects", test_json, NULL);
-	l_test_add("json unsupported types", test_json_unsupported_types, NULL);
 	l_test_add("json empty objects", test_json_empty_objects, NULL);
 	l_test_add("json parse out of order", test_json_out_of_order, NULL);
 	l_test_add("json larger object", test_json_larger_object, NULL);
 	l_test_add("json test primitives", test_json_primitives, NULL);
+	l_test_add("json test arrays", test_json_arrays, NULL);
 
 	return l_test_run();
 }
Signature · 1 line
2.31.1
author
#5 lore raw

PATCH 6/8 json: support arrays of objects

In reply to James Prestwood
---
 src/json.c | 39 +++++++++++++++++++++++++++++++++++++--
 src/json.h |  2 ++
 2 files changed, 39 insertions(+), 2 deletions(-)
src/json.c +37 -2
index 4e4c004e..d38d884e 100644
@@ -401,6 +401,21 @@ bool json_iter_get_null(struct json_iter *iter)
 	return false;
 }
 
+bool json_iter_get_container(struct json_iter *iter,
+				struct json_iter *container)
+{
+	struct json_contents *c = iter->contents;
+	jsmntok_t *t = c->tokens + iter->current;
+
+	/* TODO: Add nested array support */
+	if (t->type != JSMN_OBJECT)
+		return false;
+
+	iter_recurse(iter, t, container);
+
+	return true;
+}
+
 enum json_type json_iter_get_type(struct json_iter *iter)
 {
 	struct json_contents *c = iter->contents;
@@ -412,15 +427,35 @@ enum json_type json_iter_get_type(struct json_iter *iter)
 bool json_iter_next(struct json_iter *iter)
 {
 	struct json_contents *c = iter->contents;
+	jsmntok_t *t = c->tokens + iter->current;
+	int inc = 1;
 
 	/* For now only allow json_iter_next() on arrays */
 	if (!iter->array)
 		return false;
 
-	if (c->tokens + iter->current + 1 > ITER_END(iter))
+	/*
+	 * If this is the initial iteration skip this and just increment
+	 * current by 1 since this iterator points to an array which needs to
+	 * be advanced to the first token in the array.
+	 *
+	 * In addition primitive types and empty object will have a size of 1,
+	 * so no special handling is needed there.
+	 *
+	 * For objects (and arrays in the future), 'current' needs to be
+	 * advanced by all objects child tokens plus the object token itself.
+	 * This check ensures:
+	 *    1. It is not the initial iteration
+	 *    2. This is an object
+	 *    3. The object is not empty
+	 */
+	if (iter->current != iter->start && t->type == JSMN_OBJECT && t->size)
+		inc = find_parent_tokens(iter, t) + 1;
+
+	if (c->tokens + iter->current + inc > ITER_END(iter))
 		return false;
 
-	iter->current++;
+	iter->current += inc;
 
 	/* TODO: Add support for nested array iteration */
 	if ((c->tokens + iter->current)->type == JSMN_ARRAY)
src/json.h +2 -0
index 75ac9853..c7993a5a 100644
@@ -94,6 +94,8 @@ bool json_iter_get_int(struct json_iter *iter, int *i);
 bool json_iter_get_uint(struct json_iter *iter, unsigned int *i);
 bool json_iter_get_boolean(struct json_iter *iter, bool *b);
 bool json_iter_get_null(struct json_iter *iter);
+bool json_iter_get_container(struct json_iter *iter,
+				struct json_iter *container);
 
 enum json_type json_iter_get_type(struct json_iter *iter);
 bool json_iter_next(struct json_iter *iter);
Signature · 1 line
2.31.1
author
#6 lore raw

PATCH 7/8 unit: add arrays of objects test

In reply to James Prestwood
---
 unit/test-json.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
unit/test-json.c +28 -1
index 1a7daaff..52e3f675 100644
@@ -315,10 +315,12 @@ static void test_json_arrays(const void *data)
 			"\"int_array\":[-1, -2, -3, -4, -5, -6],"
 			"\"bool_array\":[true, false, true, false],"
 			"\"null_array\":[null, null, null, null],"
+			"\"obj_array\":[{}, {\"key\":\"value\", \"key2\":\"value2\"}],"
 			"\"mixed_array\":[1, -1, true, false, null]}";
 
 	struct json_iter iter;
-	struct json_iter i_array, ui_array, b_array, n_array, m_array;
+	struct json_iter i_array, ui_array, b_array, n_array,
+				m_array, obj_array;
 	struct json_contents *c = json_contents_new(json, strlen(json));
 
 	json_iter_init(&iter, c);
@@ -328,6 +330,7 @@ static void test_json_arrays(const void *data)
 			JSON_MANDATORY("bool_array", JSON_ARRAY, &b_array),
 			JSON_MANDATORY("int_array", JSON_ARRAY, &i_array),
 			JSON_MANDATORY("uint_array", JSON_ARRAY, &ui_array),
+			JSON_MANDATORY("obj_array", JSON_ARRAY, &obj_array),
 			JSON_UNDEFINED));
 
 	count = 1;
@@ -394,6 +397,30 @@ static void test_json_arrays(const void *data)
 		count++;
 	}
 
+	count = 0;
+
+	while (json_iter_next(&obj_array)) {
+		struct json_iter object;
+
+		assert(json_iter_get_type(&obj_array) == JSON_OBJECT);
+		assert(json_iter_get_container(&obj_array, &object));
+
+		switch (count) {
+		case 0:
+			assert(json_iter_parse(&object, JSON_UNDEFINED));
+			break;
+		case 1:
+			assert(json_iter_parse(&object,
+				JSON_MANDATORY("key", JSON_STRING, NULL),
+				JSON_UNDEFINED));
+			break;
+		}
+
+		count++;
+	}
+
+	assert(count == 2);
+
 	json_contents_free(c);
 }
 
Signature · 1 line
2.31.1
author
#7 lore raw

PATCH 8/8 unit: add test for nested arrays

In reply to James Prestwood

These are not yet supported, but test anyway to ensure iteration does not work and doesn't result in any unexpected behavior.

---
 unit/test-json.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
unit/test-json.c +22 -0
index 52e3f675..5ffd36ec 100644
@@ -424,6 +424,27 @@ static void test_json_arrays(const void *data)
 	json_contents_free(c);
 }
 
+static void test_json_nested_arrays(const void *data)
+{
+	char json[] = "{\"array\":[[1, 2], [3, 4]]}";
+	int count = 0;
+	struct json_iter iter;
+	struct json_iter array;
+	struct json_contents *c = json_contents_new(json, strlen(json));
+
+	json_iter_init(&iter, c);
+	assert(json_iter_parse(&iter,
+			JSON_MANDATORY("array", JSON_ARRAY, &array),
+			JSON_UNDEFINED));
+
+	while (json_iter_next(&array))
+		count++;
+
+	assert(count == 0);
+
+	json_contents_free(c);
+}
+
 int main(int argc, char *argv[])
 {
 	l_test_init(&argc, &argv);
@@ -436,6 +457,7 @@ int main(int argc, char *argv[])
 	l_test_add("json larger object", test_json_larger_object, NULL);
 	l_test_add("json test primitives", test_json_primitives, NULL);
 	l_test_add("json test arrays", test_json_arrays, NULL);
+	l_test_add("json test nested arrays", test_json_nested_arrays, NULL);
 
 	return l_test_run();
 }
Signature · 1 line
2.31.1