json: add json_iter_get_string
James Prestwood
PATCH 1/5 json: add json_iter_get_string
--- src/json.c | 14 ++++++++++++++ src/json.h | 1 + 2 files changed, 15 insertions(+)
src/json.c +14 -0
@@ -398,6 +398,20 @@ bool json_iter_get_container(struct json_iter *iter,return true; } +bool json_iter_get_string (struct json_iter *iter,char **s) +{ +struct json_contents *c = iter->contents; +jsmntok_t *t = c->tokens + iter->current; + +if (t->type !=JSMN_STRING ) +return false; + +if (s) + *s =TOK_TO_STR (c->json, t); + +return true; +} +enum json_type json_iter_get_type (struct json_iter *iter) {struct json_contents *c = iter->contents;
src/json.h +1 -0
@@ -95,6 +95,7 @@ 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); +bool json_iter_get_string (struct json_iter *iter,char **s);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
James Prestwood
PATCH 2/5 unit: add strings to several json iteration tests
--- unit/test-json.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-)
unit/test-json.c +38 -5
@@ -315,7 +315,7 @@ static void test_json_arrays(const void *data)"\"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]}" ; +"\"mixed_array\":[1, -1, true, false, null, \"string\"]}" ;struct json_iter iter; struct json_iter i_array, ui_array, b_array, n_array,
@@ -372,25 +372,34 @@ static void test_json_arrays(const void *data) count =0 ;while (json_iter_next (&m_array)) { -assert (json_iter_get_type (&m_array) ==JSON_PRIMITIVE ); + _auto_(l_free) char *str = NULL;switch (count) {case 0 : +assert (json_iter_get_type (&m_array) ==JSON_PRIMITIVE );assert (json_iter_get_uint (&m_array, &ui));assert (ui ==1 );break ;case 1 : +assert (json_iter_get_type (&m_array) ==JSON_PRIMITIVE );assert (json_iter_get_int (&m_array, &i));assert (i ==-1 );break ;case 2 :case 3 : +assert (json_iter_get_type (&m_array) ==JSON_PRIMITIVE );assert (json_iter_get_boolean (&m_array, &b));assert (b == count %2 ? false : true);break ;case 4 : +assert (json_iter_get_type (&m_array) ==JSON_PRIMITIVE );assert (json_iter_get_null (&m_array));break ; +case 5 : +assert (json_iter_get_type (&m_array) ==JSON_STRING ); +assert (json_iter_get_string (&m_array, &str)); +assert (!strcmp (str,"string" )); +break ; } count++;
@@ -425,7 +434,7 @@ static void test_json_arrays(const void *data)static void test_json_nested_arrays (const void *data) { -char json[] ="{\"array\":[[], {}, [1, 2], {\"key\":\"value\"}]}" ; +char json[] ="{\"array\":[[], {}, [1, 2], {\"key\":\"value\"}, [\"one\",\"two\"]]}" ;int count =0 ;struct json_iter iter;struct json_iter array;
@@ -442,8 +451,29 @@ static void test_json_nested_arrays(const void *data)assert (json_iter_get_container (&array, &inner)); -while (json_iter_next (&inner)) +while (json_iter_next (&inner)) { + _auto_(l_free) char *str = NULL; + +switch (count) { +case 0 : +case 1 : +assert (false); +break ; +case 4 : +assert (json_iter_get_type (&inner) == +JSON_STRING ); +assert (json_iter_get_string (&inner, &str)); + +if (count2 ==0 ) +assert (!strcmp ("one" , str)); +else +assert (!strcmp ("two" , str)); + +break ; + } + count2++; + }/* * TODO: add checks for object iteration. Currently these will
@@ -456,6 +486,9 @@ static void test_json_nested_arrays(const void *data)case 2 :assert (count2 ==2 );break ; +case 4 : +assert (count2 ==2 ); +break ;default :break ; }
@@ -463,7 +496,7 @@ static void test_json_nested_arrays(const void *data) count++; } -assert (count ==4 ); +assert (count ==5 );json_contents_free (c); }
Signature · 1 line
2.31.1
James Prestwood
PATCH 3/5 json: add NULL check for container on get_container
In reply to James PrestwoodIn case the caller just wants to check an object is iterable, without actually iterating it.
--- src/json.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
src/json.c +2 -1
@@ -393,7 +393,8 @@ bool json_iter_get_container(struct json_iter *iter,if (t->type !=JSMN_OBJECT && t->type !=JSMN_ARRAY )return false; -iter_recurse (iter, t, container); +if (container) +iter_recurse (iter, t, container);return true; }
Signature · 1 line
2.31.1
James Prestwood
PATCH 4/5 json: restrict json_iter_{parse,next} to objects/arrays
In reply to James Prestwoodjson_iter_parse is only meant to work on objects while json_iter_next is only meant to work on arrays.
This adds checks in both APIs to ensure they aren't being used incorrectly.
--- src/json.c | 6 ++++++ 1 file changed, 6 insertions(+)
src/json.c +6 -0
@@ -200,6 +200,9 @@ bool json_iter_parse(struct json_iter *iter, enum json_type type, ...)if (iter->start ==-1 )return false; +if (c->tokens[iter->start].type !=JSMN_OBJECT ) +return false; + args =l_queue_new ();va_start (va, type);
@@ -427,6 +430,9 @@ bool json_iter_next(struct json_iter *iter)jsmntok_t *t = c->tokens + iter->current;int inc =1 ; +if (iter->current == iter->start && t->type !=JSMN_ARRAY ) +return false; +/* * If this is the initial iteration skip this and just increment * current by 1 since this iterator points to a container which needs to
Signature · 1 line
2.31.1
James Prestwood
PATCH 5/5 unit: add checks for improper use of json_iter_parse/next
In reply to James PrestwoodThis adds checks to make sure these API's don't operate on objects/primitives they aren't supposed to.
--- unit/test-json.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-)
unit/test-json.c +18 -4
@@ -225,12 +225,14 @@ static void check_primitives(struct json_iter *i, struct json_iter *ui,assert (!json_iter_get_uint (i,NULL ));assert (!json_iter_get_boolean (i,NULL ));assert (!json_iter_get_null (i)); +assert (!json_iter_next (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_next (ui));assert (json_iter_get_int (ui, &i_val));assert (json_iter_get_uint (ui, &ui_val));assert (i_val ==10 && ui_val ==10 );
@@ -239,6 +241,7 @@ static void check_primitives(struct json_iter *i, struct json_iter *ui,assert (!json_iter_get_null (t));assert (!json_iter_get_int (t,NULL ));assert (!json_iter_get_uint (t,NULL )); +assert (!json_iter_next (t));assert (json_iter_get_boolean (t, &b_val));assert (b_val == true);
@@ -246,6 +249,7 @@ static void check_primitives(struct json_iter *i, struct json_iter *ui,assert (!json_iter_get_null (f));assert (!json_iter_get_int (f,NULL ));assert (!json_iter_get_uint (f,NULL )); +assert (!json_iter_next (f));assert (json_iter_get_boolean (f, &b_val));assert (b_val == false);
@@ -253,10 +257,12 @@ static void check_primitives(struct json_iter *i, struct json_iter *ui,assert (!json_iter_get_int (null,NULL ));assert (!json_iter_get_uint (null,NULL ));assert (!json_iter_get_boolean (null,NULL )); +assert (!json_iter_next (null));assert (json_iter_get_null (null));if (obj) {assert (json_iter_is_valid (obj)); +assert (!json_iter_next (obj)); assert(json_iter_parse(obj, JSON_MANDATORY("null_val", JSON_PRIMITIVE, null), JSON_MANDATORY("false_val", JSON_PRIMITIVE, f),
@@ -336,6 +342,7 @@ static void test_json_arrays(const void *data)while (json_iter_next (&ui_array)) {assert (json_iter_get_type (&ui_array) ==JSON_PRIMITIVE ); +assert (!json_iter_parse (&ui_array,JSON_UNDEFINED ));assert (json_iter_get_uint (&ui_array, &ui));assert (ui == (unsigned int ) count); count++;
@@ -345,6 +352,7 @@ static void test_json_arrays(const void *data)while (json_iter_next (&i_array)) {assert (json_iter_get_type (&i_array) ==JSON_PRIMITIVE ); +assert (!json_iter_parse (&i_array,JSON_UNDEFINED ));assert (json_iter_get_int (&i_array, &i));assert (i == count); count--;
@@ -354,6 +362,7 @@ static void test_json_arrays(const void *data)while (json_iter_next (&b_array)) {assert (json_iter_get_type (&b_array) ==JSON_PRIMITIVE ); +assert (!json_iter_parse (&b_array,JSON_UNDEFINED ));assert (json_iter_get_boolean (&b_array, &b));assert (b == count %2 ? false : true); count++;
@@ -363,6 +372,7 @@ static void test_json_arrays(const void *data)while (json_iter_next (&n_array)) {assert (json_iter_get_type (&n_array) ==JSON_PRIMITIVE ); +assert (!json_iter_parse (&n_array,JSON_UNDEFINED ));assert (json_iter_get_null (&n_array)); count++; }
@@ -407,6 +417,8 @@ static void test_json_arrays(const void *data) count =0 ; +assert (!json_iter_parse (&obj_array,JSON_UNDEFINED )); +while (json_iter_next (&obj_array)) {struct json_iter object;
@@ -475,17 +487,19 @@ static void test_json_nested_arrays(const void *data) count2++; } -/* -* TODO: add checks for object iteration. Currently these will -* just count all tokens in the object. -*/ switch (count) {case 0 :assert (count2 ==0 );break ; +case 1 : +assert (count2 ==0 ); +break ;case 2 :assert (count2 ==2 );break ; +case 3 : +assert (count2 ==0 ); +break ;case 4 :assert (count2 ==2 );break ;
Signature · 1 line
2.31.1