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

Commit c8f238d

Browse files
committed
feat(serialize): Add send_mam_res_deserialize()
Use send_mam_res_deserialize() to deserialize json_result which is answered from api_receive_mam_message().
1 parent 9bb4e75 commit c8f238d

3 files changed

Lines changed: 84 additions & 6 deletions

File tree

serializer/serializer.c

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ void fill_tag(char* new_tag, char* old_tag, size_t tag_len) {
66
sprintf(new_tag, "%s%*.*s", old_tag, pad_len, pad_len, nines);
77
}
88

9-
status_t ta_hash243_stack_to_json_array(hash243_stack_t stack,
10-
cJSON* const json_root,
11-
char const* const obj_name) {
9+
static status_t ta_hash243_stack_to_json_array(hash243_stack_t stack,
10+
cJSON* const json_root,
11+
char const* const obj_name) {
1212
size_t array_count = 0;
1313
cJSON* array_obj = NULL;
1414
hash243_stack_entry_t* s_iter = NULL;
@@ -41,9 +41,9 @@ status_t ta_hash243_stack_to_json_array(hash243_stack_t stack,
4141
return SC_OK;
4242
}
4343

44-
status_t ta_hash243_queue_to_json_array(hash243_queue_t queue,
45-
cJSON* const json_root,
46-
char const* const obj_name) {
44+
static status_t ta_hash243_queue_to_json_array(hash243_queue_t queue,
45+
cJSON* const json_root,
46+
char const* const obj_name) {
4747
size_t array_count;
4848
cJSON* array_obj = NULL;
4949
hash243_queue_entry_t* q_iter = NULL;
@@ -75,6 +75,25 @@ status_t ta_hash243_queue_to_json_array(hash243_queue_t queue,
7575
return SC_OK;
7676
}
7777

78+
static status_t ta_json_get_string(cJSON const* const json_obj,
79+
char const* const obj_name,
80+
char* const text) {
81+
retcode_t ret = SC_OK;
82+
83+
cJSON* json_value = cJSON_GetObjectItemCaseSensitive(json_obj, obj_name);
84+
if (json_value == NULL) {
85+
return RC_CCLIENT_JSON_KEY;
86+
}
87+
88+
if (cJSON_IsString(json_value) && (json_value->valuestring != NULL)) {
89+
strcpy(text, json_value->valuestring);
90+
} else {
91+
return RC_CCLIENT_JSON_PARSE;
92+
}
93+
94+
return ret;
95+
}
96+
7897
status_t iota_transaction_to_json_object(iota_transaction_t const* const txn,
7998
cJSON** txn_json) {
8099
if (txn == NULL) {
@@ -431,6 +450,37 @@ status_t send_mam_res_serialize(char** obj, const send_mam_res_t* const res) {
431450
return ret;
432451
}
433452

453+
status_t send_mam_res_deserialize(const char* const obj,
454+
send_mam_res_t* const res) {
455+
if (obj == NULL) {
456+
return SC_SERIALIZER_NULL;
457+
}
458+
cJSON* json_obj = cJSON_Parse(obj);
459+
status_t ret = SC_OK;
460+
tryte_t addr[NUM_TRYTES_ADDRESS + 1];
461+
462+
if (json_obj == NULL) {
463+
ret = SC_SERIALIZER_JSON_PARSE;
464+
goto done;
465+
}
466+
467+
if (ta_json_get_string(json_obj, "channel", (char*)addr) != SC_OK) {
468+
ret = SC_SERIALIZER_NULL;
469+
goto done;
470+
}
471+
send_mam_res_set_channel_id(res, addr);
472+
473+
if (ta_json_get_string(json_obj, "bundle_hash", (char*)addr) != SC_OK) {
474+
ret = SC_SERIALIZER_NULL;
475+
goto done;
476+
}
477+
send_mam_res_set_bundle_hash(res, addr);
478+
479+
done:
480+
cJSON_Delete(json_obj);
481+
return ret;
482+
}
483+
434484
status_t send_mam_req_deserialize(const char* const obj, send_mam_req_t* req) {
435485
if (obj == NULL) {
436486
return SC_SERIALIZER_NULL;

serializer/serializer.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,19 @@ status_t receive_mam_message_serialize(char** obj, const char** res);
136136
*/
137137
status_t send_mam_res_serialize(char** obj, const send_mam_res_t* const res);
138138

139+
/**
140+
* @brief Deserialze JSON string to type of send_mam_res_t
141+
*
142+
* @param[in] obj Input values in JSON
143+
* @param[out] res Response data in type of send_mam_res_t
144+
*
145+
* @return
146+
* - SC_OK on success
147+
* - non-zero on error
148+
*/
149+
status_t send_mam_res_deserialize(const char* const obj,
150+
send_mam_res_t* const res);
151+
139152
/**
140153
* @brief Deserialze JSON string to type of send_mam_req_t
141154
*

tests/test_serializer.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,20 @@ void test_serialize_send_mam_message(void) {
269269
send_mam_res_free(&res);
270270
}
271271

272+
void test_deserialize_send_mam_message_response(void) {
273+
const char* json = "{\"channel\":\"" TRYTES_81_1
274+
"\","
275+
"\"bundle_hash\":\"" TRYTES_81_2 "\"}";
276+
send_mam_res_t* res = send_mam_res_new();
277+
278+
send_mam_res_deserialize(json, res);
279+
280+
TEST_ASSERT_EQUAL_STRING(TRYTES_81_1, res->channel_id);
281+
TEST_ASSERT_EQUAL_STRING(TRYTES_81_2, res->bundle_hash);
282+
283+
send_mam_res_free(&res);
284+
}
285+
272286
void test_deserialize_send_mam_message(void) {
273287
const char* json = "{\"message\":\"" TEST_PAYLOAD "\"}";
274288
send_mam_req_t* req = send_mam_req_new();
@@ -296,6 +310,7 @@ int main(void) {
296310
RUN_TEST(test_serialize_ta_find_transactions_by_tag);
297311
RUN_TEST(test_serialize_ta_find_transactions_obj_by_tag);
298312
RUN_TEST(test_serialize_send_mam_message);
313+
RUN_TEST(test_deserialize_send_mam_message_response);
299314
RUN_TEST(test_deserialize_send_mam_message);
300315
return UNITY_END();
301316
}

0 commit comments

Comments
 (0)