/********************************************************************************* * Copyright: (C) 2023 LingYun IoT System Studio. * All rights reserved. * * Filename: pack_json.c * Description: This file is JSON format string package and parser example * * Version: 1.0.0(2023年07月10日) * Author: Guo Wenxue * ChangeLog: 1, Release initial version on "2023年07月10日 17时57分00秒" * ********************************************************************************/ #include #include #include #include #define DEVSN_LEN 11 typedef struct data_s { char devsn[DEVSN_LEN+1]; char time[32]; float temp; float rh; } pack_t; int pack_json(char *buf, int size, char *devsn, char *time, float temp, float rh); int unpack_json(char *buf, int len, pack_t *pack); int main (int argc, char **argv) { char buf[512]; char *devsn="ISK1023001"; char *time="2023-07-10 20:00:00"; float temp = 20.50; float rh = 30.50; pack_t data; int rv; if( (rv=pack_json(buf, sizeof(buf), devsn, time, temp, rh)) < 0 ) { printf("Package sample data message failed, rv=%d\n", rv); return rv; } if( (rv=unpack_json(buf, rv, &data)) < 0 ) { printf("Parser sample data message failed, rv=%d\n", rv); return rv; } return 0; } int pack_json(char *buf, int size, char *devsn, char *time, float temp, float rh) { if( !buf || size<=0 || !devsn || !time ) { printf("\033[1;31m%s:%d %s() Invalid input arguments\033[0m\n", __FILE__, __LINE__, __func__); return -1; } memset(buf, 0, size); snprintf(buf, size, "{\"devsn\":\"%s\", \"time\":\"%s\", \"temperature\":%.2f, \"humidity\":%.2f}", devsn, time, temp, rh); printf("\033[1;32mPacket Message: %s\033[0m\n", buf); return strlen(buf); } int unpack_json(char *buf, int len, pack_t *pack) { cJSON *root = NULL; cJSON *item; char *value; int bytes; int rv = 0; if( !buf || len<=0 || !pack ) { printf("\033[1;31m%s:%d %s() Invalid input arguments\033[0m\n", __FILE__, __LINE__, __func__); return -1; } printf("\033[1;33mParser Message: %s\033[0m\n", buf); root = cJSON_Parse(buf); if( !root ) { printf("cJSON_Parse root failure: %s\n", cJSON_GetErrorPtr()); return -2; } memset(pack, 0, sizeof(*pack)); /*+--------------------------+ *| Start parser SN segement | *+--------------------------+*/ item = cJSON_GetObjectItem(root, "devsn"); if( !item ) { printf("cJSON_GetObjectItem() item failure: %s\n", cJSON_GetErrorPtr()); rv = -3; goto cleanup; } bytes = strlen(item->valuestring); bytes = bytes>sizeof(pack->devsn) ? sizeof(pack->devsn) : bytes; strncpy(pack->devsn, item->valuestring, bytes); printf("Found devsn : %s\n", pack->devsn); /*+----------------------------+ *| Start parser Time segement | *+----------------------------+*/ item = cJSON_GetObjectItem(root, "time"); if( !item ) { printf("cJSON_GetObjectItem() item failure: %s\n", cJSON_GetErrorPtr()); rv = -4; goto cleanup; } bytes = strlen(item->valuestring); bytes = bytes>sizeof(pack->time) ? sizeof(pack->time) : bytes; strncpy(pack->time, item->valuestring, bytes); printf("Found time : %s\n", pack->time); /*+--------------------------+ *| Start parser temperature | *+--------------------------+*/ item = cJSON_GetObjectItem(root, "temperature"); if( !item ) { printf("cJSON_GetObjectItem() item failure: %s\n", cJSON_GetErrorPtr()); rv = -5; goto cleanup; } pack->temp = item->valuedouble; printf("Found temperature: %.2f\n", pack->temp); /*+-----------------------+ *| Start parser humidity | *+-----------------------+*/ item = cJSON_GetObjectItem(root, "humidity"); if( !item ) { printf("cJSON_GetObjectItem() item failure: %s\n", cJSON_GetErrorPtr()); rv = -6; goto cleanup; } pack->rh = item->valuedouble; printf("Found humidity : %.2f\n", pack->rh); cleanup: cJSON_Delete(root); /* must delete it, or it will result memory leak */ return rv; }