diff --git a/src/ocispec/headers.py b/src/ocispec/headers.py index 34b04746..8252a7ab 100755 --- a/src/ocispec/headers.py +++ b/src/ocispec/headers.py @@ -174,7 +174,7 @@ def append_type_c_header(obj, header, prefix): 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} (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") + header.append(f"int gen_{typename} (json_t *root, const {typename} *ptr, parser_error *err);\n\n") def header_reflect_top_array(obj, prefix, header): c_typ = helpers.get_prefixed_pointer(obj.name, obj.subtyp, prefix) or \ @@ -206,7 +206,7 @@ def header_reflect_top_array(obj, prefix, header): header.append(f"{typename} *{typename}_parse_data(const char *jsondata, const struct "\ "parser_context *ctx, parser_error *err);\n\n") header.append(f"char *{typename}_generate_json(const {typename} *ptr, "\ - "const struct parser_context *ctx, parser_error *err);\n\n") + "parser_error *err);\n\n") def header_reflect(structs, schema_info, header): ''' @@ -239,7 +239,7 @@ def header_reflect(structs, schema_info, header): "parser_error *err);\n\n") header.append(f"{prefix} *{prefix}_parse_data (const char *jsondata, const struct parser_context *ctx, "\ "parser_error *err);\n\n") - header.append(f"char *{prefix}_generate_json (const {prefix} *ptr, const struct parser_context *ctx, "\ + header.append(f"char *{prefix}_generate_json (const {prefix} *ptr,"\ "parser_error *err);\n\n") elif toptype == 'array': header_reflect_top_array(structs[length - 1], prefix, header) diff --git a/src/ocispec/json_common.c b/src/ocispec/json_common.c index 0ab01c63..8473bf09 100644 --- a/src/ocispec/json_common.c +++ b/src/ocispec/json_common.c @@ -10,167 +10,143 @@ #define MAX_NUM_STR_LEN 21 -static yajl_gen_status gen_yajl_val (yajl_val obj, yajl_gen g, parser_error *err); - -static yajl_gen_status -gen_yajl_val_obj (yajl_val obj, yajl_gen g, parser_error *err) -{ - size_t i; - yajl_gen_status stat = yajl_gen_status_ok; - - stat = yajl_gen_map_open (g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - - for (i = 0; i < obj->u.object.len; i++) - { - stat = yajl_gen_string (g, (const unsigned char *) obj->u.object.keys[i], strlen (obj->u.object.keys[i])); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - stat = gen_yajl_val (obj->u.object.values[i], g, err); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - } - - stat = yajl_gen_map_close (g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - return yajl_gen_status_ok; -} - -static yajl_gen_status -gen_yajl_val_array (yajl_val arr, yajl_gen g, parser_error *err) -{ - size_t i; - yajl_gen_status stat = yajl_gen_status_ok; - - stat = yajl_gen_array_open (g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - - for (i = 0; i < arr->u.array.len; i++) - { - stat = gen_yajl_val (arr->u.array.values[i], g, err); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - } - - stat = yajl_gen_array_close (g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - return yajl_gen_status_ok; -} - -static yajl_gen_status -gen_yajl_val (yajl_val obj, yajl_gen g, parser_error *err) -{ - yajl_gen_status __stat = yajl_gen_status_ok; - char *__tstr; - - switch (obj->type) - { - case yajl_t_string: - __tstr = YAJL_GET_STRING (obj); - if (__tstr == NULL) - { - return __stat; - } - __stat = yajl_gen_string (g, (const unsigned char *) __tstr, strlen (__tstr)); - if (yajl_gen_status_ok != __stat) - GEN_SET_ERROR_AND_RETURN (__stat, err); - return yajl_gen_status_ok; - case yajl_t_number: - __tstr = YAJL_GET_NUMBER (obj); - if (__tstr == NULL) - { - return __stat; - } - __stat = yajl_gen_number (g, __tstr, strlen (__tstr)); - if (yajl_gen_status_ok != __stat) - GEN_SET_ERROR_AND_RETURN (__stat, err); - return yajl_gen_status_ok; - case yajl_t_object: - return gen_yajl_val_obj (obj, g, err); - case yajl_t_array: - return gen_yajl_val_array (obj, g, err); - case yajl_t_true: - return yajl_gen_bool (g, true); - case yajl_t_false: - return yajl_gen_bool (g, false); - case yajl_t_null: - return yajl_gen_null(g); - case yajl_t_any: - return __stat; - } - return __stat; -} - -yajl_gen_status -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++) - { - if (obj->u.object.keys[i] == NULL) - { - continue; - } - stat = yajl_gen_string (g, (const unsigned char *) obj->u.object.keys[i], strlen (obj->u.object.keys[i])); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - stat = gen_yajl_val (obj->u.object.values[i], g, err); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - } - - return yajl_gen_status_ok; -} - -yajl_gen_status -map_uint (void *ctx, long long unsigned int num) -{ - char numstr[MAX_NUM_STR_LEN]; - int ret; - - ret = snprintf (numstr, sizeof (numstr), "%llu", num); - if (ret < 0 || (size_t) ret >= sizeof (numstr)) - return yajl_gen_in_error_state; - return yajl_gen_number ((yajl_gen) ctx, (const char *) numstr, strlen (numstr)); -} - -yajl_gen_status -map_int (void *ctx, long long int num) -{ - char numstr[MAX_NUM_STR_LEN]; - int ret; - - ret = snprintf (numstr, sizeof (numstr), "%lld", num); - if (ret < 0 || (size_t) ret >= sizeof (numstr)) - return yajl_gen_in_error_state; - return yajl_gen_number ((yajl_gen) ctx, (const char *) numstr, strlen (numstr)); -} - -bool -json_gen_init (yajl_gen *g, const struct parser_context *ctx) -{ - *g = yajl_gen_alloc (NULL); - if (NULL == *g) - return false; - - yajl_gen_config (*g, yajl_gen_beautify, (int) (! (ctx->options & OPT_GEN_SIMPLIFY))); - yajl_gen_config (*g, yajl_gen_validate_utf8, (int) (! (ctx->options & OPT_GEN_NO_VALIDATE_UTF8))); - return true; -} - -yajl_val -get_val (yajl_val tree, const char *name, yajl_type type) -{ - const char *path[] = { name, NULL }; - return yajl_tree_get (tree, path, type); -} +// static yajl_gen_status gen_yajl_val (yajl_val obj, yajl_gen g, parser_error *err); + +// static yajl_gen_status +// gen_yajl_val_obj (yajl_val obj, yajl_gen g, parser_error *err) +// { +// size_t i; +// yajl_gen_status stat = yajl_gen_status_ok; + +// stat = yajl_gen_map_open (g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); + +// for (i = 0; i < obj->u.object.len; i++) +// { +// stat = yajl_gen_string (g, (const unsigned char *) obj->u.object.keys[i], strlen (obj->u.object.keys[i])); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// stat = gen_yajl_val (obj->u.object.values[i], g, err); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// } + +// stat = yajl_gen_map_close (g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// return yajl_gen_status_ok; +// } + +// static yajl_gen_status +// gen_yajl_val_array (yajl_val arr, yajl_gen g, parser_error *err) +// { +// size_t i; +// yajl_gen_status stat = yajl_gen_status_ok; + +// stat = yajl_gen_array_open (g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); + +// for (i = 0; i < arr->u.array.len; i++) +// { +// stat = gen_yajl_val (arr->u.array.values[i], g, err); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// } + +// stat = yajl_gen_array_close (g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// return yajl_gen_status_ok; +// } + +// static yajl_gen_status +// gen_yajl_val (yajl_val obj, yajl_gen g, parser_error *err) +// { +// yajl_gen_status __stat = yajl_gen_status_ok; +// char *__tstr; + +// switch (obj->type) +// { +// case yajl_t_string: +// __tstr = YAJL_GET_STRING (obj); +// if (__tstr == NULL) +// { +// return __stat; +// } +// __stat = yajl_gen_string (g, (const unsigned char *) __tstr, strlen (__tstr)); +// if (yajl_gen_status_ok != __stat) +// GEN_SET_ERROR_AND_RETURN (__stat, err); +// return yajl_gen_status_ok; +// case yajl_t_number: +// __tstr = YAJL_GET_NUMBER (obj); +// if (__tstr == NULL) +// { +// return __stat; +// } +// __stat = yajl_gen_number (g, __tstr, strlen (__tstr)); +// if (yajl_gen_status_ok != __stat) +// GEN_SET_ERROR_AND_RETURN (__stat, err); +// return yajl_gen_status_ok; +// case yajl_t_object: +// return gen_yajl_val_obj (obj, g, err); +// case yajl_t_array: +// return gen_yajl_val_array (obj, g, err); +// case yajl_t_true: +// return yajl_gen_bool (g, true); +// case yajl_t_false: +// return yajl_gen_bool (g, false); +// case yajl_t_null: +// return yajl_gen_null(g); +// case yajl_t_any: +// return __stat; +// } +// return __stat; +// } + +// yajl_gen_status +// map_uint (void *ctx, long long unsigned int num) +// { +// char numstr[MAX_NUM_STR_LEN]; +// int ret; + +// ret = snprintf (numstr, sizeof (numstr), "%llu", num); +// if (ret < 0 || (size_t) ret >= sizeof (numstr)) +// return yajl_gen_in_error_state; +// return yajl_gen_number ((yajl_gen) ctx, (const char *) numstr, strlen (numstr)); +// } + +// yajl_gen_status +// map_int (void *ctx, long long int num) +// { +// char numstr[MAX_NUM_STR_LEN]; +// int ret; + +// ret = snprintf (numstr, sizeof (numstr), "%lld", num); +// if (ret < 0 || (size_t) ret >= sizeof (numstr)) +// return yajl_gen_in_error_state; +// return yajl_gen_number ((yajl_gen) ctx, (const char *) numstr, strlen (numstr)); +// } + +// bool +// json_gen_init (yajl_gen *g, const struct parser_context *ctx) +// { +// *g = yajl_gen_alloc (NULL); +// if (NULL == *g) +// return false; + +// yajl_gen_config (*g, yajl_gen_beautify, (int) (! (ctx->options & OPT_GEN_SIMPLIFY))); +// yajl_gen_config (*g, yajl_gen_validate_utf8, (int) (! (ctx->options & OPT_GEN_NO_VALIDATE_UTF8))); +// return true; +// } + +// yajl_val +// get_val (yajl_val tree, const char *name, yajl_type type) +// { +// const char *path[] = { name, NULL }; +// return yajl_tree_get (tree, path, type); +// } char * safe_strdup (const char *src) @@ -636,45 +612,45 @@ json_double_to_double (double d, double *converted) } -yajl_gen_status -gen_json_map_int_int (void *ctx, const json_map_int_int *map, const struct parser_context *ptx, parser_error *err) -{ - yajl_gen_status stat = yajl_gen_status_ok; - yajl_gen g = (yajl_gen) ctx; - size_t len = 0, i = 0; - if (map != NULL) - len = map->len; - if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 0); - stat = yajl_gen_map_open ((yajl_gen) g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - for (i = 0; i < len; i++) - { - char numstr[MAX_NUM_STR_LEN]; - int nret; - nret = snprintf (numstr, sizeof (numstr), "%lld", (long long int) map->keys[i]); - if (nret < 0 || (size_t) nret >= sizeof (numstr)) - { - if (! *err) - *err = strdup ("Error to print string"); - return yajl_gen_in_error_state; - } - stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) numstr, strlen (numstr)); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - stat = map_int (g, map->values[i]); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - } - - stat = yajl_gen_map_close ((yajl_gen) g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 1); - return yajl_gen_status_ok; -} +// yajl_gen_status +// gen_json_map_int_int (void *ctx, const json_map_int_int *map, const struct parser_context *ptx, parser_error *err) +// { +// yajl_gen_status stat = yajl_gen_status_ok; +// yajl_gen g = (yajl_gen) ctx; +// size_t len = 0, i = 0; +// if (map != NULL) +// len = map->len; +// if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) +// yajl_gen_config (g, yajl_gen_beautify, 0); +// stat = yajl_gen_map_open ((yajl_gen) g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// for (i = 0; i < len; i++) +// { +// char numstr[MAX_NUM_STR_LEN]; +// int nret; +// nret = snprintf (numstr, sizeof (numstr), "%lld", (long long int) map->keys[i]); +// if (nret < 0 || (size_t) nret >= sizeof (numstr)) +// { +// if (! *err) +// *err = strdup ("Error to print string"); +// return yajl_gen_in_error_state; +// } +// stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) numstr, strlen (numstr)); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// stat = map_int (g, map->values[i]); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// } + +// stat = yajl_gen_map_close ((yajl_gen) g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) +// yajl_gen_config (g, yajl_gen_beautify, 1); +// return yajl_gen_status_ok; +// } void free_json_map_int_int (json_map_int_int *map) @@ -691,84 +667,84 @@ free_json_map_int_int (json_map_int_int *map) define_cleaner_function (json_map_int_int *, free_json_map_int_int) -json_map_int_int * -make_json_map_int_int (yajl_val src, const struct parser_context *ctx, parser_error *err) -{ - __auto_cleanup (free_json_map_int_int) json_map_int_int *ret = NULL; - size_t i; - size_t len; - - (void) ctx; /* Silence compiler warning. */ - - if (src == NULL || YAJL_GET_OBJECT (src) == NULL) - return NULL; - - len = YAJL_GET_OBJECT_NO_CHECK (src)->len; - ret = calloc (1, sizeof (*ret)); - if (ret == NULL) - return NULL; - - ret->len = 0; - ret->keys = calloc (len + 1, sizeof (int)); - if (ret->keys == NULL) - { - return NULL; - } - - ret->values = calloc (len + 1, sizeof (int)); - if (ret->values == NULL) - { - return NULL; - } - - 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]; - - ret->keys[i] = 0; - ret->values[i] = 0; - ret->len = i + 1; - - if (srckey != NULL) - { - int invalid = common_safe_int (srckey, &(ret->keys[i])); - if (invalid) - { - if (*err == NULL - && asprintf (err, "Invalid key '%s' with type 'int': %s", srckey, strerror (-invalid)) < 0) - { - *err = strdup ("error allocating memory"); - } - return NULL; - } - } - - if (srcval != NULL) - { - int invalid; - if (! YAJL_IS_NUMBER (srcval)) - { - if (*err == NULL && asprintf (err, "Invalid value with type 'int' for key '%s'", srckey) < 0) - { - *err = strdup ("error allocating memory"); - } - return NULL; - } - invalid = common_safe_int (YAJL_GET_NUMBER (srcval), &(ret->values[i])); - if (invalid) - { - if (*err == NULL - && asprintf (err, "Invalid value with type 'int' for key '%s': %s", srckey, strerror (-invalid)) < 0) - { - *err = strdup ("error allocating memory"); - } - return NULL; - } - } - } - return move_ptr (ret); -} +// json_map_int_int * +// make_json_map_int_int (yajl_val src, const struct parser_context *ctx, parser_error *err) +// { +// __auto_cleanup (free_json_map_int_int) json_map_int_int *ret = NULL; +// size_t i; +// size_t len; + +// (void) ctx; /* Silence compiler warning. */ + +// if (src == NULL || YAJL_GET_OBJECT (src) == NULL) +// return NULL; + +// len = YAJL_GET_OBJECT_NO_CHECK (src)->len; +// ret = calloc (1, sizeof (*ret)); +// if (ret == NULL) +// return NULL; + +// ret->len = 0; +// ret->keys = calloc (len + 1, sizeof (int)); +// if (ret->keys == NULL) +// { +// return NULL; +// } + +// ret->values = calloc (len + 1, sizeof (int)); +// if (ret->values == NULL) +// { +// return NULL; +// } + +// 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]; + +// ret->keys[i] = 0; +// ret->values[i] = 0; +// ret->len = i + 1; + +// if (srckey != NULL) +// { +// int invalid = common_safe_int (srckey, &(ret->keys[i])); +// if (invalid) +// { +// if (*err == NULL +// && asprintf (err, "Invalid key '%s' with type 'int': %s", srckey, strerror (-invalid)) < 0) +// { +// *err = strdup ("error allocating memory"); +// } +// return NULL; +// } +// } + +// if (srcval != NULL) +// { +// int invalid; +// if (! YAJL_IS_NUMBER (srcval)) +// { +// if (*err == NULL && asprintf (err, "Invalid value with type 'int' for key '%s'", srckey) < 0) +// { +// *err = strdup ("error allocating memory"); +// } +// return NULL; +// } +// invalid = common_safe_int (YAJL_GET_NUMBER (srcval), &(ret->values[i])); +// if (invalid) +// { +// if (*err == NULL +// && asprintf (err, "Invalid value with type 'int' for key '%s': %s", srckey, strerror (-invalid)) < 0) +// { +// *err = strdup ("error allocating memory"); +// } +// return NULL; +// } +// } +// } +// return move_ptr (ret); +// } int append_json_map_int_int (json_map_int_int *map, int key, int val) @@ -811,45 +787,45 @@ append_json_map_int_int (json_map_int_int *map, int key, int val) return 0; } -yajl_gen_status -gen_json_map_int_bool (void *ctx, const json_map_int_bool *map, const struct parser_context *ptx, parser_error *err) -{ - yajl_gen_status stat = yajl_gen_status_ok; - yajl_gen g = (yajl_gen) ctx; - size_t len = 0, i = 0; - if (map != NULL) - len = map->len; - if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 0); - stat = yajl_gen_map_open ((yajl_gen) g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - for (i = 0; i < len; i++) - { - char numstr[MAX_NUM_STR_LEN]; - int nret; - nret = snprintf (numstr, sizeof (numstr), "%lld", (long long int) map->keys[i]); - if (nret < 0 || (size_t) nret >= sizeof (numstr)) - { - if (! *err) - *err = strdup ("Error to print string"); - return yajl_gen_in_error_state; - } - stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) numstr, strlen (numstr)); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - stat = yajl_gen_bool ((yajl_gen) g, (int) (map->values[i])); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - } - - stat = yajl_gen_map_close ((yajl_gen) g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 1); - return yajl_gen_status_ok; -} +// yajl_gen_status +// gen_json_map_int_bool (void *ctx, const json_map_int_bool *map, const struct parser_context *ptx, parser_error *err) +// { +// yajl_gen_status stat = yajl_gen_status_ok; +// yajl_gen g = (yajl_gen) ctx; +// size_t len = 0, i = 0; +// if (map != NULL) +// len = map->len; +// if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) +// yajl_gen_config (g, yajl_gen_beautify, 0); +// stat = yajl_gen_map_open ((yajl_gen) g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// for (i = 0; i < len; i++) +// { +// char numstr[MAX_NUM_STR_LEN]; +// int nret; +// nret = snprintf (numstr, sizeof (numstr), "%lld", (long long int) map->keys[i]); +// if (nret < 0 || (size_t) nret >= sizeof (numstr)) +// { +// if (! *err) +// *err = strdup ("Error to print string"); +// return yajl_gen_in_error_state; +// } +// stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) numstr, strlen (numstr)); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// stat = yajl_gen_bool ((yajl_gen) g, (int) (map->values[i])); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// } + +// stat = yajl_gen_map_close ((yajl_gen) g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) +// yajl_gen_config (g, yajl_gen_beautify, 1); +// return yajl_gen_status_ok; +// } void free_json_map_int_bool (json_map_int_bool *map) @@ -872,74 +848,74 @@ free_json_map_int_bool (json_map_int_bool *map) define_cleaner_function (json_map_int_bool *, free_json_map_int_bool) -json_map_int_bool * -make_json_map_int_bool (yajl_val src, const struct parser_context *ctx, parser_error *err) -{ - __auto_cleanup (free_json_map_int_bool) json_map_int_bool *ret = NULL; - size_t i; - size_t len; - - (void) ctx; /* Silence compiler warning. */ - - if (src == NULL || YAJL_GET_OBJECT (src) == NULL) - return NULL; - - len = YAJL_GET_OBJECT_NO_CHECK (src)->len; - ret = calloc (1, sizeof (*ret)); - if (ret == NULL) - return NULL; - ret->len = 0; - ret->keys = calloc (len + 1, sizeof (int)); - if (ret->keys == NULL) - { - return NULL; - } - ret->values = calloc (len + 1, sizeof (bool)); - if (ret->values == NULL) - { - return NULL; - } - 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]; - - ret->keys[i] = 0; - ret->values[i] = false; - ret->len = i + 1; - - if (srckey != NULL) - { - int invalid = common_safe_int (srckey, &(ret->keys[i])); - if (invalid) - { - if (*err == NULL - && asprintf (err, "Invalid key '%s' with type 'int': %s", srckey, strerror (-invalid)) < 0) - { - *err = strdup ("error allocating memory"); - } - return NULL; - } - } - - if (srcval != NULL) - { - if (YAJL_IS_TRUE (srcval)) - ret->values[i] = true; - else if (YAJL_IS_FALSE (srcval)) - ret->values[i] = false; - else - { - if (*err == NULL && asprintf (err, "Invalid value with type 'bool' for key '%s'", srckey) < 0) - { - *err = strdup ("error allocating memory"); - } - return NULL; - } - } - } - return move_ptr (ret); -} +// json_map_int_bool * +// make_json_map_int_bool (yajl_val src, const struct parser_context *ctx, parser_error *err) +// { +// __auto_cleanup (free_json_map_int_bool) json_map_int_bool *ret = NULL; +// size_t i; +// size_t len; + +// (void) ctx; /* Silence compiler warning. */ + +// if (src == NULL || YAJL_GET_OBJECT (src) == NULL) +// return NULL; + +// len = YAJL_GET_OBJECT_NO_CHECK (src)->len; +// ret = calloc (1, sizeof (*ret)); +// if (ret == NULL) +// return NULL; +// ret->len = 0; +// ret->keys = calloc (len + 1, sizeof (int)); +// if (ret->keys == NULL) +// { +// return NULL; +// } +// ret->values = calloc (len + 1, sizeof (bool)); +// if (ret->values == NULL) +// { +// return NULL; +// } +// 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]; + +// ret->keys[i] = 0; +// ret->values[i] = false; +// ret->len = i + 1; + +// if (srckey != NULL) +// { +// int invalid = common_safe_int (srckey, &(ret->keys[i])); +// if (invalid) +// { +// if (*err == NULL +// && asprintf (err, "Invalid key '%s' with type 'int': %s", srckey, strerror (-invalid)) < 0) +// { +// *err = strdup ("error allocating memory"); +// } +// return NULL; +// } +// } + +// if (srcval != NULL) +// { +// if (YAJL_IS_TRUE (srcval)) +// ret->values[i] = true; +// else if (YAJL_IS_FALSE (srcval)) +// ret->values[i] = false; +// else +// { +// if (*err == NULL && asprintf (err, "Invalid value with type 'bool' for key '%s'", srckey) < 0) +// { +// *err = strdup ("error allocating memory"); +// } +// return NULL; +// } +// } +// } +// return move_ptr (ret); +// } int append_json_map_int_bool (json_map_int_bool *map, int key, bool val) @@ -982,46 +958,46 @@ append_json_map_int_bool (json_map_int_bool *map, int key, bool val) return 0; } -yajl_gen_status -gen_json_map_int_string (void *ctx, const json_map_int_string *map, const struct parser_context *ptx, parser_error *err) -{ - yajl_gen_status stat = yajl_gen_status_ok; - yajl_gen g = (yajl_gen) ctx; - size_t len = 0, i = 0; - if (map != NULL) - len = map->len; - if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 0); - - stat = yajl_gen_map_open ((yajl_gen) g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - for (i = 0; i < len; i++) - { - char numstr[MAX_NUM_STR_LEN]; - int nret; - nret = snprintf (numstr, sizeof (numstr), "%lld", (long long int) map->keys[i]); - if (nret < 0 || (size_t) nret >= sizeof (numstr)) - { - if (! *err) - *err = strdup ("Error to print string"); - return yajl_gen_in_error_state; - } - stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) numstr, strlen (numstr)); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) (map->values[i]), strlen (map->values[i])); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - } - - stat = yajl_gen_map_close ((yajl_gen) g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 1); - return yajl_gen_status_ok; -} +// yajl_gen_status +// gen_json_map_int_string (void *ctx, const json_map_int_string *map, const struct parser_context *ptx, parser_error *err) +// { +// yajl_gen_status stat = yajl_gen_status_ok; +// yajl_gen g = (yajl_gen) ctx; +// size_t len = 0, i = 0; +// if (map != NULL) +// len = map->len; +// if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) +// yajl_gen_config (g, yajl_gen_beautify, 0); + +// stat = yajl_gen_map_open ((yajl_gen) g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// for (i = 0; i < len; i++) +// { +// char numstr[MAX_NUM_STR_LEN]; +// int nret; +// nret = snprintf (numstr, sizeof (numstr), "%lld", (long long int) map->keys[i]); +// if (nret < 0 || (size_t) nret >= sizeof (numstr)) +// { +// if (! *err) +// *err = strdup ("Error to print string"); +// return yajl_gen_in_error_state; +// } +// stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) numstr, strlen (numstr)); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) (map->values[i]), strlen (map->values[i])); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// } + +// stat = yajl_gen_map_close ((yajl_gen) g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) +// yajl_gen_config (g, yajl_gen_beautify, 1); +// return yajl_gen_status_ok; +// } void free_json_map_int_string (json_map_int_string *map) @@ -1045,77 +1021,77 @@ free_json_map_int_string (json_map_int_string *map) define_cleaner_function (json_map_int_string *, free_json_map_int_string) -json_map_int_string * -make_json_map_int_string (yajl_val src, const struct parser_context *ctx, parser_error *err) -{ - __auto_cleanup (free_json_map_int_string) json_map_int_string *ret = NULL; - size_t i; - size_t len; - - if (src == NULL || YAJL_GET_OBJECT (src) == NULL) - return NULL; - - (void) ctx; /* Silence compiler warning. */ - - len = YAJL_GET_OBJECT_NO_CHECK (src)->len; - - ret = calloc (1, sizeof (*ret)); - if (ret == NULL) - return NULL; - - ret->len = 0; - ret->keys = calloc (len + 1, sizeof (int)); - if (ret->keys == NULL) - { - return NULL; - } - - ret->values = calloc (len + 1, sizeof (char *)); - if (ret->values == NULL) - { - return NULL; - } - - 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]; - - ret->keys[i] = 0; - ret->values[i] = NULL; - ret->len = i + 1; - - if (srckey != NULL) - { - int invalid; - invalid = common_safe_int (srckey, &(ret->keys[i])); - if (invalid) - { - if (*err == NULL - && asprintf (err, "Invalid key '%s' with type 'int': %s", srckey, strerror (-invalid)) < 0) - { - *err = strdup ("error allocating memory"); - } - return NULL; - } - } - - if (srcval != NULL) - { - if (! YAJL_IS_STRING (srcval)) - { - if (*err == NULL && asprintf (err, "Invalid value with type 'string' for key '%s'", srckey) < 0) - { - *err = strdup ("error allocating memory"); - } - return NULL; - } - char *str = YAJL_GET_STRING_NO_CHECK (srcval); - ret->values[i] = strdup (str ? str : ""); - } - } - return move_ptr (ret); -} +// json_map_int_string * +// make_json_map_int_string (yajl_val src, const struct parser_context *ctx, parser_error *err) +// { +// __auto_cleanup (free_json_map_int_string) json_map_int_string *ret = NULL; +// size_t i; +// size_t len; + +// if (src == NULL || YAJL_GET_OBJECT (src) == NULL) +// return NULL; + +// (void) ctx; /* Silence compiler warning. */ + +// len = YAJL_GET_OBJECT_NO_CHECK (src)->len; + +// ret = calloc (1, sizeof (*ret)); +// if (ret == NULL) +// return NULL; + +// ret->len = 0; +// ret->keys = calloc (len + 1, sizeof (int)); +// if (ret->keys == NULL) +// { +// return NULL; +// } + +// ret->values = calloc (len + 1, sizeof (char *)); +// if (ret->values == NULL) +// { +// return NULL; +// } + +// 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]; + +// ret->keys[i] = 0; +// ret->values[i] = NULL; +// ret->len = i + 1; + +// if (srckey != NULL) +// { +// int invalid; +// invalid = common_safe_int (srckey, &(ret->keys[i])); +// if (invalid) +// { +// if (*err == NULL +// && asprintf (err, "Invalid key '%s' with type 'int': %s", srckey, strerror (-invalid)) < 0) +// { +// *err = strdup ("error allocating memory"); +// } +// return NULL; +// } +// } + +// if (srcval != NULL) +// { +// if (! YAJL_IS_STRING (srcval)) +// { +// if (*err == NULL && asprintf (err, "Invalid value with type 'string' for key '%s'", srckey) < 0) +// { +// *err = strdup ("error allocating memory"); +// } +// return NULL; +// } +// char *str = YAJL_GET_STRING_NO_CHECK (srcval); +// ret->values[i] = strdup (str ? str : ""); +// } +// } +// return move_ptr (ret); +// } int append_json_map_int_string (json_map_int_string *map, int key, const char *val) @@ -1153,36 +1129,36 @@ append_json_map_int_string (json_map_int_string *map, int key, const char *val) return 0; } -yajl_gen_status -gen_json_map_string_int (void *ctx, const json_map_string_int *map, const struct parser_context *ptx, parser_error *err) -{ - yajl_gen_status stat = yajl_gen_status_ok; - yajl_gen g = (yajl_gen) ctx; - size_t len = 0, i = 0; - if (map != NULL) - len = map->len; - if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 0); - stat = yajl_gen_map_open ((yajl_gen) g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - for (i = 0; i < len; i++) - { - stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) (map->keys[i]), strlen (map->keys[i])); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - stat = map_int (g, map->values[i]); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - } - - stat = yajl_gen_map_close ((yajl_gen) g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 1); - return yajl_gen_status_ok; -} +// yajl_gen_status +// gen_json_map_string_int (void *ctx, const json_map_string_int *map, const struct parser_context *ptx, parser_error *err) +// { +// yajl_gen_status stat = yajl_gen_status_ok; +// yajl_gen g = (yajl_gen) ctx; +// size_t len = 0, i = 0; +// if (map != NULL) +// len = map->len; +// if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) +// yajl_gen_config (g, yajl_gen_beautify, 0); +// stat = yajl_gen_map_open ((yajl_gen) g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// for (i = 0; i < len; i++) +// { +// stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) (map->keys[i]), strlen (map->keys[i])); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// stat = map_int (g, map->values[i]); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// } + +// stat = yajl_gen_map_close ((yajl_gen) g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) +// yajl_gen_config (g, yajl_gen_beautify, 1); +// return yajl_gen_status_ok; +// } void free_json_map_string_int (json_map_string_int *map) @@ -1205,79 +1181,79 @@ free_json_map_string_int (json_map_string_int *map) define_cleaner_function (json_map_string_int *, free_json_map_string_int) -json_map_string_int * -make_json_map_string_int (yajl_val src, const struct parser_context *ctx, parser_error *err) -{ - __auto_cleanup (free_json_map_string_int) json_map_string_int *ret = NULL; - size_t i; - size_t len; - - (void) ctx; /* Silence compiler warning. */ - - if (src == NULL || YAJL_GET_OBJECT (src) == NULL) - return NULL; - - len = YAJL_GET_OBJECT_NO_CHECK (src)->len; - ret = calloc (1, sizeof (*ret)); - if (ret == NULL) - { - *(err) = strdup ("error allocating memory"); - return NULL; - } - ret->len = 0; - ret->keys = calloc (len + 1, sizeof (char *)); - if (ret->keys == NULL) - { - *(err) = strdup ("error allocating memory"); - return NULL; - } - ret->values = calloc (len + 1, sizeof (int)); - if (ret->values == NULL) - { - *(err) = strdup ("error allocating memory"); - return NULL; - } - 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]; - - ret->keys[i] = NULL; - ret->values[i] = 0; - ret->len = i + 1; - - ret->keys[i] = strdup (srckey ? srckey : ""); - if (ret->keys[i] == NULL) - { - *(err) = strdup ("error allocating memory"); - return NULL; - } - - if (srcval != NULL) - { - int invalid; - if (! YAJL_IS_NUMBER (srcval)) - { - if (*err == NULL && asprintf (err, "Invalid value with type 'int' for key '%s'", srckey) < 0) - { - *err = strdup ("error allocating memory"); - } - return NULL; - } - invalid = common_safe_int (YAJL_GET_NUMBER (srcval), &(ret->values[i])); - if (invalid) - { - if (*err == NULL - && asprintf (err, "Invalid value with type 'int' for key '%s': %s", srckey, strerror (-invalid)) < 0) - { - *err = strdup ("error allocating memory"); - } - return NULL; - } - } - } - return move_ptr (ret); -} +// json_map_string_int * +// make_json_map_string_int (yajl_val src, const struct parser_context *ctx, parser_error *err) +// { +// __auto_cleanup (free_json_map_string_int) json_map_string_int *ret = NULL; +// size_t i; +// size_t len; + +// (void) ctx; /* Silence compiler warning. */ + +// if (src == NULL || YAJL_GET_OBJECT (src) == NULL) +// return NULL; + +// len = YAJL_GET_OBJECT_NO_CHECK (src)->len; +// ret = calloc (1, sizeof (*ret)); +// if (ret == NULL) +// { +// *(err) = strdup ("error allocating memory"); +// return NULL; +// } +// ret->len = 0; +// ret->keys = calloc (len + 1, sizeof (char *)); +// if (ret->keys == NULL) +// { +// *(err) = strdup ("error allocating memory"); +// return NULL; +// } +// ret->values = calloc (len + 1, sizeof (int)); +// if (ret->values == NULL) +// { +// *(err) = strdup ("error allocating memory"); +// return NULL; +// } +// 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]; + +// ret->keys[i] = NULL; +// ret->values[i] = 0; +// ret->len = i + 1; + +// ret->keys[i] = strdup (srckey ? srckey : ""); +// if (ret->keys[i] == NULL) +// { +// *(err) = strdup ("error allocating memory"); +// return NULL; +// } + +// if (srcval != NULL) +// { +// int invalid; +// if (! YAJL_IS_NUMBER (srcval)) +// { +// if (*err == NULL && asprintf (err, "Invalid value with type 'int' for key '%s'", srckey) < 0) +// { +// *err = strdup ("error allocating memory"); +// } +// return NULL; +// } +// invalid = common_safe_int (YAJL_GET_NUMBER (srcval), &(ret->values[i])); +// if (invalid) +// { +// if (*err == NULL +// && asprintf (err, "Invalid value with type 'int' for key '%s': %s", srckey, strerror (-invalid)) < 0) +// { +// *err = strdup ("error allocating memory"); +// } +// return NULL; +// } +// } +// } +// return move_ptr (ret); +// } int append_json_map_string_int (json_map_string_int *map, const char *key, int val) @@ -1313,38 +1289,38 @@ append_json_map_string_int (json_map_string_int *map, const char *key, int val) return 0; } -yajl_gen_status -gen_json_map_string_int64 (void *ctx, const json_map_string_int64 *map, const struct parser_context *ptx, - parser_error *err) -{ - yajl_gen_status stat = yajl_gen_status_ok; - yajl_gen g = (yajl_gen) ctx; - size_t len = 0, i = 0; - if (map != NULL) - len = map->len; - if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 0); - stat = yajl_gen_map_open ((yajl_gen) g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - - for (i = 0; i < len; i++) - { - stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) (map->keys[i]), strlen (map->keys[i])); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - stat = map_int (g, map->values[i]); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - } - - stat = yajl_gen_map_close ((yajl_gen) g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 1); - return yajl_gen_status_ok; -} +// yajl_gen_status +// gen_json_map_string_int64 (void *ctx, const json_map_string_int64 *map, const struct parser_context *ptx, +// parser_error *err) +// { +// yajl_gen_status stat = yajl_gen_status_ok; +// yajl_gen g = (yajl_gen) ctx; +// size_t len = 0, i = 0; +// if (map != NULL) +// len = map->len; +// if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) +// yajl_gen_config (g, yajl_gen_beautify, 0); +// stat = yajl_gen_map_open ((yajl_gen) g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); + +// for (i = 0; i < len; i++) +// { +// stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) (map->keys[i]), strlen (map->keys[i])); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// stat = map_int (g, map->values[i]); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// } + +// stat = yajl_gen_map_close ((yajl_gen) g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) +// yajl_gen_config (g, yajl_gen_beautify, 1); +// return yajl_gen_status_ok; +// } void free_json_map_string_int64 (json_map_string_int64 *map) @@ -1367,55 +1343,56 @@ free_json_map_string_int64 (json_map_string_int64 *map) define_cleaner_function (json_map_string_int64 *, free_json_map_string_int64) -json_map_string_int64 * -make_json_map_string_int64 (yajl_val src, const struct parser_context *ctx, - parser_error *err) -{ - __auto_cleanup (free_json_map_string_int64) json_map_string_int64 *ret = NULL; - - (void) ctx; /* Silence compiler warning. */ - - if (src != NULL && YAJL_GET_OBJECT (src) != NULL) - { - size_t i; - size_t len = YAJL_GET_OBJECT (src)->len; - ret = safe_malloc (sizeof (*ret)); - ret->len = len; - ret->keys = safe_malloc ((len + 1) * sizeof (char *)); - ret->values = safe_malloc ((len + 1) * sizeof (int64_t)); - for (i = 0; i < len; i++) - { - const char *srckey = YAJL_GET_OBJECT (src)->keys[i]; - yajl_val srcval = YAJL_GET_OBJECT (src)->values[i]; - ret->keys[i] = safe_strdup (srckey ? srckey : ""); +// json_map_string_int64 * +// make_json_map_string_int64 (yajl_val src, const struct parser_context *ctx, +// parser_error *err) +// { +// __auto_cleanup (free_json_map_string_int64) json_map_string_int64 *ret = NULL; + +// (void) ctx; /* Silence compiler warning. */ + +// if (src != NULL && YAJL_GET_OBJECT (src) != NULL) +// { +// size_t i; +// size_t len = YAJL_GET_OBJECT (src)->len; +// ret = safe_malloc (sizeof (*ret)); +// ret->len = len; +// ret->keys = safe_malloc ((len + 1) * sizeof (char *)); +// ret->values = safe_malloc ((len + 1) * sizeof (int64_t)); +// for (i = 0; i < len; i++) +// { +// const char *srckey = YAJL_GET_OBJECT (src)->keys[i]; +// yajl_val srcval = YAJL_GET_OBJECT (src)->values[i]; +// ret->keys[i] = safe_strdup (srckey ? srckey : ""); + +// if (srcval != NULL) +// { +// int64_t invalid; +// if (! YAJL_IS_NUMBER (srcval)) +// { +// if (*err == NULL && asprintf (err, "Invalid value with type 'int' for key '%s'", srckey) < 0) +// { +// *(err) = safe_strdup ("error allocating memory"); +// } +// return NULL; +// } +// invalid = common_safe_int64 (YAJL_GET_NUMBER (srcval), &(ret->values[i])); +// if (invalid) +// { +// if (*err == NULL +// && asprintf (err, "Invalid value with type 'int' for key '%s': %s", srckey, strerror (-invalid)) +// < 0) +// { +// *(err) = safe_strdup ("error allocating memory"); +// } +// return NULL; +// } +// } +// } +// } +// return move_ptr (ret); +// } - if (srcval != NULL) - { - int64_t invalid; - if (! YAJL_IS_NUMBER (srcval)) - { - if (*err == NULL && asprintf (err, "Invalid value with type 'int' for key '%s'", srckey) < 0) - { - *(err) = safe_strdup ("error allocating memory"); - } - return NULL; - } - invalid = common_safe_int64 (YAJL_GET_NUMBER (srcval), &(ret->values[i])); - if (invalid) - { - if (*err == NULL - && asprintf (err, "Invalid value with type 'int' for key '%s': %s", srckey, strerror (-invalid)) - < 0) - { - *(err) = safe_strdup ("error allocating memory"); - } - return NULL; - } - } - } - } - return move_ptr (ret); -} int append_json_map_string_int64 (json_map_string_int64 *map, const char *key, int64_t val) { @@ -1449,37 +1426,37 @@ append_json_map_string_int64 (json_map_string_int64 *map, const char *key, int64 return 0; } -yajl_gen_status -gen_json_map_string_bool (void *ctx, const json_map_string_bool *map, const struct parser_context *ptx, - parser_error *err) -{ - yajl_gen_status stat = yajl_gen_status_ok; - yajl_gen g = (yajl_gen) ctx; - size_t len = 0, i = 0; - if (map != NULL) - len = map->len; - if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 0); - stat = yajl_gen_map_open ((yajl_gen) g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - for (i = 0; i < len; i++) - { - stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) (map->keys[i]), strlen (map->keys[i])); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - stat = yajl_gen_bool ((yajl_gen) g, (int) (map->values[i])); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - } - - stat = yajl_gen_map_close ((yajl_gen) g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 1); - return yajl_gen_status_ok; -} +// yajl_gen_status +// gen_json_map_string_bool (void *ctx, const json_map_string_bool *map, const struct parser_context *ptx, +// parser_error *err) +// { +// yajl_gen_status stat = yajl_gen_status_ok; +// yajl_gen g = (yajl_gen) ctx; +// size_t len = 0, i = 0; +// if (map != NULL) +// len = map->len; +// if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) +// yajl_gen_config (g, yajl_gen_beautify, 0); +// stat = yajl_gen_map_open ((yajl_gen) g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// for (i = 0; i < len; i++) +// { +// stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) (map->keys[i]), strlen (map->keys[i])); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// stat = yajl_gen_bool ((yajl_gen) g, (int) (map->values[i])); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// } + +// stat = yajl_gen_map_close ((yajl_gen) g); +// if (yajl_gen_status_ok != stat) +// GEN_SET_ERROR_AND_RETURN (stat, err); +// if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) +// yajl_gen_config (g, yajl_gen_beautify, 1); +// return yajl_gen_status_ok; +// } void free_json_map_string_bool (json_map_string_bool *map) @@ -1503,68 +1480,68 @@ free_json_map_string_bool (json_map_string_bool *map) define_cleaner_function (json_map_string_bool *, free_json_map_string_bool) -json_map_string_bool * -make_json_map_string_bool (yajl_val src, const struct parser_context *ctx, parser_error *err) -{ - __auto_cleanup (free_json_map_string_bool) json_map_string_bool *ret = NULL; - size_t i; - size_t len; - - (void) ctx; /* Silence compiler warning. */ - - len = YAJL_GET_OBJECT_NO_CHECK (src)->len; - - if (src == NULL || YAJL_GET_OBJECT (src) == NULL) - return NULL; - - ret = calloc (1, sizeof (*ret)); - if (ret == NULL) - return NULL; - ret->len = 0; - ret->keys = calloc (len + 1, sizeof (char *)); - if (ret->keys == NULL) - { - return NULL; - } - - ret->values = calloc (len + 1, sizeof (bool)); - if (ret->values == NULL) - { - return NULL; - } - 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]; - - ret->keys[i] = NULL; - ret->values[i] = NULL; - ret->len = i + 1; - - ret->keys[i] = strdup (srckey ? srckey : ""); - if (ret->keys[i] == NULL) - { - *(err) = strdup ("error allocating memory"); - return NULL; - } - if (srcval != NULL) - { - if (YAJL_IS_TRUE (srcval)) - ret->values[i] = true; - else if (YAJL_IS_FALSE (srcval)) - ret->values[i] = false; - else - { - if (*err == NULL && asprintf (err, "Invalid value with type 'bool' for key '%s'", srckey) < 0) - { - *err = strdup ("error allocating memory"); - } - return NULL; - } - } - } - return move_ptr (ret); -} +// json_map_string_bool * +// make_json_map_string_bool (yajl_val src, const struct parser_context *ctx, parser_error *err) +// { +// __auto_cleanup (free_json_map_string_bool) json_map_string_bool *ret = NULL; +// size_t i; +// size_t len; + +// (void) ctx; /* Silence compiler warning. */ + +// len = YAJL_GET_OBJECT_NO_CHECK (src)->len; + +// if (src == NULL || YAJL_GET_OBJECT (src) == NULL) +// return NULL; + +// ret = calloc (1, sizeof (*ret)); +// if (ret == NULL) +// return NULL; +// ret->len = 0; +// ret->keys = calloc (len + 1, sizeof (char *)); +// if (ret->keys == NULL) +// { +// return NULL; +// } + +// ret->values = calloc (len + 1, sizeof (bool)); +// if (ret->values == NULL) +// { +// return NULL; +// } +// 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]; + +// ret->keys[i] = NULL; +// ret->values[i] = NULL; +// ret->len = i + 1; + +// ret->keys[i] = strdup (srckey ? srckey : ""); +// if (ret->keys[i] == NULL) +// { +// *(err) = strdup ("error allocating memory"); +// return NULL; +// } +// if (srcval != NULL) +// { +// if (YAJL_IS_TRUE (srcval)) +// ret->values[i] = true; +// else if (YAJL_IS_FALSE (srcval)) +// ret->values[i] = false; +// else +// { +// if (*err == NULL && asprintf (err, "Invalid value with type 'bool' for key '%s'", srckey) < 0) +// { +// *err = strdup ("error allocating memory"); +// } +// return NULL; +// } +// } +// } +// return move_ptr (ret); +// } int append_json_map_string_bool (json_map_string_bool *map, const char *key, bool val) @@ -1615,39 +1592,21 @@ append_json_map_string_bool (json_map_string_bool *map, const char *key, bool va return 0; } -yajl_gen_status -gen_json_map_string_string (void *ctx, const json_map_string_string *map, const struct parser_context *ptx, - parser_error *err) +int +gen_json_map_string_string (json_t *root, const json_map_string_string *map, parser_error *err) { - yajl_gen_status stat = yajl_gen_status_ok; - yajl_gen g = (yajl_gen) ctx; + int stat = JSON_GEN_SUCCESS; size_t len = 0, i = 0; if (map != NULL) len = map->len; - - if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 0); - - stat = yajl_gen_map_open ((yajl_gen) g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - + for (i = 0; i < len; i++) { - stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) (map->keys[i]), strlen (map->keys[i])); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) (map->values[i]), strlen (map->values[i])); - if (yajl_gen_status_ok != stat) + stat = json_object_set(root, (const char *)(map->keys[i]), json_string((const char *)(map->values[i]))); + if (JSON_GEN_SUCCESS != stat) GEN_SET_ERROR_AND_RETURN (stat, err); } - - stat = yajl_gen_map_close ((yajl_gen) g); - if (yajl_gen_status_ok != stat) - GEN_SET_ERROR_AND_RETURN (stat, err); - if (! len && ! (ptx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 1); - return yajl_gen_status_ok; + return JSON_GEN_SUCCESS; } void @@ -1713,7 +1672,7 @@ make_json_map_string_string (json_t *src, const struct parser_context *ctx, for (i = 0; i < len; i++) { const char *srckey = json_object_to_keys_values (src)->keys[i]; - json_t *srcval = &json_object_to_keys_values (src)->values[i]; + const json_t *srcval = &json_object_to_keys_values (src)->values[i]; ret->keys[i] = NULL; ret->values[i] = NULL; @@ -1727,7 +1686,7 @@ make_json_map_string_string (json_t *src, const struct parser_context *ctx, if (srcval != NULL) { char *str; - if (! YAJL_IS_STRING (srcval)) + if (! json_is_string (srcval)) { if (*err == NULL && asprintf (err, "Invalid value with type 'string' for key '%s'", srckey) < 0) { @@ -1843,65 +1802,54 @@ append_json_map_string_string (json_map_string_string *map, const char *key, con return 0; } -static void -cleanup_yajl_gen (yajl_gen g) -{ - if (! g) - return; - yajl_gen_clear (g); - yajl_gen_free (g); -} - -define_cleaner_function (yajl_gen, cleanup_yajl_gen) - -char * -json_marshal_string (const char *str, size_t length, const struct parser_context *ctx, parser_error *err) -{ - __auto_cleanup (cleanup_yajl_gen) yajl_gen g = NULL; - struct parser_context tmp_ctx = { 0 }; - const unsigned char *gen_buf = NULL; - char *json_buf = NULL; - size_t gen_len = 0; - yajl_gen_status stat; - - if (str == NULL || err == NULL) - return NULL; - - *err = NULL; - if (ctx == NULL) - ctx = (const struct parser_context *) (&tmp_ctx); - - if (! json_gen_init (&g, ctx)) - { - *err = strdup ("Json_gen init failed"); - return json_buf; - } - stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) str, length); - if (yajl_gen_status_ok != stat) - { - if (asprintf (err, "error generating json, errcode: %d", (int) stat) < 0) - *err = strdup ("error allocating memory"); - return json_buf; - } - yajl_gen_get_buf (g, &gen_buf, &gen_len); - if (gen_buf == NULL) - { - *err = strdup ("Error to get generated json"); - return json_buf; - } - - json_buf = calloc (1, gen_len + 1); - if (json_buf == NULL) - { - *err = strdup ("error allocating memory"); - return json_buf; - } - - (void) memcpy (json_buf, gen_buf, gen_len); - json_buf[gen_len] = '\0'; - - return json_buf; -} +// char * +// json_marshal_string (const char *str, size_t length, const struct parser_context *ctx, parser_error *err) +// { +// __auto_cleanup (cleanup_yajl_gen) yajl_gen g = NULL; +// struct parser_context tmp_ctx = { 0 }; +// const unsigned char *gen_buf = NULL; +// char *json_buf = NULL; +// size_t gen_len = 0; +// yajl_gen_status stat; + +// if (str == NULL || err == NULL) +// return NULL; + +// *err = NULL; +// if (ctx == NULL) +// ctx = (const struct parser_context *) (&tmp_ctx); + +// if (! json_gen_init (&g, ctx)) +// { +// *err = strdup ("Json_gen init failed"); +// return json_buf; +// } +// stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) str, length); +// if (yajl_gen_status_ok != stat) +// { +// if (asprintf (err, "error generating json, errcode: %d", (int) stat) < 0) +// *err = strdup ("error allocating memory"); +// return json_buf; +// } +// yajl_gen_get_buf (g, &gen_buf, &gen_len); +// if (gen_buf == NULL) +// { +// *err = strdup ("Error to get generated json"); +// return json_buf; +// } + +// json_buf = calloc (1, gen_len + 1); +// if (json_buf == NULL) +// { +// *err = strdup ("error allocating memory"); +// return json_buf; +// } + +// (void) memcpy (json_buf, gen_buf, gen_len); +// json_buf[gen_len] = '\0'; + +// return json_buf; +// } /** * json_array_to_struct This function extracts keys and values and stores it in struct @@ -2010,15 +1958,4 @@ json_t *copy_unmatched_fields(json_t *src, const char **exclude_keys, size_t num } 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)); } \ No newline at end of file diff --git a/src/ocispec/json_common.h b/src/ocispec/json_common.h index 538fab47..8d42d7ba 100644 --- a/src/ocispec/json_common.h +++ b/src/ocispec/json_common.h @@ -6,8 +6,6 @@ #include #include #include -#include -#include #include #ifdef __cplusplus @@ -36,6 +34,12 @@ extern "C" { // options not to validate utf8 data #define OPT_GEN_NO_VALIDATE_UTF8 0x10 +// Generating json succeded +#define JSON_GEN_SUCCESS 0 + +// Generating json failed +#define JSON_GEN_FAILED -1 + #define define_cleaner_function(type, cleaner) \ static inline void cleaner##_function (type *ptr) \ { \ @@ -80,15 +84,13 @@ struct parser_context FILE *errfile; }; -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); +// int map_uint (json_t *root, long long unsigned int num); -yajl_gen_status map_int (void *ctx, long long int num); +// int map_int (json_t *root, long long int num); -bool json_gen_init (yajl_gen *g, const struct parser_context *ctx); +// bool json_gen_init (yajl_gen *g, const struct parser_context *ctx); -yajl_val get_val (yajl_val tree, const char *name, yajl_type type); +// yajl_val get_val (yajl_val tree, const char *name, yajl_type type); char *safe_strdup (const char *src); @@ -147,9 +149,9 @@ typedef struct void free_json_map_int_int (json_map_int_int *map); -json_map_int_int *make_json_map_int_int (yajl_val src, const struct parser_context *ctx, parser_error *err); +//json_map_int_int *make_json_map_int_int (yajl_val src, const struct parser_context *ctx, parser_error *err); -yajl_gen_status gen_json_map_int_int (void *ctx, const json_map_int_int *map, const struct parser_context *ptx, +int gen_json_map_int_int (json_t *root, const json_map_int_int *map, parser_error *err); int append_json_map_int_int (json_map_int_int *map, int key, int val); @@ -163,9 +165,9 @@ typedef struct void free_json_map_int_bool (json_map_int_bool *map); -json_map_int_bool *make_json_map_int_bool (yajl_val src, const struct parser_context *ctx, parser_error *err); +//json_map_int_bool *make_json_map_int_bool (yajl_val src, const struct parser_context *ctx, parser_error *err); -yajl_gen_status gen_json_map_int_bool (void *ctx, const json_map_int_bool *map, const struct parser_context *ptx, +int gen_json_map_int_bool (json_t *root, const json_map_int_bool *map, parser_error *err); int append_json_map_int_bool (json_map_int_bool *map, int key, bool val); @@ -179,9 +181,9 @@ typedef struct void free_json_map_int_string (json_map_int_string *map); -json_map_int_string *make_json_map_int_string (yajl_val src, const struct parser_context *ctx, parser_error *err); +//json_map_int_string *make_json_map_int_string (yajl_val src, const struct parser_context *ctx, parser_error *err); -yajl_gen_status gen_json_map_int_string (void *ctx, const json_map_int_string *map, const struct parser_context *ptx, +int gen_json_map_int_string (json_t *root, const json_map_int_string *map, parser_error *err); int append_json_map_int_string (json_map_int_string *map, int key, const char *val); @@ -195,9 +197,9 @@ typedef struct void free_json_map_string_int (json_map_string_int *map); -json_map_string_int *make_json_map_string_int (yajl_val src, const struct parser_context *ctx, parser_error *err); +//json_map_string_int *make_json_map_string_int (yajl_val src, const struct parser_context *ctx, parser_error *err); -yajl_gen_status gen_json_map_string_int (void *ctx, const json_map_string_int *map, const struct parser_context *ptx, +int gen_json_map_string_int (json_t *root, const json_map_string_int *map, parser_error *err); int append_json_map_string_int (json_map_string_int *map, const char *key, int val); @@ -211,7 +213,7 @@ typedef struct void free_json_map_string_bool (json_map_string_bool *map); -json_map_string_bool *make_json_map_string_bool (yajl_val src, const struct parser_context *ctx, parser_error *err); +//json_map_string_bool *make_json_map_string_bool (yajl_val src, const struct parser_context *ctx, parser_error *err); typedef struct { @@ -222,14 +224,14 @@ typedef struct void free_json_map_string_int64 (json_map_string_int64 *map); -json_map_string_int64 *make_json_map_string_int64 (yajl_val src, const struct parser_context *ctx, parser_error *err); +//json_map_string_int64 *make_json_map_string_int64 (yajl_val src, const struct parser_context *ctx, parser_error *err); -yajl_gen_status gen_json_map_string_int64 (void *ctx, const json_map_string_int64 *map, - const struct parser_context *ptx, parser_error *err); +int gen_json_map_string_int64 (json_t *root, const json_map_string_int64 *map, + parser_error *err); int append_json_map_string_int64 (json_map_string_int64 *map, const char *key, int64_t val); -yajl_gen_status gen_json_map_string_bool (void *ctx, const json_map_string_bool *map, const struct parser_context *ptx, +int gen_json_map_string_bool (json_t *root, const json_map_string_bool *map, parser_error *err); int append_json_map_string_bool (json_map_string_bool *map, const char *key, bool val); @@ -247,8 +249,7 @@ json_map_string_string *clone_map_string_string (json_map_string_string *src); 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); +int gen_json_map_string_string (json_t *root, const json_map_string_string *map, parser_error *err); int append_json_map_string_string (json_map_string_string *map, const char *key, const char *val); @@ -273,7 +274,7 @@ 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); +//yajl_val json_to_yajl(json_t *json); #ifdef __cplusplus } diff --git a/src/ocispec/sources.py b/src/ocispec/sources.py index 9950a1e1..33ded527 100755 --- a/src/ocispec/sources.py +++ b/src/ocispec/sources.py @@ -373,30 +373,20 @@ def get_map_string_obj(obj, c_file, prefix): c_file.append(' size_t len = 0, i;\n') c_file.append(" if (ptr != NULL)\n") c_file.append(" len = ptr->len;\n") - c_file.append(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") - c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 0);\n') - c_file.append(" stat = yajl_gen_map_open ((yajl_gen) g);\n") - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(f' if (len || (ptr != NULL && ptr->keys != NULL && ptr->{child.fixname} != NULL))\n') c_file.append(' {\n') c_file.append(' for (i = 0; i < len; i++)\n') c_file.append(' {\n') - c_file.append(' char *str = ptr->keys[i] ? ptr->keys[i] : "";\n') - c_file.append(' stat = yajl_gen_string ((yajl_gen) g, \ -(const unsigned char *)str, strlen (str));\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(' json_t *subroot = json_object();\n') + c_file.append(f' stat = gen_{childname} (subroot, ptr->{child.fixname}[i], err);\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.append(f' stat = gen_{childname} (g, ptr->{child.fixname}[i], ctx, err);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(' char *str = ptr->keys[i] ? ptr->keys[i] : "";\n') + c_file.append(' stat = json_object_set (root, (const char *)str, subroot);\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(' }\n') c_file.append(' }\n') - c_file.append(" stat = yajl_gen_map_close ((yajl_gen) g);\n") - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.append(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") - c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 1);\n') def get_obj_arr_obj_array(obj, c_file, prefix): if obj.subtypobj or obj.subtyp == 'object': @@ -405,57 +395,39 @@ def get_obj_arr_obj_array(obj, c_file, prefix): typename = obj.subtypname else: typename = helpers.get_name_substr(obj.name, prefix) - c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname} != NULL))\n') + c_file.append(f' if (ptr != NULL && ptr->{obj.fixname} != NULL)\n') c_file.append(' {\n') c_file.append(' size_t len = 0, i;\n') - c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.origname}"), {int(l)} /* strlen ("{obj.origname}") */);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(f" if (ptr != NULL && ptr->{obj.fixname} != NULL)\n") c_file.append(f" len = ptr->{obj.fixname}_len;\n") - c_file.append(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") - c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 0);\n') - c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(' json_t *subroot = json_array();\n') c_file.append(' for (i = 0; i < len; i++)\n') c_file.append(' {\n') if obj.doublearray: - c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(' json_t *subsubroot = json_array();\n') c_file.append(" size_t j;\n") c_file.append(f' for (j = 0; j < ptr->{obj.fixname}_item_lens[i]; j++)\n') c_file.append(' {\n') - c_file.append(f' stat = gen_{typename} (g, ptr->{obj.fixname}[i][j], ctx, err);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(f' stat = gen_{typename} (subsubroot, ptr->{obj.fixname}[i][j], err);\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(' }\n') - c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') + c_file.append(' stat = json_array_append(subroot, subsubroot);\n') else: - c_file.append(f' stat = gen_{typename} (g, ptr->{obj.fixname}[i], ctx, err);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(f' stat = gen_{typename} (subroot, ptr->{obj.fixname}[i], err);\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(' }\n') - c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') - c_file.append(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") - c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 1);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(f' stat = json_object_set(root, (const char *)("{obj.origname}"), subroot);\n') c_file.append(' }\n') elif obj.subtyp == 'byte': l = len(obj.origname) - c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname} != NULL && ptr->{obj.fixname}_len))\n') + c_file.append(f' if (ptr != NULL && ptr->{obj.fixname} != NULL && ptr->{obj.fixname}_len)\n') c_file.append(' {\n') c_file.append(' const char *str = "";\n') c_file.append(' size_t len = 0;\n') - c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") if obj.doublearray: - c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(' json_t *subroot = json_array();\n') c_file.append(" {\n") c_file.append(" size_t i;\n") c_file.append(f" for (i = 0; i < ptr->{obj.fixname}_len; i++)\n") @@ -464,61 +436,46 @@ def get_obj_arr_obj_array(obj, c_file, prefix): c_file.append(f" str = (const char *)ptr->{obj.fixname}[i];\n") c_file.append(" else ()\n") c_file.append(" str = "";\n") - c_file.append(' stat = yajl_gen_string ((yajl_gen) g, \ - (const unsigned char *)str, strlen(str));\n') + c_file.append(' stat = json_array_append (subroot, json_string((const char *)str));\n') c_file.append(" }\n") c_file.append(" }\n") - c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') + c_file.append(f' stat = json_object_set (root, (const char *)("{obj.origname}"), subroot);\n') else: c_file.append(f" if (ptr != NULL && ptr->{obj.fixname} != NULL)\n") c_file.append(" {\n") c_file.append(f" str = (const char *)ptr->{obj.fixname};\n") c_file.append(f" len = ptr->{obj.fixname}_len;\n") c_file.append(" }\n") - c_file.append(' stat = yajl_gen_string ((yajl_gen) g, \ - (const unsigned char *)str, len);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(f' stat = json_object_set (root, (const char *)("{obj.origname}"), json_string((const char *)str));\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(" }\n") else: - l = len(obj.origname) - c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname} != NULL))\n') + c_file.append(f' if (ptr != NULL && ptr->{obj.fixname} != NULL)\n') c_file.append(' {\n') c_file.append(' size_t len = 0, i;\n') - c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(' json_t *subroot = json_array();\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(f" if (ptr != NULL && ptr->{obj.fixname} != NULL)\n") c_file.append(f" len = ptr->{obj.fixname}_len;\n") - c_file.append(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") - c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 0);\n') - c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(' for (i = 0; i < len; i++)\n') c_file.append(' {\n') if obj.doublearray: typename = helpers.get_map_c_types(obj.subtyp) - c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(' json_t *subsubroot = json_array();\n') c_file.append(" size_t j;\n") c_file.append(f' for (j = 0; j < ptr->{obj.fixname}_item_lens[i]; j++)\n') c_file.append(' {\n') - json_value_generator(c_file, 4, f"ptr->{obj.fixname}[i][j]", 'g', 'ctx', obj.subtyp) + json_value_generator(c_file, 4, f"ptr->{obj.fixname}[i][j]", 'subsubroot', obj.subtyp) c_file.append(' }\n') - c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') + c_file.append(' stat = json_array_append(subroot, subsubroot);\n') else: - json_value_generator(c_file, 3, f"ptr->{obj.fixname}[i]", 'g', 'ctx', obj.subtyp) - + json_value_generator(c_file, 3, f"ptr->{obj.fixname}[i]", 'subroot', obj.subtyp) c_file.append(' }\n') - c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.append(" if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))\n") - c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 1);\n') - c_file.append(' }\n') + c_file.append(f' stat = json_object_set(root, (const char *)("{obj.origname}"), subroot);\n') + c_file.append(' }\n') def get_obj_arr_obj(obj, c_file, prefix): """ @@ -528,64 +485,57 @@ def get_obj_arr_obj(obj, c_file, prefix): """ if obj.typ == 'string': l = len(obj.origname) - c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname} != NULL))\n' ) + c_file.append(f' if (ptr != NULL && ptr->{obj.fixname} != NULL)\n' ) c_file.append(' {\n') - c_file.append(' char *str = "";\n') - c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(f' stat = json_object_set(root, (const char *)("{obj.origname}"), json_string(ptr->{obj.fixname}));\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.append(f" if (ptr != NULL && ptr->{obj.fixname} != NULL)\n") - c_file.append(f" str = ptr->{obj.fixname};\n") - json_value_generator(c_file, 2, "str", 'g', 'ctx', obj.typ) c_file.append(" }\n") elif helpers.judge_data_type(obj.typ): - c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname}_present))\n') + c_file.append(f' if (ptr != NULL && ptr->{obj.fixname}_present)\n') c_file.append(' {\n') + json_conv = 'json_real' if obj.typ == 'double': numtyp = 'double' - elif obj.typ.startswith("uint") or obj.typ == 'GID' or obj.typ == 'UID': - numtyp = 'long long unsigned int' else: - numtyp = 'long long int' + numtyp = 'json_int_t' + json_conv = 'json_integer' l = len(obj.origname) - c_file.append(f' {numtyp} num = 0;\n') - c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.append(f" if (ptr != NULL && ptr->{obj.fixname})\n") + c_file.append(f' {numtyp} num = 0;\n') + c_file.append(f" if (ptr != NULL && ptr->{obj.fixname})\n") c_file.append(f" num = ({numtyp})ptr->{obj.fixname};\n") - json_value_generator(c_file, 2, "num", 'g', 'ctx', obj.typ) - c_file.append(" }\n") - elif helpers.judge_data_pointer_type(obj.typ): - c_file.append(f' if ((ptr != NULL && ptr->{obj.fixname} != NULL))\n') - c_file.append(' {\n') - numtyp = helpers.obtain_data_pointer_type(obj.typ) - if numtyp == "": - return - l = len(obj.origname) - c_file.append(f' {helpers.get_map_c_types(numtyp)} num = 0;\n') - c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, \ -(const unsigned char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(f' stat = json_object_set(root, (const char *)("{obj.origname}"), {json_conv}(num));\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.append(f" if (ptr != NULL && ptr->{obj.fixname} != NULL)\n") - c_file.append(" {\n") - c_file.append(f" num = ({helpers.get_map_c_types(numtyp)})*(ptr->{obj.fixname});\n") - c_file.append(" }\n") - json_value_generator(c_file, 2, "num", 'g', 'ctx', numtyp) c_file.append(" }\n") +# elif helpers.judge_data_pointer_type(obj.typ): +# c_file.append(f' if (ptr != NULL && ptr->{obj.fixname} != NULL)\n') +# c_file.append(' {\n') +# numtyp = helpers.obtain_data_pointer_type(obj.typ) +# if numtyp == "": +# return +# l = len(obj.origname) +# c_file.append(f' {helpers.get_map_c_types(numtyp)} num = 0;\n') +# c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, \ +# (const char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') +# c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") +# c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") +# c_file.append(f" if (ptr != NULL && ptr->{obj.fixname} != NULL)\n") +# c_file.append(" {\n") +# c_file.append(f" num = ({helpers.get_map_c_types(numtyp)})*(ptr->{obj.fixname});\n") +# c_file.append(" }\n") +# json_value_generator(c_file, 2, "num", 'root', numtyp) +# c_file.append(" }\n") elif obj.typ == 'boolean': - c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname}_present))\n') + c_file.append(f' if (ptr != NULL && ptr->{obj.fixname}_present)\n') c_file.append(' {\n') c_file.append(' bool b = false;\n') - l = len(obj.origname) - c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(f" if (ptr != NULL && ptr->{obj.fixname})\n") + c_file.append(f" b = ptr->{obj.fixname};\n") + c_file.append(f' stat = json_object_set(root, (const char *)("{obj.origname}"), json_boolean(ptr->{obj.fixname}));\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.append(f" if (ptr != NULL && ptr->{obj.fixname})\n") - c_file.append(f" b = ptr->{obj.fixname};\n") c_file.append(" \n") - json_value_generator(c_file, 2, "b", 'g', 'ctx', obj.typ) c_file.append(" }\n") elif obj.typ == 'object' or obj.typ == 'mapStringObject': l = len(obj.origname) @@ -593,26 +543,28 @@ def get_obj_arr_obj(obj, c_file, prefix): typename = obj.subtypname else: typename = helpers.get_prefixed_name(obj.name, prefix) - c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname} != NULL))\n') + c_file.append(f' if (ptr != NULL && ptr->{obj.fixname} != NULL)\n') c_file.append(" {\n") - c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.origname}"), {l} /* strlen ("{obj.origname}") */);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(' json_t *subroot = json_object();\n') + c_file.append(f' stat = gen_{typename} (subroot, ptr != NULL ? ptr->{obj.fixname} : NULL, err);\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.append(f' stat = gen_{typename} (g, ptr != NULL ? ptr->{obj.fixname} : NULL, ctx, err);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(f' stat = json_object_set(root, (const char *)("{obj.origname}"), subroot);\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(" }\n") elif obj.typ == 'array': get_obj_arr_obj_array(obj, c_file, prefix) elif helpers.valid_basic_map_name(obj.typ): l = len(obj.origname) - c_file.append(f' if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname} != NULL))\n') + c_file.append(f' if (ptr != NULL && ptr->{obj.fixname} != NULL)\n') c_file.append(' {\n') - c_file.append(f' stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)("{obj.fixname}"), {l} /* strlen ("{obj.fixname}") */);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(' json_t *subroot = json_object();\n') + c_file.append(f' stat = gen_{helpers.make_basic_map_name(obj.typ)} (subroot, ptr ? ptr->{obj.fixname} : NULL, err);\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - c_file.append(f' stat = gen_{helpers.make_basic_map_name(obj.typ)} (g, ptr ? ptr->{obj.fixname} : NULL, ctx, err);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(f' stat = json_object_set(root, (const char *)("{obj.fixname}"), subroot);\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(" }\n") @@ -633,40 +585,27 @@ def get_c_json(obj, c_file, prefix): if objs is None: return c_file.append( - f"yajl_gen_status\ngen_{typename} (yajl_gen g, const {typename} *ptr, const struct parser_context " \ - "*ctx, parser_error *err)\n") + f"int\ngen_{typename} (json_t *root, const {typename} *ptr, " \ + "parser_error *err)\n") c_file.append("{\n") - c_file.append(" yajl_gen_status stat = yajl_gen_status_ok;\n") + c_file.append(" int stat = JSON_GEN_SUCCESS;\n") c_file.append(" *err = NULL;\n") c_file.append(" (void) ptr; /* Silence compiler warning. */\n") if obj.typ == 'mapStringObject': get_map_string_obj(obj, c_file, prefix) elif obj.typ == 'object' or (obj.typ == 'array' and obj.subtypobj): nodes = obj.children if obj.typ == 'object' else obj.subtypobj - if nodes is None: - c_file.append(' if (!(ctx->options & OPT_GEN_SIMPLIFY))\n') - c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 0);\n') - - c_file.append(" stat = yajl_gen_map_open ((yajl_gen) g);\n") - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") for i in nodes or []: get_obj_arr_obj(i, c_file, prefix) if obj.typ == 'object': if obj.children is not None: c_file.append(" if (ptr != NULL && ptr->_residual != NULL)\n") c_file.append(" {\n") - c_file.append(" stat = gen_yajl_object_residual (ptr->_residual, g, err);\n") - c_file.append(" if (yajl_gen_status_ok != stat)\n") + c_file.append(" stat = json_object_update_missing(root, ptr->_residual);\n") + c_file.append(" if (JSON_GEN_SUCCESS != stat)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(" }\n") - c_file.append(" stat = yajl_gen_map_close ((yajl_gen) g);\n") - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") - if nodes is None: - c_file.append(' if (!(ctx->options & OPT_GEN_SIMPLIFY))\n') - c_file.append(' yajl_gen_config (g, yajl_gen_beautify, 1);\n') - c_file.append(' return yajl_gen_status_ok;\n') + c_file.append(' return JSON_GEN_SUCCESS;\n') c_file.append("}\n\n") @@ -952,32 +891,32 @@ def make_clone(obj, c_file, prefix): c_file.append("}\n\n") -def json_value_generator(c_file, level, src, dst, ptx, typ): +def json_value_generator(c_file, level, src, dst, typ): """ Description: json value generateor Interface: None History: 2019-06-17 """ if helpers.valid_basic_map_name(typ): - c_file.append(f'{" " * (level)}stat = gen_{helpers.make_basic_map_name(typ)} ({dst}, {src}, {ptx}, err);\n') - c_file.append(f"{' ' * level}if (stat != yajl_gen_status_ok)\n") + c_file.append(f'{" " * (level)}stat = gen_{helpers.make_basic_map_name(typ)} ({dst}, {src}, err);\n') + c_file.append(f"{' ' * level}if (stat != JSON_GEN_SUCCESS)\n") c_file.append(f"{' ' * (level + 1)}GEN_SET_ERROR_AND_RETURN (stat, err);\n") elif typ == 'string': - c_file.append(f'{" " * (level)}stat = yajl_gen_string ((yajl_gen){dst}, (const unsigned char *)({src}), strlen ({src}));\n') - c_file.append(f"{' ' * level}if (stat != yajl_gen_status_ok)\n") + c_file.append(f'{" " * (level)}stat = json_array_append({dst}, json_string({src}));\n') + c_file.append(f"{' ' * level}if (stat != JSON_GEN_SUCCESS)\n") c_file.append(f"{' ' * (level + 1)}GEN_SET_ERROR_AND_RETURN (stat, err);\n") elif helpers.judge_data_type(typ): if typ == 'double': - c_file.append(f'{" " * (level)}stat = yajl_gen_double ((yajl_gen){dst}, {src});\n') + c_file.append(f'{" " * (level)}stat = json_array_append({dst}, json_real({src}));\n') elif typ.startswith("uint") or typ == 'GID' or typ == 'UID': - c_file.append(f"{' ' * level}stat = map_uint ({dst}, {src});\n") + c_file.append(f"{' ' * level}stat = json_array_append ({dst}, json_integer({src}));\n") else: - c_file.append(f"{' ' * level}stat = map_int ({dst}, {src});\n") - c_file.append(f"{' ' * level}if (stat != yajl_gen_status_ok)\n") + c_file.append(f"{' ' * level}stat = json_array_append({dst}, json_integer({src}));\n") + c_file.append(f"{' ' * level}if (stat != JSON_GEN_SUCCESS)\n") c_file.append(f"{' ' * (level + 1)}GEN_SET_ERROR_AND_RETURN (stat, err);\n") elif typ == 'boolean': - c_file.append(f'{" " * (level)}stat = yajl_gen_bool ((yajl_gen){dst}, (int)({src}));\n') - c_file.append(f"{' ' * level}if (stat != yajl_gen_status_ok)\n") + c_file.append(f'{" " * (level)}stat = json_array_append({dst}, json_boolean({src}));\n') + c_file.append(f"{' ' * level}if (stat != JSON_GEN_SUCCESS)\n") c_file.append(f"{' ' * (level + 1)}GEN_SET_ERROR_AND_RETURN (stat, err);\n") def make_c_array_free (i, c_file, prefix): @@ -1394,24 +1333,19 @@ def get_c_epilog_for_array_make_gen(c_file, prefix, typ, obj): return typename = helpers.get_top_array_type_name(obj.name, prefix) - c_file.append(f"yajl_gen_status gen_{typename} (yajl_gen g, const {typename} *ptr, const struct parser_context *ctx," + """ + c_file.append(f"int gen_{typename} (json_t *root, const {typename} *ptr, " + """ parser_error *err) { - yajl_gen_status stat; size_t i; - + int stat; if (ptr == NULL) - return yajl_gen_status_ok; - *err = NULL; + return JSON_GEN_SUCCESS; """) if obj.subtypobj or obj.subtyp == 'object': c_file.append("""\n - stat = yajl_gen_array_open ((yajl_gen) g); - if (stat != yajl_gen_status_ok) - GEN_SET_ERROR_AND_RETURN (stat, err); + //CheckCheck for (i = 0; i < ptr->len; i++) - { """) if obj.subtypname: @@ -1420,89 +1354,86 @@ def get_c_epilog_for_array_make_gen(c_file, prefix, typ, obj): subtypename = helpers.get_name_substr(obj.name, prefix) c_file.append(' {\n') if obj.doublearray: - c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") + c_file.append(" json_t *subroot = json_array();\n") c_file.append(" size_t j;\n") c_file.append(' for (j = 0; j < ptr->subitem_lens[i]; j++)\n') c_file.append(' {\n') - c_file.append(f' stat = gen_{subtypename} (g, ptr->items[i][j], ctx, err);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(f' stat = gen_{subtypename} (subroot, ptr->items[i][j], err);\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(' }\n') - c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') + c_file.append(" int stat = json_array_append(root, subroot);\n") + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") else: - c_file.append(f' stat = gen_{subtypename} (g, ptr->items[i], ctx, err);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") + c_file.append(f' int stat = gen_{subtypename} (root, ptr->items[i], err);\n') + c_file.append(f' printf("%d sssssssssssssssssssssssssssssssssssssssssssss", stat);\n') + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append("""\n } - } - stat = yajl_gen_array_close ((yajl_gen) g); """) elif obj.subtyp == 'byte': c_file.append(' {\n') c_file.append(' const char *str = NULL;\n') if obj.doublearray: - c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(" {\n") c_file.append(" size_t i;\n") + c_file.append(" json_t *subroot = json_array();\n") c_file.append(" for (i = 0; i < ptr->len; i++)\n") c_file.append(" {\n") c_file.append(" if (ptr->items[i] != NULL)\n") c_file.append(" str = (const char *)ptr->items[i];\n") c_file.append(" else ()\n") c_file.append(" str = "";\n") - c_file.append(' stat = yajl_gen_string ((yajl_gen) g, \ - (const unsigned char *)str, strlen(str));\n') + c_file.append(" json_t *jstr = json_string(str);\n") + c_file.append(" int stat = json_array_append(subroot, jstr);\n") + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(" }\n") + c_file.append(" int stat = json_array_append(root, subroot);\n") + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(" }\n") - c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') else: c_file.append(" if (ptr != NULL && ptr->items != NULL)\n") c_file.append(" {\n") c_file.append(" str = (const char *)ptr->items;\n") c_file.append(" }\n") - c_file.append(' stat = yajl_gen_string ((yajl_gen) g, \ - (const unsigned char *)str, ptr->len);\n') + c_file.append(" json_t *jstr = json_string(str);\n") + c_file.append(" int stat = json_array_append(root, jstr);\n") + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(' }\n') else: c_file.append("""\n - stat = yajl_gen_array_open ((yajl_gen) g); - if (stat != yajl_gen_status_ok) - GEN_SET_ERROR_AND_RETURN (stat, err); for (i = 0; i < ptr->len; i++) { """) c_file.append(' {\n') if obj.doublearray: - c_file.append(' stat = yajl_gen_array_open ((yajl_gen) g);\n') - c_file.append(" if (stat != yajl_gen_status_ok)\n") - c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") c_file.append(" size_t j;\n") + c_file.append(" json_t *subroot = json_array();\n") c_file.append(' for (j = 0; j < ptr->subitem_lens[i]; j++)\n') c_file.append(' {\n') - json_value_generator(c_file, 4, "ptr->items[i][j]", 'g', 'ctx', obj.subtyp) + json_value_generator(c_file, 4, "ptr->items[i][j]", 'subroot', obj.subtyp) c_file.append(' }\n') - c_file.append(' stat = yajl_gen_array_close ((yajl_gen) g);\n') + c_file.append(" int stat = json_array_append(root, subroot);\n") + c_file.append(" if (stat != JSON_GEN_SUCCESS)\n") + c_file.append(" GEN_SET_ERROR_AND_RETURN (stat, err);\n") else: - json_value_generator(c_file, 3, "ptr->items[i]", 'g', 'ctx', obj.subtyp) + json_value_generator(c_file, 3, "ptr->items[i]", 'root', obj.subtyp) c_file.append("""\n } } - stat = yajl_gen_array_close ((yajl_gen) g); """) c_file.append("""\n - if (ptr->len > 0 && !(ctx->options & OPT_GEN_SIMPLIFY)) - yajl_gen_config (g, yajl_gen_beautify, 1); - if (stat != yajl_gen_status_ok) + if (stat != JSON_GEN_SUCCESS) GEN_SET_ERROR_AND_RETURN (stat, err); - return yajl_gen_status_ok; + return JSON_GEN_SUCCESS; } """) @@ -1599,63 +1530,34 @@ def get_c_epilog(c_file, prefix, typ, obj): "\treturn ptr; \n}\n" ) - c_file.append("""\nstatic void\ncleanup_yajl_gen (yajl_gen g) + c_file.append("""\nstatic void\ncleanup_yajl_gen (json_t *root) { - if (!g) - return; - yajl_gen_clear (g); - yajl_gen_free (g); + json_decref(root); } -define_cleaner_function (yajl_gen, cleanup_yajl_gen) +//define_cleaner_function (json_t *, json_decref) """) c_file.append("\n char * \n" + -f"{typename}_generate_json (const {typename} *ptr, const struct parser_context *ctx, parser_error *err)" + +f"{typename}_generate_json (const {typename} *ptr, parser_error *err)" + """{ - __auto_cleanup(cleanup_yajl_gen) yajl_gen g = NULL; - struct parser_context tmp_ctx = { 0 }; - const unsigned char *gen_buf = NULL; - char *json_buf = NULL; - size_t gen_len = 0; + __auto_cleanup(json_decref) json_t *root = json_object(); if (ptr == NULL || err == NULL) return NULL; *err = NULL; - if (ctx == NULL) - ctx = (const struct parser_context *)(&tmp_ctx); - - if (!json_gen_init(&g, ctx)) - { - *err = strdup ("Json_gen init failed"); - return json_buf; - } \n """ + - f"if (yajl_gen_status_ok != gen_{typename} (g, ptr, ctx, err))" + + f"if (JSON_GEN_FAILED == gen_{typename} (root, ptr, err))" + """ { if (*err == NULL) *err = strdup ("Failed to generate json"); - return json_buf; - } - - yajl_gen_get_buf (g, &gen_buf, &gen_len); - if (gen_buf == NULL) - { - *err = strdup ("Error to get generated json"); - return json_buf; + return NULL; } - json_buf = calloc (1, gen_len + 1); - if (json_buf == NULL) - { - *err = strdup ("Cannot allocate memory"); - return json_buf; - } - (void) memcpy (json_buf, gen_buf, gen_len); - json_buf[gen_len] = '\\0'; + char *json_str = json_dumps(root, JSON_INDENT(2)); - return json_buf; + return json_str; } """) diff --git a/tests/test-1.c b/tests/test-1.c index 6e35b424..b87a7d56 100644 --- a/tests/test-1.c +++ b/tests/test-1.c @@ -34,7 +34,7 @@ main () printf ("error %s\n", err); exit (1); } - json_buf = runtime_spec_schema_config_schema_generate_json(container, 0, &err); + json_buf = runtime_spec_schema_config_schema_generate_json(container, &err); if (json_buf == NULL) { printf("gen error %s\n", err); exit (1); diff --git a/tests/test-10.c b/tests/test-10.c index 4cb884ff..38c052d7 100644 --- a/tests/test-10.c +++ b/tests/test-10.c @@ -239,7 +239,7 @@ do_test_object_double_array() free(test_data->refobjarrays[0][0]->item1); test_data->refobjarrays[0][0]->item1 = strdup("objectrefstr"); - json_buf = basic_test_double_array_generate_json(test_data, &ctx, &err); + json_buf = basic_test_double_array_generate_json(test_data, &err); if (json_buf == NULL) { printf("gen error %s\n", err); ret = 1; @@ -312,7 +312,7 @@ int do_test_top_array_of_int() } } test_data->items[0] = 111; - json_buf = basic_test_top_array_int_container_generate_json(test_data, &ctx, &err); + json_buf = basic_test_top_array_int_container_generate_json(test_data, &err); if (json_buf == NULL) { printf("gen error %s\n", err); @@ -369,7 +369,7 @@ int do_test_top_array_of_string() } free(test_data->items[0]); test_data->items[0] = strdup("hello"); - json_buf = basic_test_top_array_string_container_generate_json(test_data, &ctx, &err); + json_buf = basic_test_top_array_string_container_generate_json(test_data, &err); if (json_buf == NULL) { printf("gen error %s\n", err); @@ -437,7 +437,7 @@ int do_test_top_double_array_of_string() } free(test_data->items[0][0]); test_data->items[0][0] = strdup("hello"); - json_buf = basic_test_top_double_array_string_container_generate_json(test_data, &ctx, &err); + json_buf = basic_test_top_double_array_string_container_generate_json(test_data, &err); if (json_buf == NULL) { printf("gen error %s\n", err); @@ -504,7 +504,7 @@ int do_test_top_double_array_of_int() } } test_data->items[0][0] = 888; - json_buf = basic_test_top_double_array_int_container_generate_json(test_data, &ctx, &err); + json_buf = basic_test_top_double_array_int_container_generate_json(test_data, &err); if (json_buf == NULL) { printf("gen error %s\n", err); @@ -587,7 +587,7 @@ int do_test_top_double_array_of_obj() test_data->items[0][0]->second = 999; free(test_data->items[0][1]->third); test_data->items[0][1]->third = strdup("hello"); - json_buf = basic_test_top_double_array_obj_container_generate_json(test_data, &ctx, &err); + json_buf = basic_test_top_double_array_obj_container_generate_json(test_data, &err); if (json_buf == NULL) { printf("gen error %s\n", err); @@ -676,7 +676,7 @@ int do_test_top_double_array_of_refobj() test_data->items[0][0]->item2 = 999; free(test_data->items[0][1]->item1); test_data->items[0][1]->item1 = strdup("hello"); - json_buf = basic_test_top_double_array_refobj_container_generate_json(test_data, &ctx, &err); + json_buf = basic_test_top_double_array_refobj_container_generate_json(test_data, &err); if (json_buf == NULL) { printf("gen error %s\n", err); diff --git a/tests/test-11.c b/tests/test-11.c index 20c5f492..488aff70 100644 --- a/tests/test-11.c +++ b/tests/test-11.c @@ -41,7 +41,7 @@ main () printf ("error %s\n", err); exit (1); } - json_buf = image_spec_schema_image_layout_schema_generate_json(image_layout, &ctx, &err); + json_buf = image_spec_schema_image_layout_schema_generate_json(image_layout, &err); if (json_buf == NULL) { printf("gen error %s\n", err); free(err); diff --git a/tests/test-3.c b/tests/test-3.c index 26f267aa..5f51295a 100644 --- a/tests/test-3.c +++ b/tests/test-3.c @@ -34,7 +34,7 @@ main () printf ("error %s\n", err); exit (1); } - json_buf = image_spec_schema_config_schema_generate_json(image, 0, &err); + json_buf = image_spec_schema_config_schema_generate_json(image, &err); if (json_buf == NULL) { printf("gen error %s\n", err); exit (1); diff --git a/tests/test-4.c b/tests/test-4.c index f1055416..33bb99d8 100644 --- a/tests/test-4.c +++ b/tests/test-4.c @@ -34,7 +34,7 @@ main () printf ("error %s\n", err); exit (1); } - json_buf = image_spec_schema_image_index_schema_generate_json(image_index, 0, &err); + json_buf = image_spec_schema_image_index_schema_generate_json(image_index, &err); if (json_buf == NULL) { printf("gen error %s\n", err); exit (1); diff --git a/tests/test-5.c b/tests/test-5.c index e96cab12..31d48fbc 100644 --- a/tests/test-5.c +++ b/tests/test-5.c @@ -34,7 +34,7 @@ main () printf ("error %s\n", err); exit (1); } - json_buf = image_spec_schema_image_layout_schema_generate_json(image_layout, 0, &err); + json_buf = image_spec_schema_image_layout_schema_generate_json(image_layout, &err); if (json_buf == NULL) { printf("gen error %s\n", err); exit (1); diff --git a/tests/test-6.c b/tests/test-6.c index a5ad7f4e..76826dc4 100644 --- a/tests/test-6.c +++ b/tests/test-6.c @@ -34,7 +34,7 @@ main () printf ("error %s\n", err); exit (1); } - json_buf = image_spec_schema_image_manifest_schema_generate_json(manifest, 0, &err); + json_buf = image_spec_schema_image_manifest_schema_generate_json(manifest, &err); if (json_buf == NULL) { printf("gen error %s\n", err); exit (1); diff --git a/tests/test-7.c b/tests/test-7.c index 226d466e..c0dd8ae7 100644 --- a/tests/test-7.c +++ b/tests/test-7.c @@ -34,7 +34,7 @@ main () printf ("error %s\n", err); exit (1); } - json_buf = image_spec_schema_config_schema_generate_json(image, 0, &err); + json_buf = image_spec_schema_config_schema_generate_json(image, &err); if (json_buf == NULL) { printf("gen error %s\n", err); exit (1); diff --git a/tests/test-8.c b/tests/test-8.c index f066660a..c8eca592 100644 --- a/tests/test-8.c +++ b/tests/test-8.c @@ -36,7 +36,7 @@ main () exit (1); } json_buf = image_manifest_items_image_manifest_items_schema_container_generate_json ( - (const image_manifest_items_image_manifest_items_schema_container*)image_items, 0, &err); + (const image_manifest_items_image_manifest_items_schema_container*)image_items, &err); if (json_buf == NULL) { printf ("gen error %s\n", err); exit (1); diff --git a/tests/test-9.c b/tests/test-9.c index e7a358d6..595d82c1 100644 --- a/tests/test-9.c +++ b/tests/test-9.c @@ -41,7 +41,7 @@ main () printf ("error %s\n", err); exit (1); } - json_buf = image_spec_schema_image_layout_schema_generate_json(image_layout, &ctx, &err); + json_buf = image_spec_schema_image_layout_schema_generate_json(image_layout, &err); if (json_buf == NULL) { printf("gen error %s\n", err); free(err);