Skip to content

Commit

Permalink
Array and Objects converted to json_t also added Containerfile
Browse files Browse the repository at this point in the history
Signed-off-by: Sourav Moitra <[email protected]>
  • Loading branch information
xw19 committed Nov 15, 2024
1 parent 038dec3 commit fb861a0
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 126 deletions.
23 changes: 23 additions & 0 deletions Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM ubuntu:22.04

RUN apt-get update && \
apt-get install -y \
autoconf \
build-essential \
git \
libtool \
pkg-config \
python3


COPY . libocispec

RUN cd libocispec/jansson && autoreconf -fi && ./configure && make && make install

RUN cd libocispec && \
export JANSSON_CFLAGS=-I/usr/local/include && \
export JANSSON_LIBS=/usr/local/lib/libjansson.so && \
./autogen.sh --enable-embedded-yajl && \
./configure --enable-embedded-yajl CFLAGS='-Wall -Wextra -Werror' && \
make -j $(nproc) distcheck DISTCHECK_CONFIGURE_FLAGS="--enable-embedded-yajl" AM_DISTCHECK_DVI_TARGET="" TESTS="" && \
make clean
6 changes: 3 additions & 3 deletions src/ocispec/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def append_header_arr(obj, header, prefix):
typename = helpers.get_name_substr(obj.name, prefix)
header.append(f"}}\n{typename};\n\n")
header.append(f"void free_{typename} ({typename} *ptr);\n\n")
header.append(f"{typename} *make_{typename} (yajl_val tree, const struct parser_context *ctx, parser_error *err);\n\n")
header.append(f"{typename} *make_{typename} (json_t *jtree, const struct parser_context *ctx, parser_error *err);\n\n")


def append_header_map_str_obj(obj, header, prefix):
Expand Down Expand Up @@ -164,7 +164,7 @@ def append_type_c_header(obj, header, prefix):
else:
append_header_child_others(i, header, prefix)
if obj.children is not None:
header.append(" yajl_val _residual;\n")
header.append(" json_t *_residual;\n")
if len(present_tags) > 0:
header.append("\n")
for tag in present_tags:
Expand All @@ -173,7 +173,7 @@ def append_type_c_header(obj, header, prefix):
header.append(f"}}\n{typename};\n\n")
header.append(f"void free_{typename} ({typename} *ptr);\n\n")
header.append(f"{typename} *clone_{typename} ({typename} *src);\n")
header.append(f"{typename} *make_{typename} (yajl_val tree, const struct parser_context *ctx, parser_error *err);\n\n")
header.append(f"{typename} *make_{typename} (json_t *jtree, const struct parser_context *ctx, parser_error *err);\n\n")
header.append(f"yajl_gen_status gen_{typename} (yajl_gen g, const {typename} *ptr, const struct parser_context *ctx, parser_error *err);\n\n")

def header_reflect_top_array(obj, prefix, header):
Expand Down
124 changes: 114 additions & 10 deletions src/ocispec/json_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ gen_yajl_val (yajl_val obj, yajl_gen g, parser_error *err)
}

