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

Commit

Permalink
feat(serialize): Add send_mam_res_deserialize()
Browse files Browse the repository at this point in the history
Use send_mam_res_deserialize() to deserialize json_result
which is answered from api_receive_mam_message().
  • Loading branch information
howjmay committed Apr 17, 2019
1 parent 9bb4e75 commit c8f238d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
62 changes: 56 additions & 6 deletions serializer/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ void fill_tag(char* new_tag, char* old_tag, size_t tag_len) {
sprintf(new_tag, "%s%*.*s", old_tag, pad_len, pad_len, nines);
}

status_t ta_hash243_stack_to_json_array(hash243_stack_t stack,
cJSON* const json_root,
char const* const obj_name) {
static status_t ta_hash243_stack_to_json_array(hash243_stack_t stack,
cJSON* const json_root,
char const* const obj_name) {
size_t array_count = 0;
cJSON* array_obj = NULL;
hash243_stack_entry_t* s_iter = NULL;
Expand Down Expand Up @@ -41,9 +41,9 @@ status_t ta_hash243_stack_to_json_array(hash243_stack_t stack,
return SC_OK;
}

status_t ta_hash243_queue_to_json_array(hash243_queue_t queue,
cJSON* const json_root,
char const* const obj_name) {
static status_t ta_hash243_queue_to_json_array(hash243_queue_t queue,
cJSON* const json_root,
char const* const obj_name) {
size_t array_count;
cJSON* array_obj = NULL;
hash243_queue_entry_t* q_iter = NULL;
Expand Down Expand Up @@ -75,6 +75,25 @@ status_t ta_hash243_queue_to_json_array(hash243_queue_t queue,
return SC_OK;
}

static status_t ta_json_get_string(cJSON const* const json_obj,
char const* const obj_name,
char* const text) {
retcode_t ret = SC_OK;

cJSON* json_value = cJSON_GetObjectItemCaseSensitive(json_obj, obj_name);
if (json_value == NULL) {
return RC_CCLIENT_JSON_KEY;
}

if (cJSON_IsString(json_value) && (json_value->valuestring != NULL)) {
strcpy(text, json_value->valuestring);
} else {
return RC_CCLIENT_JSON_PARSE;
}

return ret;
}

status_t iota_transaction_to_json_object(iota_transaction_t const* const txn,
cJSON** txn_json) {
if (txn == NULL) {
Expand Down Expand Up @@ -431,6 +450,37 @@ status_t send_mam_res_serialize(char** obj, const send_mam_res_t* const res) {
return ret;
}

status_t send_mam_res_deserialize(const char* const obj,
send_mam_res_t* const res) {
if (obj == NULL) {
return SC_SERIALIZER_NULL;
}
cJSON* json_obj = cJSON_Parse(obj);
status_t ret = SC_OK;
tryte_t addr[NUM_TRYTES_ADDRESS + 1];

if (json_obj == NULL) {
ret = SC_SERIALIZER_JSON_PARSE;
goto done;
}

if (ta_json_get_string(json_obj, "channel", (char*)addr) != SC_OK) {
ret = SC_SERIALIZER_NULL;
goto done;
}
send_mam_res_set_channel_id(res, addr);

if (ta_json_get_string(json_obj, "bundle_hash", (char*)addr) != SC_OK) {
ret = SC_SERIALIZER_NULL;
goto done;
}
send_mam_res_set_bundle_hash(res, addr);

done:
cJSON_Delete(json_obj);
return ret;
}

status_t send_mam_req_deserialize(const char* const obj, send_mam_req_t* req) {
if (obj == NULL) {
return SC_SERIALIZER_NULL;
Expand Down
13 changes: 13 additions & 0 deletions serializer/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ status_t receive_mam_message_serialize(char** obj, const char** res);
*/
status_t send_mam_res_serialize(char** obj, const send_mam_res_t* const res);

/**
* @brief Deserialze JSON string to type of send_mam_res_t
*
* @param[in] obj Input values in JSON
* @param[out] res Response data in type of send_mam_res_t
*
* @return
* - SC_OK on success
* - non-zero on error
*/
status_t send_mam_res_deserialize(const char* const obj,
send_mam_res_t* const res);

/**
* @brief Deserialze JSON string to type of send_mam_req_t
*
Expand Down
15 changes: 15 additions & 0 deletions tests/test_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@ void test_serialize_send_mam_message(void) {
send_mam_res_free(&res);
}

void test_deserialize_send_mam_message_response(void) {
const char* json = "{\"channel\":\"" TRYTES_81_1
"\","
"\"bundle_hash\":\"" TRYTES_81_2 "\"}";
send_mam_res_t* res = send_mam_res_new();

send_mam_res_deserialize(json, res);

TEST_ASSERT_EQUAL_STRING(TRYTES_81_1, res->channel_id);
TEST_ASSERT_EQUAL_STRING(TRYTES_81_2, res->bundle_hash);

send_mam_res_free(&res);
}

void test_deserialize_send_mam_message(void) {
const char* json = "{\"message\":\"" TEST_PAYLOAD "\"}";
send_mam_req_t* req = send_mam_req_new();
Expand Down Expand Up @@ -296,6 +310,7 @@ int main(void) {
RUN_TEST(test_serialize_ta_find_transactions_by_tag);
RUN_TEST(test_serialize_ta_find_transactions_obj_by_tag);
RUN_TEST(test_serialize_send_mam_message);
RUN_TEST(test_deserialize_send_mam_message_response);
RUN_TEST(test_deserialize_send_mam_message);
return UNITY_END();
}

0 comments on commit c8f238d

Please sign in to comment.