Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate json_t from parsing the json string #143

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions src/ocispec/json_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/ocispec/json_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 8 additions & 10 deletions src/ocispec/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
)

Expand Down
Loading