• 大小: 322KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-06
  • 语言: 其他
  • 标签: STM32  CJSON  

资源简介

使用CJSON库在STM32平台上实现JSON对象的构造和解析,工程亲测可用,demo工程包括构造一个JSON对象通过串口发送出去,以及提供串口接收一个示例json对象解析,然后将解析出来的结果通过串口在发送出去,并且移植了内存管理,解决内存泄露问题,好东西值得高分

资源截图

代码片段和文件信息

/*
  Copyright (c) 2009 Dave Gamble

  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. */

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include “cJSON.h“

#include “malloc.h“

static const char *ep;

const char *cJSON_GetErrorPtr(void) {return ep;}

static int cJSON_strcasecmp(const char *s1const char *s2)
{
if (!s1) return (s1==s2)?0:1;if (!s2) return 1;
for(; tolower(*s1) == tolower(*s2); ++s1 ++s2) if(*s1 == 0) return 0;
return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
}

static void *(*cJSON_malloc)(size_t sz) = mymalloc;
static void (*cJSON_free)(void *ptr) = myfree;

static char* cJSON_strdup(const char* str)
{
      size_t len;
      char* copy;

      len = strlen(str) + 1;
      if (!(copy = (char*)cJSON_malloc(len))) return 0;
      memcpy(copystrlen);
      return copy;
}

void cJSON_InitHooks(cJSON_Hooks* hooks)
{
    if (!hooks) { /* Reset hooks */
        cJSON_malloc = malloc;
        cJSON_free = free;
        return;
    }

cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc;
cJSON_free  = (hooks->free_fn)?hooks->free_fn:free;
}

/* Internal constructor. */
static cJSON *cJSON_New_Item(void)
{
cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON));
if (node) memset(node0sizeof(cJSON));
return node;
}

/* Delete a cJSON structure. */
void cJSON_Delete(cJSON *c)
{
cJSON *next;
while (c)
{
next=c->next;
if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child);
if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring);
if (c->string) cJSON_free(c->string);
cJSON_free(c);
c=next;
}
}

/* Parse the input text to generate a number and populate the result into item. */
static const char *parse_number(cJSON *itemconst char *num)
{
double n=0sign=1scale=0;int subscale=0signsubscale=1;

if (*num==‘-‘) sign=-1num++; /* Has sign

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      23200  2014-11-23 10:00  stm32_cjson_demo\stm32_cjson_demo\cJSON\cJSON.c

     文件       6833  2014-11-21 12:29  stm32_cjson_demo\stm32_cjson_demo\cJSON\cJSON.h

     文件       1091  2013-08-14 14:02  stm32_cjson_demo\stm32_cjson_demo\cJSON\LICENSE

     文件       8751  2009-10-28 23:48  stm32_cjson_demo\stm32_cjson_demo\cJSON\README

     文件       6573  2013-02-09 12:56  stm32_cjson_demo\stm32_cjson_demo\cJSON\test.c

     文件        583  2009-08-23 23:46  stm32_cjson_demo\stm32_cjson_demo\cJSON\tests\test1

     文件        242  2009-08-23 23:46  stm32_cjson_demo\stm32_cjson_demo\cJSON\tests\test2

     文件        605  2009-08-23 23:46  stm32_cjson_demo\stm32_cjson_demo\cJSON\tests\test3

     文件       3467  2009-08-23 23:47  stm32_cjson_demo\stm32_cjson_demo\cJSON\tests\test4

     文件        873  2009-08-23 23:47  stm32_cjson_demo\stm32_cjson_demo\cJSON\tests\test5

     文件      17273  2010-06-07 10:25  stm32_cjson_demo\stm32_cjson_demo\CMSIS\core_cm3.c

     文件      85714  2011-02-09 14:59  stm32_cjson_demo\stm32_cjson_demo\CMSIS\core_cm3.h

     文件      15766  2011-03-10 10:52  stm32_cjson_demo\stm32_cjson_demo\CMSIS\startup\startup_stm32f10x_cl.s

     文件      15503  2011-03-10 10:52  stm32_cjson_demo\stm32_cjson_demo\CMSIS\startup\startup_stm32f10x_hd.s

     文件      15692  2011-03-10 10:52  stm32_cjson_demo\stm32_cjson_demo\CMSIS\startup\startup_stm32f10x_hd_vl.s

     文件      12376  2011-03-10 10:52  stm32_cjson_demo\stm32_cjson_demo\CMSIS\startup\startup_stm32f10x_ld.s

     文件      13656  2011-03-10 10:52  stm32_cjson_demo\stm32_cjson_demo\CMSIS\startup\startup_stm32f10x_ld_vl.s

     文件      12765  2011-03-10 10:52  stm32_cjson_demo\stm32_cjson_demo\CMSIS\startup\startup_stm32f10x_md.s

     文件      14073  2011-03-10 10:51  stm32_cjson_demo\stm32_cjson_demo\CMSIS\startup\startup_stm32f10x_md_vl.s

     文件      15955  2011-03-10 10:51  stm32_cjson_demo\stm32_cjson_demo\CMSIS\startup\startup_stm32f10x_xl.s

     文件     633941  2011-03-10 10:51  stm32_cjson_demo\stm32_cjson_demo\CMSIS\stm32f10x.h

     文件      36557  2011-03-10 10:51  stm32_cjson_demo\stm32_cjson_demo\CMSIS\system_stm32f10x.c

     文件       2085  2011-03-10 10:51  stm32_cjson_demo\stm32_cjson_demo\CMSIS\system_stm32f10x.h

     文件       8982  2011-03-10 10:47  stm32_cjson_demo\stm32_cjson_demo\FWlib\inc\misc.h

     文件      21690  2011-03-10 10:47  stm32_cjson_demo\stm32_cjson_demo\FWlib\inc\stm32f10x_adc.h

     文件       7555  2011-03-10 10:47  stm32_cjson_demo\stm32_cjson_demo\FWlib\inc\stm32f10x_bkp.h

     文件      27559  2011-03-10 10:47  stm32_cjson_demo\stm32_cjson_demo\FWlib\inc\stm32f10x_can.h

     文件       6573  2011-03-10 10:47  stm32_cjson_demo\stm32_cjson_demo\FWlib\inc\stm32f10x_cec.h

     文件       2162  2011-03-10 10:47  stm32_cjson_demo\stm32_cjson_demo\FWlib\inc\stm32f10x_crc.h

     文件      15233  2011-03-10 10:47  stm32_cjson_demo\stm32_cjson_demo\FWlib\inc\stm32f10x_dac.h

............此处省略74个文件信息

评论

共有 条评论