Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 0c66fb8

Browse files
committed
feat(api): Refactor response JSON format
1 parent 01630e7 commit 0c66fb8

File tree

2 files changed

+20
-46
lines changed

2 files changed

+20
-46
lines changed

serializer/serializer.c

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,19 @@ int serializer_logger_release() {
2525
return 0;
2626
}
2727

28-
static status_t ta_hash243_stack_to_json_array(hash243_stack_t stack, cJSON* const json_root,
29-
char const* const obj_name) {
28+
static status_t ta_hash243_stack_to_json_array(hash243_stack_t stack, cJSON* json_root) {
3029
size_t array_count = 0;
31-
cJSON* array_obj = NULL;
3230
hash243_stack_entry_t* s_iter = NULL;
3331
tryte_t trytes_out[NUM_TRYTES_HASH + 1];
3432
size_t trits_count = 0;
3533

3634
array_count = hash243_stack_count(stack);
3735
if (array_count > 0) {
38-
array_obj = cJSON_CreateArray();
39-
if (array_obj == NULL) {
40-
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_SERIALIZER_JSON_CREATE");
41-
return SC_SERIALIZER_JSON_CREATE;
42-
}
43-
cJSON_AddItemToObject(json_root, obj_name, array_obj);
44-
4536
LL_FOREACH(stack, s_iter) {
4637
trits_count = flex_trits_to_trytes(trytes_out, NUM_TRYTES_HASH, s_iter->hash, NUM_TRITS_HASH, NUM_TRITS_HASH);
4738
trytes_out[NUM_TRYTES_HASH] = '\0';
4839
if (trits_count != 0) {
49-
cJSON_AddItemToArray(array_obj, cJSON_CreateString((const char*)trytes_out));
40+
cJSON_AddItemToArray(json_root, cJSON_CreateString((const char*)trytes_out));
5041
} else {
5142
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_CCLIENT_INVALID_FLEX_TRITS");
5243
return SC_CCLIENT_INVALID_FLEX_TRITS;
@@ -59,28 +50,19 @@ static status_t ta_hash243_stack_to_json_array(hash243_stack_t stack, cJSON* con
5950
return SC_OK;
6051
}
6152

62-
static status_t ta_hash243_queue_to_json_array(hash243_queue_t queue, cJSON* const json_root,
63-
char const* const obj_name) {
53+
static status_t ta_hash243_queue_to_json_array(hash243_queue_t queue, cJSON* const json_root) {
6454
size_t array_count;
65-
cJSON* array_obj = NULL;
6655
hash243_queue_entry_t* q_iter = NULL;
6756

6857
array_count = hash243_queue_count(queue);
6958
if (array_count > 0) {
70-
array_obj = cJSON_CreateArray();
71-
if (array_obj == NULL) {
72-
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_SERIALIZER_JSON_CREATE");
73-
return SC_SERIALIZER_JSON_CREATE;
74-
}
75-
cJSON_AddItemToObject(json_root, obj_name, array_obj);
76-
7759
CDL_FOREACH(queue, q_iter) {
7860
tryte_t trytes_out[NUM_TRYTES_HASH + 1];
7961
size_t trits_count =
8062
flex_trits_to_trytes(trytes_out, NUM_TRYTES_HASH, q_iter->hash, NUM_TRITS_HASH, NUM_TRITS_HASH);
8163
trytes_out[NUM_TRYTES_HASH] = '\0';
8264
if (trits_count != 0) {
83-
cJSON_AddItemToArray(array_obj, cJSON_CreateString((const char*)trytes_out));
65+
cJSON_AddItemToArray(json_root, cJSON_CreateString((const char*)trytes_out));
8466
} else {
8567
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_CCLIENT_INVALID_FLEX_TRITS");
8668
return SC_CCLIENT_INVALID_FLEX_TRITS;
@@ -254,38 +236,30 @@ status_t iota_transaction_to_json_object(iota_transaction_t const* const txn, cJ
254236
return SC_OK;
255237
}
256238

257-
static status_t transaction_array_to_json_array(cJSON* json_root, char* obj_name,
258-
const transaction_array_t* const txn_array) {
239+
static status_t transaction_array_to_json_array(cJSON* json_root, const transaction_array_t* const txn_array) {
259240
status_t ret = SC_OK;
260241
iota_transaction_t* txn = NULL;
261242
cJSON* txn_obj = NULL;
262-
cJSON* array_obj = cJSON_CreateArray();
263-
264-
if (array_obj == NULL) {
265-
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_SERIALIZER_JSON_CREATE");
266-
return SC_SERIALIZER_JSON_CREATE;
267-
}
268-
cJSON_AddItemToObject(json_root, obj_name, array_obj);
269243

270244
TX_OBJS_FOREACH(txn_array, txn) {
271245
txn_obj = NULL;
272246
ret = iota_transaction_to_json_object(txn, &txn_obj);
273247
if (ret != SC_OK) {
274248
return ret;
275249
}
276-
cJSON_AddItemToArray(array_obj, txn_obj);
250+
cJSON_AddItemToArray(json_root, txn_obj);
277251
}
278252
return ret;
279253
}
280254

281255
status_t ta_generate_address_res_serialize(char** obj, const ta_generate_address_res_t* const res) {
282-
cJSON* json_root = cJSON_CreateObject();
256+
cJSON* json_root = cJSON_CreateArray();
283257
status_t ret = SC_OK;
284258
if (json_root == NULL) {
285259
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_SERIALIZER_JSON_CREATE");
286260
return SC_SERIALIZER_JSON_CREATE;
287261
}
288-
ret = ta_hash243_queue_to_json_array(res->addresses, json_root, "address");
262+
ret = ta_hash243_queue_to_json_array(res->addresses, json_root);
289263
if (ret) {
290264
return ret;
291265
}
@@ -482,13 +456,13 @@ status_t ta_find_transaction_objects_req_deserialize(const char* const obj,
482456

483457
status_t ta_find_transaction_objects_res_serialize(char** obj, const transaction_array_t* const res) {
484458
status_t ret = SC_OK;
485-
cJSON* json_root = cJSON_CreateObject();
459+
cJSON* json_root = cJSON_CreateArray();
486460
if (json_root == NULL) {
487461
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_CCLIENT_JSON_CREATE");
488462
return SC_CCLIENT_JSON_CREATE;
489463
}
490464

491-
ret = transaction_array_to_json_array(json_root, "transactions", res);
465+
ret = transaction_array_to_json_array(json_root, res);
492466
if (ret != SC_OK) {
493467
goto done;
494468
}
@@ -506,14 +480,14 @@ status_t ta_find_transaction_objects_res_serialize(char** obj, const transaction
506480

507481
status_t ta_find_transactions_res_serialize(char** obj, const ta_find_transactions_res_t* const res) {
508482
status_t ret = SC_OK;
509-
cJSON* json_root = cJSON_CreateObject();
483+
cJSON* json_root = cJSON_CreateArray();
510484
if (json_root == NULL) {
511485
ret = SC_SERIALIZER_JSON_CREATE;
512486
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_SERIALIZER_JSON_CREATE");
513487
goto done;
514488
}
515489

516-
ret = ta_hash243_queue_to_json_array(res->hashes, json_root, "hashes");
490+
ret = ta_hash243_queue_to_json_array(res->hashes, json_root);
517491
if (ret) {
518492
goto done;
519493
}
@@ -531,14 +505,14 @@ status_t ta_find_transactions_res_serialize(char** obj, const ta_find_transactio
531505

532506
status_t ta_find_transactions_obj_res_serialize(char** obj, const ta_find_transactions_obj_res_t* const res) {
533507
status_t ret = SC_OK;
534-
cJSON* json_root = cJSON_CreateObject();
508+
cJSON* json_root = cJSON_CreateArray();
535509
if (json_root == NULL) {
536510
ret = SC_SERIALIZER_JSON_CREATE;
537511
log_error(seri_logger_id, "[%s:%d:%s]\n", __func__, __LINE__, "SC_SERIALIZER_JSON_CREATE");
538512
goto done;
539513
}
540514

541-
ret = transaction_array_to_json_array(json_root, "transactions", res->txn_obj);
515+
ret = transaction_array_to_json_array(json_root, res->txn_obj);
542516
if (ret != SC_OK) {
543517
goto done;
544518
}

tests/test_serializer.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "test_define.h"
1111

1212
void test_serialize_ta_generate_address(void) {
13-
const char* json = "{\"address\":[\"" TRYTES_81_1 "\",\"" TRYTES_81_2 "\"]}";
13+
const char* json = "[\"" TRYTES_81_1 "\",\"" TRYTES_81_2 "\"]";
1414
char* json_result;
1515
ta_generate_address_res_t* res = ta_generate_address_res_new();
1616
flex_trit_t hash_trits_1[FLEX_TRIT_SIZE_243], hash_trits_2[FLEX_TRIT_SIZE_243];
@@ -51,7 +51,7 @@ void test_deserialize_ta_send_transfer(void) {
5151

5252
void test_serialize_ta_find_transaction_objects(void) {
5353
const char* json =
54-
"{\"transactions\":[{\"hash\":\"" TRYTES_81_1 "\","
54+
"[{\"hash\":\"" TRYTES_81_1 "\","
5555
"\"signature_and_message_fragment\":\"" TRYTES_2187_1 "\","
5656
"\"address\":\"" TRYTES_81_1 "\",\"value\":" STR(VALUE) ","
5757
"\"obsolete_tag\":\"" TAG_MSG "\",\"timestamp\":" STR(TIMESTAMP) ","
@@ -63,7 +63,7 @@ void test_serialize_ta_find_transaction_objects(void) {
6363
"\"attachment_timestamp\":" STR(TIMESTAMP) ","
6464
"\"attachment_timestamp_lower_bound\":" STR(TIMESTAMP)","
6565
"\"attachment_timestamp_upper_bound\":" STR(TIMESTAMP)","
66-
"\"nonce\":\"" NONCE "\"}]}";
66+
"\"nonce\":\"" NONCE "\"}]";
6767
char* json_result;
6868
flex_trit_t msg_trits[FLEX_TRIT_SIZE_6561], tag_trits[FLEX_TRIT_SIZE_81], hash_trits_1[FLEX_TRIT_SIZE_243],
6969
hash_trits_2[FLEX_TRIT_SIZE_243];
@@ -124,7 +124,7 @@ void test_serialize_ta_find_transaction_objects(void) {
124124
}
125125

126126
void test_serialize_ta_find_transactions_by_tag(void) {
127-
const char* json = "{\"hashes\":[\"" TRYTES_81_1 "\",\"" TRYTES_81_2 "\"]}";
127+
const char* json = "[\"" TRYTES_81_1 "\",\"" TRYTES_81_2 "\"]";
128128
char* json_result;
129129
ta_find_transactions_res_t* res = ta_find_transactions_res_new();
130130
flex_trit_t hash_trits_1[FLEX_TRIT_SIZE_243], hash_trits_2[FLEX_TRIT_SIZE_243];
@@ -143,7 +143,7 @@ void test_serialize_ta_find_transactions_by_tag(void) {
143143

144144
void test_serialize_ta_find_transactions_obj_by_tag(void) {
145145
const char* json =
146-
"{\"transactions\":[{\"hash\":\"" TRYTES_81_1 "\","
146+
"[{\"hash\":\"" TRYTES_81_1 "\","
147147
"\"signature_and_message_fragment\":\"" TRYTES_2187_1 "\","
148148
"\"address\":\"" TRYTES_81_1 "\",\"value\":" STR(VALUE) ","
149149
"\"obsolete_tag\":\"" TAG_MSG "\",\"timestamp\":" STR(TIMESTAMP) ","
@@ -155,7 +155,7 @@ void test_serialize_ta_find_transactions_obj_by_tag(void) {
155155
"\"attachment_timestamp\":" STR(TIMESTAMP) ","
156156
"\"attachment_timestamp_lower_bound\":" STR(TIMESTAMP)","
157157
"\"attachment_timestamp_upper_bound\":" STR(TIMESTAMP)","
158-
"\"nonce\":\"" NONCE "\"}]}";
158+
"\"nonce\":\"" NONCE "\"}]";
159159
char* json_result;
160160
iota_transaction_t* txn = transaction_new();
161161
flex_trit_t msg_trits[FLEX_TRIT_SIZE_6561], tag_trits[FLEX_TRIT_SIZE_81], hash1[FLEX_TRIT_SIZE_243],

0 commit comments

Comments
 (0)