#ifndef _JSON_PACKET_H_INCLUDED_ #define _JSON_PACKET_H_INCLUDED_ #include #include "cJSON.h" #include "iobuffer.h" /** * inout: a input/output arguments, if return NO_REPLY the output is ignored. */ /* * simple json function packet * 简单的json pack */ typedef struct{ uint16_t cap; /*capacity*/ char *buf; char *pos; }json_simple; json_simple json_simple_init(char *pos, uint16_t cap); /* ** return NG or OK **/ uint32_t json_simple_add_int(json_simple *json, char *key, int32_t val); uint32_t json_simple_add_str(json_simple *json, char *key, char *val); /*json_simple_tostring ** no need to free **/ const char *json_simple_tostring(json_simple *json); const uint32_t json_simple_len(json_simple *json); /* 使用例子 char buf[256]; json_simple json = json_simple_init(buf, sizeof(buf)); json_simple_add_int(&json, "cmd", 0x11); json_simple_add_str(&json, "uuid", "my_uuid"); char *str = json_simple_tostring(&json); **/ /* * Json function packet */ typedef struct{ uint16_t cmd; //value of cmd_sign, max 65535 int16_t ret; ///NO_REPLY this packet will not be sent back. cJSON *json; }json_pkt; uint8_t json_pkt_parse(json_pkt *packet, const char *str, const char *cmd_sign); //func according to funcMark. #define json_pkt_deinit json_pkt_reset//also deinit, make the mjson, func NULL and mPacketBuffer.size = 0 void json_pkt_reset(json_pkt *packet); //also deinit, make the mjson, func NULL and mPacketBuffer.size = 0 void json_pkt_setcmd(json_pkt *packet, const uint16_t cmd); //str will be strdup //the following functions will act to mJson void json_pkt_add_int(json_pkt *packet, const char *key, const int val); void json_pkt_add_str(json_pkt *packet, const char *key, const char *val); void json_pkt_add_item(json_pkt *packet, const char *key, cJSON *val); void json_pkt_add_item_int(json_pkt *packet, const char *item, const char *key, const int val); void json_pkt_add_item_str(json_pkt *packet, const char *item, const char *key, const char *val); void json_pkt_remove(json_pkt *packet, const char *key); cJSON *json_pkt_detach(json_pkt *packet, const char *key); cJSON *json_pkt_get_item(json_pkt *packet, const char *key); const char *json_pkt_get_string(json_pkt *packet, const char *key, const char *defaultVal); int json_pkt_get_int(json_pkt *packet, const char *key, const int defaultVal); uint8_t json_pkt_get_bool(json_pkt *packet, const char *key,const uint8_t defaultVal); float json_pkt_get_float(json_pkt *packet, const char *key, const double defaultVal); int json_pkt_get_item_int(json_pkt *packet, cJSON * obj, const char *key, const int defaultVal); int json_pkt_get_intArray(json_pkt *packet, const char *key, int *array, const int size); //convert current mJson object to string and append to mPacketBuffer, and do not call twice. free it. char* json_pkt_toJson_string(json_pkt *packet); #endif