/*
* Fork form https://github.com/DaveGamble/cJSON
* cJSON version: v1.6.0
*/
/*
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/* cJSON */
/* JSON parser in C. */
/* disable warnings about old C89 functions in MSVC */
#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
#define _CRT_SECURE_NO_DEPRECATE
#endif
#ifdef __GNUC__
#pragma GCC visibility push(default)
#endif
#if defined(_MSC_VER)
#pragma warning (push)
/* disable warning about single line comments in system headers */
#pragma warning (disable : 4001)
#endif
/************************************************************************************/
//
#include "cJSON.h"
#include <string.h>
#include <math.h>
#include <limits.h>
#ifndef ESP8266_PLATFORM
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#endif
#ifdef ENABLE_LOCALES
#include <locale.h>
#endif
#if defined(_MSC_VER)
#pragma warning (pop)
#endif
#ifdef __GNUC__
#pragma GCC visibility pop
#endif
/* define our own boolean type */
#ifndef true
#define true ((cJSON_bool)1)
#endif
#ifndef false
#define false ((cJSON_bool)0)
#endif
/************************************************************************************/
// 有关错误信息的代码
/* 错误信息结构体 */
typedef struct {
const unsigned char *json;
size_t position;
} error;
static error global_error = { NULL, 0 };
/*
* function: cJSON_GetErrorPtr
* description: 获取错误信息
*/
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void)
{
return (const char*) (global_error.json + global_error.position);
}
/************************************************************************************/
// 有关版本的代码
/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 6) || (CJSON_VERSION_PATCH != 0)
#error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
#endif
/*
* function: cJSON_Version
* description: get version of cJSON
*/
CJSON_PUBLIC(const char*) cJSON_Version(void)
{
static char version[15];
os_sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH);
return version;
}
/************************************************************************************/
/*
* function: case_insensitive_strcmp
* description: Case insensitive string comparison, doesn't consider two NULL pointers equal though
* 不区分大小写比较
*/
ICACHE_FLASH_ATTR static int case_insensitive_strcmp(
const unsigned char *string1, const unsigned char *string2) {
if ((string1 == NULL) || (string2 == NULL)) {
return 1;
}
if (string1 == string2) {
return 0;
}
for (; tolower(*string1) == tolower(*string2);
(void) string1++, string2++) {
if (*string1 == '\0') {
return 0;
}
}
return tolower(*string1) - tolower(*string2);
}
/************************************************************************************/
// 有关内存分配和释放的代码
// 内存分配和释放的钩子
typedef struct internal_hooks {
void *(*allocate)(size_t size);
void (*deallocate)(void *pointer);
void *(*reallocate)(void *pointer, size_t size);
} internal_hooks;
// 定义cJSON中内存分配采用的方式
// 移植的时候可能需要修改
#if defined(_MSC_VER)
/* work around MSVC error C2322: '...' address of dillimport '...' is not static */
static void *internal_malloc(size_t size)
{
return malloc(size);
}
static void internal_free(void *pointer)
{
free(pointer);
}
static void *internal_realloc(void *pointer, size_t size)
{
return realloc(pointer, size);
}
#elif defined(ESP8266_PLATFORM)
ICACHE_FLASH_ATTR static void *internal_malloc(size_t size) {
return (void *) os_malloc(size);
}
ICACHE_FLASH_ATTR static void internal_free(void *pointer) {
os_free(pointer);
}
ICACHE_FLASH_ATTR static void *internal_realloc(void *pointer, size_t size) {
return (void *) os_realloc(pointer, size);
}
#else
#define internal_malloc malloc
#define internal_free free
#define internal_realloc realloc
#endif
static internal_hooks global_hooks = { internal_malloc, internal_free,
internal_realloc };
/************************************************************************************/
/*
* function: cJSON_strdup
* description: 字符串拷贝函数,在构建object和array时会用到
*/
ICACHE_FLASH_ATTR static unsigned char* cJSON_strdup(
const unsigned char* string, const internal_hooks * const hooks) {
size_t length = 0;
unsigned char *copy = NULL;
// 检查参数
if (string == NULL) {
return NULL;
}
length = os_strlen((const char*) string) + sizeof("");
copy = (unsigned char*) hooks->allocate(length); // 申请内存
if (copy == NULL) {
return NULL;
}
os_memcpy(copy, string, length);
return copy;
}
/*
* function: cJSON_InitHooks
* description: 初始化钩子函数
*/
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) {
#ifndef ESP8266_PLATFORM
if (hooks == NULL)
{
/* Reset hooks */
global_hooks.allocate = malloc;
global_hooks.deallocate = free;
global_hooks.reallocate = realloc;
return;
}
global_hooks.allocate = malloc;
if (hooks->malloc_fn != NULL)
{
global_hooks.allocate = hooks->malloc_fn;
}
global_hooks.deallocate = free;
if (hooks->free_fn != NULL)
{
global_hooks.deallocate = hooks->free_fn;
}
/* use realloc only if both free and malloc are used */
global_hooks.reallocate = NULL;
if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free))
{
global_hooks.reallocate = realloc;
}
#else
if (hooks->malloc_fn != NULL) {
global_hooks.allocate = hooks->malloc_fn;
}
if (hooks->free_fn != NULL) {
global_hooks.deallocate = hooks->free_fn;
}
#endif
}
/************************************************************************************/
/*
* function: cJSON_New_Item
* description: 构造一个item,item可以理解为一个节点
*/
/* Internal constructor. */
ICACHE_FLASH_ATTR static cJSON *cJSON_New_Item(
const internal_hooks * const hooks) {
cJSON* node = (cJSON*) hooks->allocate(sizeof(cJSON));
if (node) {
os_memset(node, '\0', sizeof(cJSON)); // 设置node为0
}
return node;
}
/*
* function: cJSON_Delete
* description: 删除item,采用递归的方式释放item->child,循环方式释放item->next
* 当root节点不使用后必须调用这个函数
*/
/* Delete a cJSON structure. */
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) {
cJSON *next = NULL;
while (item != NULL) {
next = item->next;
if (!(item->type & cJSON_IsReference) && (item->child != NULL)) {
cJSON_Delete(item->child); // 开始递归
}
if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) {
global_hooks.deallocate(item->valuestring);
}
if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) {
global_hooks.deallocate(item->string);
}
global_hooks.dea
ESP8266示例D01-cjson_app.7z
版权申诉
101 浏览量
2023-07-13
16:42:22
上传
评论
收藏 63KB 7Z 举报
应用市场
- 粉丝: 456
- 资源: 3808