| 186 | * ══════════════════════════════════════════════════════════════════ */ |
| 187 | |
| 188 | char *cbm_jsonrpc_format_response(const cbm_jsonrpc_response_t *resp) { |
| 189 | yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); |
| 190 | yyjson_mut_val *root = yyjson_mut_obj(doc); |
| 191 | yyjson_mut_doc_set_root(doc, root); |
| 192 | |
| 193 | yyjson_mut_obj_add_str(doc, root, "jsonrpc", "2.0"); |
| 194 | if (resp->id_str) { |
| 195 | yyjson_mut_obj_add_str(doc, root, "id", resp->id_str); |
| 196 | } else { |
| 197 | yyjson_mut_obj_add_int(doc, root, "id", resp->id); |
| 198 | } |
| 199 | |
| 200 | if (resp->error_json) { |
| 201 | /* Parse the error JSON and embed */ |
| 202 | yyjson_doc *err_doc = yyjson_read(resp->error_json, strlen(resp->error_json), 0); |
| 203 | if (err_doc) { |
| 204 | yyjson_mut_val *err_val = yyjson_val_mut_copy(doc, yyjson_doc_get_root(err_doc)); |
| 205 | yyjson_mut_obj_add_val(doc, root, "error", err_val); |
| 206 | yyjson_doc_free(err_doc); |
| 207 | } |
| 208 | } else if (resp->result_json) { |
| 209 | /* Parse the result JSON and embed */ |
| 210 | yyjson_doc *res_doc = yyjson_read(resp->result_json, strlen(resp->result_json), 0); |
| 211 | if (res_doc) { |
| 212 | yyjson_mut_val *res_val = yyjson_val_mut_copy(doc, yyjson_doc_get_root(res_doc)); |
| 213 | yyjson_mut_obj_add_val(doc, root, "result", res_val); |
| 214 | yyjson_doc_free(res_doc); |
| 215 | } |
| 216 | } else { |
| 217 | /* JSON-RPC 2.0 spec: response MUST contain "result" or "error" */ |
| 218 | yyjson_mut_obj_add_null(doc, root, "result"); |
| 219 | } |
| 220 | |
| 221 | char *out = yy_doc_to_str(doc); |
| 222 | yyjson_mut_doc_free(doc); |
| 223 | return out; |
| 224 | } |
| 225 | |
| 226 | char *cbm_jsonrpc_format_error(int64_t id, int code, const char *message) { |
| 227 | yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); |
no test coverage detected