json作为通信报文

xiaoxiao2021-02-28  134

通常报文格式一般分为两种,一种是报文类型标识,长度,校验。还有一种是报文类型标识,结束标识,校验。

第一种有利于接收方进行报文解析,应为首先能知道将要接收的报文长度。但是第一类报文的扩展性,灵活性和嵌套性比较差,类似tcpip协议头等传统协议。

第二种报文对于接收方来说比较不好处理,需要不停的判断结束符和校验,但是报文灵活,可以随意嵌套,类似于json和xml语言。

json或者xml其实质是不同类型的对象组成的多叉树,在c语言用的链表数据结构构建。

json new创建的要手动put,put会递归的put一遍子树,get的要手动put,因为会增加引用计数。

json_object_object_add会先删除同key的元素,再添加key的元素。

json_c库的一些对象:

struct json_object {   enum json_type o_type;  //类型   json_object_delete_fn *_delete; //释放函数   json_object_to_json_string_fn *_to_json_string; //json转str函数   int _ref_count; //引用计数   struct printbuf *_pb; // 打印buf   union data {   //真正的数据 联合体     boolean c_boolean; //bool型     double c_double;  //double 型     int64_t c_int64; //int64型     struct lh_table *c_object; //object 型     struct array_list *c_array; //list 型     struct {  //string型         char *str;         int len;     } c_string;   } o; }; //链式结构 struct lh_table { /** * Size of our hash. */ unsigned char size; /** * Numbers of entries. */ unsigned char count; /** * The first entry. */ struct lh_entry *head; /** * The last entry. */ struct lh_entry *tail; struct lh_entry *table; /** * A pointer onto the function responsible for freeing an entry. */ lh_entry_free_fn *free_fn; lh_hash_fn *hash_fn; lh_equal_fn *equal_fn; }; 双向链表 struct lh_entry { /** * The key. */ void *k; /** * The value. */ const void *v; /** * The next entry */ struct lh_entry *next; /** * The previous entry. */ struct lh_entry *prev; };
转载请注明原文地址: https://www.6miu.com/read-17685.html

最新回复(0)