From 1de626ef70679c690097e2104ca8facd4de3bc35 Mon Sep 17 00:00:00 2001 From: Sourav Moitra Date: Wed, 20 Nov 2024 10:52:48 +0530 Subject: [PATCH 1/2] Generate json_t from parsing the json string Signed-off-by: Sourav Moitra --- src/ocispec/sources.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/ocispec/sources.py b/src/ocispec/sources.py index e085c2da..e27ac520 100755 --- a/src/ocispec/sources.py +++ b/src/ocispec/sources.py @@ -1572,13 +1572,13 @@ def get_c_epilog(c_file, prefix, typ, obj): """) c_file.append(""" -define_cleaner_function (yajl_val, yajl_tree_free) +define_cleaner_function (json_t *, json_decref) """ + f"\n {typename} * " + f"{typename}_parse_data (const char *jsondata, const struct parser_context *ctx, parser_error *err)\n {{ \n" + - f" {typename} *ptr = NULL;" + - """__auto_cleanup(yajl_tree_free) yajl_val tree = NULL; - char errbuf[1024]; + f" {typename} *ptr = NULL;\n" + + """\t__auto_cleanup(json_decref) json_t *tree = NULL; + json_error_t error; struct parser_context tmp_ctx = { 0 }; if (jsondata == NULL || err == NULL) @@ -1588,16 +1588,14 @@ def get_c_epilog(c_file, prefix, typ, obj): if (ctx == NULL) ctx = (const struct parser_context *)(&tmp_ctx); - tree = yajl_tree_parse (jsondata, errbuf, sizeof (errbuf)); - // we will rename the jtree to tree once we use jansson to parse - json_t *jtree = yajl_to_json(tree); - if (jtree == NULL) + tree = json_loads (jsondata, 0, &error); + if (tree == NULL) { - if (asprintf (err, "cannot parse the data: %s", errbuf) < 0) + if (asprintf (err, "cannot parse the data: %s", error.text) < 0) *err = strdup ("error allocating memory"); return NULL; }\n""" + - f"\tptr = make_{typename} (jtree, ctx, err);\n" + + f"\tptr = make_{typename} (tree, ctx, err);\n" + "\treturn ptr; \n}\n" ) From 6d1252a5772c147d5ed702c34391cf56506328dc Mon Sep 17 00:00:00 2001 From: Sourav Moitra Date: Wed, 20 Nov 2024 12:10:15 +0530 Subject: [PATCH 2/2] Remove yajl_to_json no longer needed Signed-off-by: Sourav Moitra --- src/ocispec/json_common.c | 37 ------------------------------------- src/ocispec/json_common.h | 2 -- 2 files changed, 39 deletions(-) diff --git a/src/ocispec/json_common.c b/src/ocispec/json_common.c index 563e5605..0ab01c63 100644 --- a/src/ocispec/json_common.c +++ b/src/ocispec/json_common.c @@ -1903,43 +1903,6 @@ json_marshal_string (const char *str, size_t length, const struct parser_context return json_buf; } -/* -This function is temporary function to convert yajl_val. This fuction will -not be required once we completely move to jansson - -Input: yajl_val - -Output: json_t -*/ -json_t *yajl_to_json(yajl_val val) { - if YAJL_IS_NULL(val) { - return json_null(); - } else if (YAJL_IS_TRUE(val)) { - return json_true(); - } else if (YAJL_IS_FALSE(val)) { - return json_false(); - } else if (YAJL_IS_DOUBLE(val)) { - return json_real(YAJL_GET_DOUBLE(val)); - } else if (YAJL_IS_INTEGER(val)) { - return json_integer(YAJL_GET_INTEGER(val)); - } else if (YAJL_IS_STRING(val)) { - return json_string(YAJL_GET_STRING(val)); - } else if (YAJL_IS_ARRAY(val)) { - json_t *jansson_array = json_array(); - for (size_t i = 0; i < val->u.array.len; i++) { - json_array_append(jansson_array, yajl_to_json(val->u.array.values[i])); - } - return jansson_array; - } else { - json_t *jansson_object = json_object(); - for (size_t i = 0; i < val->u.object.len; i++) { - json_object_set_new(jansson_object, val->u.object.keys[i], yajl_to_json(val->u.object.values[i])); - } - return jansson_object; - } - return NULL; -} - /** * json_array_to_struct This function extracts keys and values and stores it in struct * Input: json_t diff --git a/src/ocispec/json_common.h b/src/ocispec/json_common.h index 530ef5f6..538fab47 100644 --- a/src/ocispec/json_common.h +++ b/src/ocispec/json_common.h @@ -254,8 +254,6 @@ int append_json_map_string_string (json_map_string_string *map, const char *key, char *json_marshal_string (const char *str, size_t length, const struct parser_context *ctx, parser_error *err); -json_t *yajl_to_json(yajl_val val); - typedef struct { json_t * values;