天河高端网站建设在线课堂网站开发
web/
2025/10/1 22:11:16/
文章来源:
天河高端网站建设,在线课堂网站开发,网站的登录界面怎么做,怀柔网站建设推广文章目录 前言add_item_to_object函数是干什么的add_item_to_object代码解析函数实现函数原理解析开头的代码constant_key参数的作用最后的if判断 add_item_to_array函数 总结 前言
在我们的日常编程中#xff0c;JSON已经成为了一种非常常见的数据交换格式。在C语言中#… 文章目录 前言add_item_to_object函数是干什么的add_item_to_object代码解析函数实现函数原理解析开头的代码constant_key参数的作用最后的if判断 add_item_to_array函数 总结 前言
在我们的日常编程中JSON已经成为了一种非常常见的数据交换格式。在C语言中我们通常使用cJSON库来处理JSON数据。cJSON库提供了一系列的函数来创建、解析和操作JSON数据。其中add_item_to_object函数是一个关键的函数它允许我们将一个新的项目添加到一个已存在的JSON对象中。在这篇文章中我们将深入探讨add_item_to_object函数的内部实现。 add_item_to_object函数是干什么的
这个函数用于把一个item添加到一个对象里面 他是cJSON_AddItemToObject和cJSON_AddItemToObjectCS函数的实现
add_item_to_object代码解析
函数实现
static cJSON_bool add_item_to_object(cJSON * const object, const char * const string, cJSON * const item, const internal_hooks * const hooks, const cJSON_bool constant_key)
{char *new_key NULL;int new_type cJSON_Invalid;if ((object NULL) || (string NULL) || (item NULL) || (object item)){return false;}if (constant_key){new_key (char*)cast_away_const(string);new_type item-type | cJSON_StringIsConst;}else{new_key (char*)cJSON_strdup((const unsigned char*)string, hooks);if (new_key NULL){return false;}new_type item-type ~cJSON_StringIsConst;}if (!(item-type cJSON_StringIsConst) (item-string ! NULL)){hooks-deallocate(item-string);}item-string new_key;item-type new_type;return add_item_to_array(object, item);
}函数原理解析
开头的代码
首先他声明了两个变量用于存储key值和该键值对的类型的
char *new_key NULL;
int new_type cJSON_Invalid;紧接着他去判断参数的合法性为了内存的异常安全‘
if ((object NULL) || (string NULL) || (item NULL) || (object item))
{return false;
}constant_key参数的作用
在cJSON库中constant_key参数是一个布尔值它决定了键key是进行深拷贝还是浅拷贝。
如果constant_key为true那么在添加新项目到JSON对象时键key将会进行浅拷贝也就是直接复制指针。这意味着如果原始的键字符串在以后被修改或释放那么存储在JSON对象中的键也会受到影响。
如果constant_key为false那么键key将会进行深拷贝也就是复制整个字符串的内容到新的内存位置。这样即使原始的键字符串在以后被修改或释放也不会影响到存储在JSON对象中的键。
可以看到他的浅拷贝和深拷贝是完全不同的
if (constant_key)
{new_key (char*)cast_away_const(string);new_type item-type | cJSON_StringIsConst;
}
else
{new_key (char*)cJSON_strdup((const unsigned char*)string, hooks);if (new_key NULL){return false;}new_type item-type ~cJSON_StringIsConst;
}在浅拷贝直接把参数的key复制到new_key里面然后把new_type赋值成对应的类型 那么他为何要| cJSON_StringIsConst 这行代码中的|操作符是C语言中的位运算符表示按位或操作。cJSON_StringIsConst是一个常量它的值通常为0x200在二进制中表示为10 0000 0000。
当我们执行item-type | cJSON_StringIsConst时实际上是将item-type的值和cJSON_StringIsConst的值进行按位或操作。这样做的目的是为了将item-type的第10位设置为1而不改变其他位的值。
这里cJSON_StringIsConst的作用是标记字符串是否为常量。如果一个字符串被标记为常量那么在cJSON对象被删除时这个字符串就不会被释放。这对于那些指向静态或全局变量的字符串非常有用因为这些字符串不能被释放。
所以new_type item-type | cJSON_StringIsConst;这行代码的作用就是将item-type的值更新为新的类型同时保留了原来的类型信息并标记字符串为常量。
在深拷贝中使用了cJSON_strdup函数复制字符串我们上篇文章已经介绍过 new_type item-type ~cJSON_StringIsConst;类比上面这段代码也就是把这个字符串标记为不是常量在删除的时候可以直接删除
最后的if判断
这段代码的目的是在添加新的键值对到JSON对象时安全地处理旧的键字符串。
if (!(item-type cJSON_StringIsConst) (item-string ! NULL))
{hooks-deallocate(item-string);
}!(item-type cJSON_StringIsConst)这段的含义如下 这部分是检查item-type是否被标记为cJSON_StringIsConst。如果被标记为cJSON_StringIsConst那么这个字符串就是一个常量字符串我们不能释放它。所以我们使用!操作符来检查item-type是否没有被标记为cJSON_StringIsConst。
(item-string ! NULL)这部分是检查item-string是否为NULL。如果item-string为NULL那么我们就没有必要也不能释放它。
最后就把参数item复制上我们的状态就行了
item-string new_key;
item-type new_type;我们会发现这个函数仅仅是把键和type安装到参数type上面但是还没有进行item安装到object上面所以这里又出现一个函数他专门用于链表的操作的add_item_to_array
add_item_to_array函数
add_item_to_array函数代码如下
static cJSON_bool add_item_to_array(cJSON *array, cJSON *item)
{cJSON *child NULL;if ((item NULL) || (array NULL) || (array item)){return false;}child array-child;/** To find the last item in array quickly, we use prev in array*/if (child NULL){/* list is empty, start new one */array-child item;item-prev item;item-next NULL;}else{/* append to the end */if (child-prev){suffix_object(child-prev, item);array-child-prev item;}}return true;
}他的主要代码如下
if (child NULL)
{/* list is empty, start new one */array-child item;item-prev item;item-next NULL;
}
else
{/* append to the end */if (child-prev){suffix_object(child-prev, item);array-child-prev item;}
}他的图例
如果链表为空 (child NULL)那么新的item就会成为链表的第一个也是唯一的元素。这个情况可以用下面的字符画来表示
array|vitem -- NULL^|item在这个图中array-child指向itemitem-prev指向item自身item-next指向NULL。
如果链表不为空那么新的item就会被添加到链表的末尾。这个情况可以用下面的字符画来表示
array|vchild1 -- child2 -- ... -- childN -- item -- NULL^ ^| |child1 item在这个图中array-child指向链表的第一个元素child1childN-next指向新的itemitem-prev指向childNitem-next指向NULL。
这样就成功的把我们的item连接到object里面了 总结
通过深入研究add_item_to_object函数的源码我们可以更好地理解cJSON库是如何处理JSON对象的。这个函数的实现虽然简单但却非常关键它使得我们可以方便地向JSON对象中添加新的项目。希望这篇文章能帮助你更好地理解cJSON库的内部工作原理以及如何在你自己的项目中使用它。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/85278.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!