From 161824050964f191315928e6d598bc584ec7cb7b Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 27 Dec 2023 16:13:55 +0100 Subject: json: factor out json_object_put into json_free --- src/json.c | 5 +++++ src/json.h | 2 ++ src/json_rpc.c | 57 ++++++++++++++++++++++++++------------------------------- src/protocol.c | 3 ++- src/run_queue.c | 6 +++--- 5 files changed, 38 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/json.c b/src/json.c index 2e0a6bb..9a38e4e 100644 --- a/src/json.c +++ b/src/json.c @@ -18,6 +18,11 @@ #include #include +void json_free(struct json_object *obj) +{ + json_object_put(obj); +} + static const char *json_to_string_internal(struct json_object *obj, int flags) { const char *result = json_object_to_json_string_ext(obj, flags); diff --git a/src/json.h b/src/json.h index cd0ee8c..2d496c1 100644 --- a/src/json.h +++ b/src/json.h @@ -19,6 +19,8 @@ log_err("JSON: %s failed\n", fn); \ } while (0) +void json_free(struct json_object *); + const char *json_to_string(struct json_object *); const char *json_to_string_pretty(struct json_object *); struct json_object *json_from_string(const char *); diff --git a/src/json_rpc.c b/src/json_rpc.c index 6877fd3..4a8879a 100644 --- a/src/json_rpc.c +++ b/src/json_rpc.c @@ -204,12 +204,9 @@ static int jsonrpc_request_create_internal(struct jsonrpc_request **_request, in goto exit; } - request->impl = json_object_new_object(); - if (!request->impl) { - json_errno("json_object_new_object"); - ret = -1; + ret = json_new_object(&request->impl); + if (ret < 0) goto free; - } ret = jsonrpc_set_version(request->impl); if (ret < 0) @@ -235,7 +232,7 @@ static int jsonrpc_request_create_internal(struct jsonrpc_request **_request, in goto exit; free_impl: - json_object_put(request->impl); + json_free(request->impl); free: free(request); exit: @@ -250,7 +247,7 @@ int jsonrpc_request_create(struct jsonrpc_request **_request, int id, const char void jsonrpc_request_destroy(struct jsonrpc_request *request) { - json_object_put(request->impl); + json_free(request->impl); free(request); } @@ -313,7 +310,7 @@ int jsonrpc_request_recv(struct jsonrpc_request **request, int fd) return ret; free_impl: - json_object_put(impl); + json_free(impl); return ret; } @@ -331,24 +328,26 @@ const char *jsonrpc_request_get_method(const struct jsonrpc_request *request) static struct json_object *jsonrpc_request_create_params(struct jsonrpc_request *request) { + int ret = 0; const char *const key = jsonrpc_key_params; if (!json_has(request->impl, key)) { - struct json_object *params = json_object_new_object(); - if (!params) { - json_errno("json_object_new_object"); + struct json_object *params = NULL; + + ret = json_new_object(¶ms); + if (ret < 0) return NULL; - } - int ret = json_set(request->impl, key, params); + + ret = json_set(request->impl, key, params); if (ret < 0) { - json_object_put(params); + json_free(params); return NULL; } return params; } struct json_object *params = NULL; - int ret = json_get(request->impl, key, ¶ms); + ret = json_get(request->impl, key, ¶ms); if (ret < 0) return NULL; return params; @@ -409,12 +408,9 @@ int jsonrpc_response_create_internal(struct jsonrpc_response **_response, goto exit; } - response->impl = json_object_new_object(); - if (!response->impl) { - json_errno("json_object_new_object"); - ret = -1; + ret = json_new_object(&response->impl); + if (ret < 0) goto free; - } ret = jsonrpc_set_version(response->impl); if (ret < 0) @@ -427,7 +423,7 @@ int jsonrpc_response_create_internal(struct jsonrpc_response **_response, ret = json_set(response->impl, jsonrpc_key_id, id); if (ret < 0) { - json_object_put(id); + json_free(id); goto free_impl; } @@ -445,7 +441,7 @@ int jsonrpc_response_create_internal(struct jsonrpc_response **_response, goto exit; free_impl: - json_object_put(response->impl); + json_free(response->impl); free: free(response); exit: @@ -460,20 +456,19 @@ int jsonrpc_response_create(struct jsonrpc_response **response, void jsonrpc_response_destroy(struct jsonrpc_response *response) { - json_object_put(response->impl); + json_free(response->impl); free(response); } int jsonrpc_error_create(struct jsonrpc_response **response, struct jsonrpc_request *request, int code, const char *message) { - struct json_object *error = json_object_new_object(); - if (!error) { - json_errno("json_object_new_object"); - return -1; - } - int ret = 0; + struct json_object *error = NULL; + + ret = json_new_object(&error); + if (ret < 0) + return ret; ret = json_set_int_const_key(error, jsonrpc_key_code, code); if (ret < 0) @@ -489,7 +484,7 @@ int jsonrpc_error_create(struct jsonrpc_response **response, struct jsonrpc_requ return ret; free: - json_object_put(error); + json_free(error); return ret; } @@ -544,7 +539,7 @@ int jsonrpc_response_recv(struct jsonrpc_response **response, int fd) return ret; free_impl: - json_object_put(impl); + json_free(impl); return ret; } diff --git a/src/protocol.c b/src/protocol.c index 106c991..c45b869 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -9,6 +9,7 @@ #include "base64.h" #include "compiler.h" #include "const.h" +#include "json.h" #include "json_rpc.h" #include "process.h" #include "run_queue.h" @@ -218,7 +219,7 @@ int response_create_get_runs(struct jsonrpc_response **response, return ret; free_json: - json_object_put(runs_json); + json_free(runs_json); return ret; } diff --git a/src/run_queue.c b/src/run_queue.c index a1e286b..4374797 100644 --- a/src/run_queue.c +++ b/src/run_queue.c @@ -107,7 +107,7 @@ int run_to_json(const struct run *entry, struct json_object **_json) return ret; free: - json_object_put(json); + json_free(json); return ret; } @@ -167,7 +167,7 @@ int run_queue_to_json(const struct run_queue *queue, struct json_object **_json) ret = json_append(json, entry_json); if (ret < 0) { - json_object_put(entry_json); + json_free(entry_json); goto free; } } @@ -176,7 +176,7 @@ int run_queue_to_json(const struct run_queue *queue, struct json_object **_json) return ret; free: - json_object_put(json); + json_free(json); return ret; } -- cgit v1.2.3