static void json_echo(struct command *cmd, const char *buffer, const jsmntok_t *params) { struct json_result *response = new_json_result(cmd); json_object_start(response, NULL); json_add_num(response, "num", params->size); json_add_literal(response, "echo", json_tok_contents(buffer, params), json_tok_len(params)); json_object_end(response); command_success(cmd, response); }
void json_add_object(struct json_result *result, ...) { va_list ap; const char *field; va_start(ap, result); json_object_start(result, NULL); while ((field = va_arg(ap, const char *)) != NULL) { jsmntype_t type = va_arg(ap, jsmntype_t); const char *value = va_arg(ap, const char *); if (type == JSMN_STRING) json_add_string(result, field, value); else json_add_literal(result, field, value, strlen(value)); } json_object_end(result); va_end(ap); }