aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2023-11-12 03:16:39 +0100
committerEgor Tensin <Egor.Tensin@gmail.com>2023-11-12 03:16:39 +0100
commit9391049ffcc93682ee23125bb8d5b8f915fe128b (patch)
treeeadf53106a13cabc63421bd9026ed2557736956f
parentjson_rpc: add a routine to generate request IDs (diff)
downloadcimple-9391049ffcc93682ee23125bb8d5b8f915fe128b.tar.gz
cimple-9391049ffcc93682ee23125bb8d5b8f915fe128b.zip
client: print the server response
-rw-r--r--src/client.c12
-rw-r--r--src/json.c14
-rw-r--r--src/json.h1
-rw-r--r--src/json_rpc.c5
-rw-r--r--src/json_rpc.h2
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 <stdlib.h>
#include <string.h>
-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 *);