int main(void) { struct json *reply, *item1, *item2, *params; struct jrpc_client *client = &my_client; int ret = jrpc_client_init(client, HOST, PORT); if (ret != 0) { exit(ret); } item1 = json_create_object(); json_add_number_to_object(item1, "A", 3); json_add_number_to_object(item1, "B", 10); item2 = json_create_object(); json_add_number_to_object(item2, "A", 1); json_add_number_to_object(item2, "B", 2); params = json_create_array(); json_add_item_to_object(item1,"S", item2); json_add_item_to_array(params, item1); // jrpc_client_call will free params if ((ret = jrpc_client_call(client, "foo", params, &reply)) != 0) { exit(ret); } printf("%s\n", json_to_string(reply)); json_delete(reply); jrpc_client_close(client); return 0; }
void json_add_item_to_object(struct json *object, const char *string, struct json *item) { if (!item) return; if (item->string) json_free(item->string); item->string = json_strdup(string); json_add_item_to_array(object, item); }
static struct json *foo(struct jrpc_context * ctx, struct json *params, struct json *id) { struct json *reply, *item, *array; int a, b, i; char buf[1024]; json_dump(params); json_dump(id); item = json_get_object_item(params->child, "A"); a = item->valueint; item = json_get_object_item(params->child, "B"); b = item->valueint; sprintf(buf, "recv a:%d b:%d", a, b); array = json_create_array(); for(i = 0; i < a; i ++){ item = json_create_object(); json_add_number_to_object(item, "A", i); json_add_number_to_object(item, "B", b++); json_add_item_to_array(array, item); } reply = json_create_object(); json_add_item_to_object(reply, "Args", array); json_add_string_to_object(reply, "Str", buf); json_dump(reply); return reply; }
void json_add_item_reference_to_array(struct json *array, struct json *item) { json_add_item_to_array(array, create_reference(item)); }