cJson在物联网项目中经常使用,但是cJson毕竟不是专为MCU平台使用,所以做了很多通用性的东西,导致MCU平台项目中即使没有用到malloc,cJson也会将malloc引入到固件中,导致固件过大。
所以决定将cJson裁剪一下,把malloc等标准库移除。让其更适合MCU平台使用。
本仓库中,查看branch:my-mcu-version。
所有修改项已经集中到cJson_Config.h中:
#define cJSON_INLINE __attribute__((always_inline)) static inline
/* malloc/free objects using the malloc/free functions */
cJSON_INLINE void *cJSON_malloc(size_t size)
{
return malloc(size);
}
cJSON_INLINE void cJSON_free(void *object)
{
free(object);
}
/*
* config default sprintf function for cJSON
*/
#define cJSON_SPRINTF sprintf
/*
* config default strlen function for cJSON
*/
#define cJSON_STRLEN strlen
/*
* config default strcpy function for cJSON
*/
#define cJSON_STRCPY strcpy
/*
* config default strncmp function for cJSON
*/
#define cJSON_STRNCMP strncmp
/*
* config default strtod function for cJSON
*/
#define cJSON_STRTOD strtod
/*
* config default sscanf function for cJSON
*/
#define cJSON_SSCANF sscanf
/*
* config default memcpy function for cJSON
*/
#define cJSON_MEMCPY memcpy
/*
* config default memset function for cJSON
*/
#define cJSON_MEMSET memset
/*
* config default fabs function for cJSON
*/
#define cJSON_FABS fabs
移植步骤:
1.根据mcu编译平台,更改cJSON_INLINE宏定义,该宏为了将函数内联。
2.在cJSON_malloc和cJSON_malloc函数接口中,添加项目的内存分配和释放函数。
3.后面一些杂项,如果你的项目中有自己实现的代码,可以更改。
到此就移植完毕了,无需再像普通cJson一样,需要经过初始化再使用。这个版本即可直接使用。
714



被折叠的 条评论
为什么被折叠?



