compact formatting of json
[src/app-framework-binder.git] / src / afb-hreq.c
index 35b90aa..ab174e2 100644 (file)
@@ -611,6 +611,7 @@ struct afb_req afb_hreq_to_req(struct afb_hreq *hreq)
 
 static struct afb_arg req_get(struct afb_hreq *hreq, const char *name)
 {
+       const char *value;
        struct hreq_data *hdat = get_data(hreq, name, 0);
        if (hdat)
                return (struct afb_arg){
@@ -618,10 +619,11 @@ static struct afb_arg req_get(struct afb_hreq *hreq, const char *name)
                        .value = hdat->value,
                        .path = hdat->path
                };
-               
+
+       value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
        return (struct afb_arg){
-               .name = name,
-               .value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name),
+               .name = value == NULL ? NULL : name,
+               .value = value,
                .path = NULL
        };
 }
@@ -675,7 +677,7 @@ static void req_send(struct afb_hreq *hreq, const char *buffer, size_t size)
 
 static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t max)
 {
-       ssize_t len = stpncpy(buf, json_object_to_json_string(obj)+pos, max) - buf;
+       ssize_t len = stpncpy(buf, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN)+pos, max) - buf;
        return len ? : (ssize_t)MHD_CONTENT_READER_END_OF_STREAM;
 }
 
@@ -691,16 +693,12 @@ static void req_reply(struct afb_hreq *hreq, unsigned retcode, const char *statu
        reply = afb_msg_json_reply(status, info, resp, token, uuid);
 
        reqid = afb_hreq_get_argument(hreq, long_key_for_reqid);
-       if (reqid != NULL && json_object_object_get_ex(reply, "request", &request)) {
-               json_object_object_add (request, long_key_for_reqid, json_object_new_string(reqid));
-       } else {
+       if (reqid == NULL)
                reqid = afb_hreq_get_argument(hreq, short_key_for_reqid);
-               if (reqid != NULL && json_object_object_get_ex(reply, "request", &request)) {
-                       json_object_object_add (request, short_key_for_reqid, json_object_new_string(reqid));
-               }
-       }
+       if (reqid != NULL && json_object_object_get_ex(reply, "request", &request))
+               json_object_object_add (request, short_key_for_reqid, json_object_new_string(reqid));
 
-       response = MHD_create_response_from_callback((uint64_t)strlen(json_object_to_json_string(reply)), SIZE_RESPONSE_BUFFER, (void*)send_json_cb, reply, (void*)json_object_put);
+       response = MHD_create_response_from_callback((uint64_t)strlen(json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PLAIN)), SIZE_RESPONSE_BUFFER, (void*)send_json_cb, reply, (void*)json_object_put);
        afb_hreq_reply(hreq, retcode, response, NULL);
 }