yajl_gen_status
gen_yajl_object_residual (yajl_val obj, yajl_gen g, parser_error *err)
gen_yajl_object_residual (json_t *j, yajl_gen g, parser_error *err)
{
size_t i;
yajl_val obj = json_to_yajl(j);
yajl_gen_status stat = yajl_gen_status_ok;

for (i = 0; i < obj->u.object.len; i++)
Expand Down Expand Up @@ -1673,18 +1674,19 @@ free_json_map_string_string (json_map_string_string *map)
define_cleaner_function (json_map_string_string *, free_json_map_string_string)

json_map_string_string *
make_json_map_string_string (yajl_val src, const struct parser_context *ctx,
make_json_map_string_string (json_t *src, const struct parser_context *ctx,
parser_error *err)
{
__auto_cleanup (free_json_map_string_string) json_map_string_string *ret = NULL;
size_t i;
size_t len;

(void) ctx; /* Silence compiler warning. */
if (src == NULL || YAJL_GET_OBJECT (src) == NULL)
if (src == NULL)
return NULL;

len = YAJL_GET_OBJECT_NO_CHECK (src)->len;

len = json_object_to_keys_values (src)->len;

ret = calloc (1, sizeof (*ret));
if (ret == NULL)
Expand All @@ -1710,8 +1712,8 @@ make_json_map_string_string (yajl_val src, const struct parser_context *ctx,
}
for (i = 0; i < len; i++)
{
const char *srckey = YAJL_GET_OBJECT_NO_CHECK (src)->keys[i];
yajl_val srcval = YAJL_GET_OBJECT_NO_CHECK (src)->values[i];
const char *srckey = json_object_to_keys_values (src)->keys[i];
json_t *srcval = &json_object_to_keys_values (src)->values[i];

ret->keys[i] = NULL;
ret->values[i] = NULL;
Expand All @@ -1734,7 +1736,7 @@ make_json_map_string_string (yajl_val src, const struct parser_context *ctx,
return NULL;
}

str = YAJL_GET_STRING_NO_CHECK (srcval);
str = json_string_value(srcval);

ret->values[i] = strdup (str ? str : "");
if (ret->values[i] == NULL)
Expand Down Expand Up @@ -1941,16 +1943,118 @@ json_t *yajl_to_json(yajl_val val) {
jansson_array_values *json_array_to_struct(json_t *array) {
if (!json_is_array(array)) {
// Handle error: Input is not an array
return (jansson_array_values){NULL, 0};
return NULL;
}

size_t len = json_array_size(array);
jansson_array_values r*esult = {json_array(), len};
jansson_array_values *result = malloc(sizeof(jansson_array_values));
if (!result) {
return NULL; // Handle allocation failure
}

result->values = json_array();
result->len = len;

if (!result->values) {
free(result);
return NULL; // Handle allocation failure
}

for (size_t i = 0; i < len; i++) {
json_t *value = json_array_get(array, i);
json_array_append_new(result.values, json_incref(value));
json_array_append_new(result->values, json_incref(value));
}

return result;
}


/**
* json_object_to_keys_values
* Input: json_t
* Output: jansson_object_keys_values *
*
* This method extracts keys and values and stores it in array of keys and values
*/
jansson_object_keys_values *json_object_to_keys_values(json_t *object) {
if (!json_is_object(object)) {
// Handle error: Input is not an object
return NULL;
}

size_t len = json_object_size(object);
jansson_object_keys_values *result = malloc(sizeof(jansson_object_keys_values));
if (!result) {
return NULL; // Handle allocation failure
}

result->keys = calloc(len, sizeof(char*));
result->values = json_array();
result->len = len;

if (!result->keys || !result->values) {
free(result->keys);
json_decref(result->values);
free(result);
return NULL; // Handle allocation failure
}

json_t *key_iter = json_object_iter(object);
for (size_t i = 0; key_iter; key_iter = json_object_iter_next(object, key_iter)) {
const char *key = json_object_iter_key(key_iter);
json_t *value = json_object_iter_value(key_iter);

result->keys[i] = strdup(key);
json_array_append_new(result->values, json_incref(value));
i++;
}

return result;
}


/**
* copy_unmatched_fields
* Input: json_t, const char **, size_t
* Ouput: jsont_t
*
* We exxtract all the fields and we match them with the supplied keys if they don't match
* we add it to new json_t
*/
json_t *copy_unmatched_fields(json_t *src, const char **exclude_keys, size_t num_keys) {
json_t *dst = json_object();
json_t *value;

json_t *key_iter = json_object_iter(src);
while (key_iter) {
const char *key = json_object_iter_key(key_iter);
value = json_object_iter_value(key_iter);

bool found = false;
for (size_t i = 0; i < num_keys; i++) {
if (strcmp(key, exclude_keys[i]) == 0) {
found = true;
break;
}
}

if (!found) {
json_object_set_new(dst, key, json_incref(value));
}

key_iter = json_object_iter_next(src, key_iter);
}

return dst;
}

/** Temporary function we will not need it once transformation is done */
yajl_val json_to_yajl(json_t *json) {
char errbuf[1024];

char *json_string = json_dumps(json, JSON_INDENT(2));
if (!json_string) {
return NULL;
}
return yajl_tree_parse((const char *)json_string, errbuf, sizeof(errbuf));
}
17 changes: 15 additions & 2 deletions src/ocispec/json_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct parser_context
FILE *errfile;
};

yajl_gen_status gen_yajl_object_residual (yajl_val obj, yajl_gen g, parser_error *err);
yajl_gen_status gen_yajl_object_residual (json_t *j, yajl_gen g, parser_error *err);

yajl_gen_status map_uint (void *ctx, long long unsigned int num);

Expand Down Expand Up @@ -245,7 +245,7 @@ void free_json_map_string_string (json_map_string_string *map);

json_map_string_string *clone_map_string_string (json_map_string_string *src);

json_map_string_string *make_json_map_string_string (yajl_val src, const struct parser_context *ctx, parser_error *err);
json_map_string_string *make_json_map_string_string (json_t *src, const struct parser_context *ctx, parser_error *err);

yajl_gen_status gen_json_map_string_string (void *ctx, const json_map_string_string *map,
const struct parser_context *ptx, parser_error *err);
Expand All @@ -264,6 +264,19 @@ typedef struct

jansson_array_values *json_array_to_struct(json_t *array);

typedef struct
{
const char **keys;
json_t * values;
size_t len;
} jansson_object_keys_values;

jansson_object_keys_values *json_object_to_keys_values(json_t *object);

json_t *copy_unmatched_fields(json_t *src, const char **exclude_keys, size_t num_keys);

yajl_val json_to_yajl(json_t *json);

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit fb861a0

Please sign in to comment.