From 9391049ffcc93682ee23125bb8d5b8f915fe128b Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 12 Nov 2023 03:16:39 +0100 Subject: client: print the server response --- src/client.c | 12 ++++++++---- src/json.c | 14 ++++++++++++-- src/json.h | 1 + src/json_rpc.c | 5 +++++ src/json_rpc.h | 2 ++ 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/client.c b/src/client.c index eda4374..1594014 100644 --- a/src/client.c +++ b/src/client.c @@ -91,11 +91,15 @@ int client_main(UNUSED const struct client *client, const struct settings *setti if (ret < 0) goto close; - if (jsonrpc_response_is_error(response)) { - log_err("server failed to process the request\n"); - ret = -1; - goto free_response; + const char *response_str = jsonrpc_response_to_string(response); + if (response_str) { + if (jsonrpc_response_is_error(response)) + ret = -1; + printf("%s", response_str); + } else { + log_err("no response\n"); } + goto free_response; free_response: jsonrpc_response_destroy(response); diff --git a/src/json.c b/src/json.c index 10b34a9..2909c74 100644 --- a/src/json.c +++ b/src/json.c @@ -17,9 +17,9 @@ #include #include -const char *json_to_string(struct json_object *obj) +static const char *json_to_string_internal(struct json_object *obj, int flags) { - const char *result = json_object_to_json_string(obj); + const char *result = json_object_to_json_string_ext(obj, flags); if (!result) { json_errno("json_object_to_json_string"); return NULL; @@ -27,6 +27,16 @@ const char *json_to_string(struct json_object *obj) return result; } +const char *json_to_string(struct json_object *obj) +{ + return json_to_string_internal(obj, JSON_C_TO_STRING_SPACED); +} + +const char *json_to_string_pretty(struct json_object *obj) +{ + return json_to_string_internal(obj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); +} + struct json_object *json_from_string(const char *src) { enum json_tokener_error error; diff --git a/src/json.h b/src/json.h index 6ac30cb..d14fd97 100644 --- a/src/json.h +++ b/src/json.h @@ -20,6 +20,7 @@ } while (0) 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 *); int json_clone(const struct json_object *, const char *key, struct json_object **value); diff --git a/src/json_rpc.c b/src/json_rpc.c index 31794ec..dbeff91 100644 --- a/src/json_rpc.c +++ b/src/json_rpc.c @@ -391,6 +391,11 @@ int jsonrpc_request_set_param_int(struct jsonrpc_request *request, const char *n return json_set_int(params, name, value); } +const char *jsonrpc_response_to_string(const struct jsonrpc_response *response) +{ + return json_to_string_pretty(response->impl); +} + int jsonrpc_response_create_internal(struct jsonrpc_response **_response, const struct jsonrpc_request *request, struct json_object *result, struct json_object *error) diff --git a/src/json_rpc.h b/src/json_rpc.h index 090caca..077b0f5 100644 --- a/src/json_rpc.h +++ b/src/json_rpc.h @@ -39,6 +39,8 @@ int jsonrpc_request_set_param_int(struct jsonrpc_request *, const char *name, in struct jsonrpc_response; +const char *jsonrpc_response_to_string(const struct jsonrpc_response *); + int jsonrpc_response_create(struct jsonrpc_response **, const struct jsonrpc_request *, struct json_object *result); void jsonrpc_response_destroy(struct jsonrpc_response *); -- cgit v1.2.3