|
这几天使用了一下JSON在STM32103平台上的使用,还是很好用的,在此记录下。 JSON是啥我也不总结了,就是直观的看起来是一个有格式的字符串,看起来非常清楚明白,有点像Python中的dicitionary的意思,实际使用的过程中主要就是“打包”和“解析”。下面直接来代码。 - char * Status_to_cJSON( char * cJSONROOM, ROBOStatus_TypeDef status)//传入一个变量的指针,这里cJSONROOM是一个全局变量(一个提前规定大小的字符数组),用来存放转换之后的JSON字符串
1 H% g( i- h3 t+ [8 i( d' k
( p" _, C3 U) y2 Z% }- {* B$ b. h1 v% y& T) u
( w. K0 S: i2 i; `- char *result;' G: F1 `# i- k7 e: s" m
- 8 V1 V5 j! u; Q
- cJSON *root,*subroot;//新建两个cJSON的对象指针
' @; P5 {! }; w
! P7 H6 a( V, V) q- root=cJSON_CreateObject();//创建一个机器人状态的JSON对象& O, a# _' r7 L. R
- & g4 U j# y+ u+ K7 B5 p$ f
- subroot=cJSON_CreateObject();//subroot是下面的一个嵌入对象9 h7 D8 c/ w0 j
- + C+ L# Z6 B: ^. K% X _
- cJSON_AddStringToObject(subroot,"RunStatus",status.RunStatus);//将status.RunStatus的值赋值到subroot下面一个叫RunStatus的变量。
1 \' s6 z3 A/ u& N; k
: h$ q" F" I' D0 w1 `! [8 ~- cJSON_AddStringToObject(subroot,"CurrentTime",status.CurrentTime);1 R* M$ F2 u9 P. l1 e2 K
- 1 O5 v. f) n/ A! ?1 X5 |
- //同上
" E/ W( K% L0 S8 ~ - . Y% h6 R7 ?% R3 J: {2 Q* Q3 I
- cJSON_AddNumberToObject(subroot,"CurrentPosition",status.CurrentPositiont);- h; G9 p1 I3 }, g
- 4 [' x4 e' V. n. e
- //将位置信息赋值到subroot下面一个叫CurrentPosition的变量,注意此处为Number类型! K* Q& @) z; O4 H
- 6 S P5 F% U0 H$ i1 Y
- cJSON_AddNumberToObject(subroot,"CurrentSpeed",status.CurrentSpeed);: {& R4 X+ }# d+ G; i. ?* P
- 4 K$ s5 F! `( ^$ t2 y5 m
- cJSON_AddNumberToObject(subroot,"RunningCount",status.RunningCount);
' B0 ^- j8 b) T* i$ H/ h - & j7 ?; L2 `. F/ B
- cJSON_AddNumberToObject(subroot,"CurrentTemp",status.CurrentTemp);; B1 H' `* j" w7 F& w- K& S, v
- 6 a8 a) O8 j5 B8 U Q
- cJSON_AddNumberToObject(subroot,"CurrentVoltage",status.CurrentVoltage);' h2 z0 P- T' O& k& Z
- - f5 v3 _! f- n- L1 t5 H( m$ t5 Q
- cJSON_AddNumberToObject(subroot,"CurrentAmp",status.CurrentAmp);8 w9 H, d; }) g3 L8 J% O8 L4 B' M
- 8 v4 t1 H8 p+ v& c; h: `# H! |
- cJSON_AddNumberToObject(subroot,"CurrentDir",status.CurrentDir);
o$ d- N9 p3 j. g1 y/ q1 d7 Q& i - 9 |( t" A: G+ {# T3 b/ t/ y- o; O
- cJSON_AddNumberToObject(subroot,"ControlSystemEnergy",status.ControlSystemEnergy);2 W2 i: N3 W6 I. d* C. [
- 7 {& t; ^! c- Y
- cJSON_AddNumberToObject(subroot,"DynamicSystemEnergy",status.DynamicSystemEnergy);
5 n( I4 t k; p7 `
- `* k5 \* {# o+ h- cJSON_AddItemToObject(root, "RobotStatus", subroot);2 x, G3 z4 Z5 `! X
- : u; L; W7 a7 u
- result=cJSON_PrintUnformatted(root);//生成JSONC字符串,注意可以用cJSON_Print()格式的,就是人眼看着好看一点,就是有点占用存储空间
g; j% o. X6 {$ W7 A# i - 6 o6 B* W" \. G* R! r
- strcpy(cJSONROOM,result);//将转换的结果字符串赋值给传入的全局变量
6 i) D O9 P) ~
4 G( _" q6 o: c' o$ p& d" e3 a9 ?- cJSON_Delete(root);//最后将root根节点删除) ~; [4 j6 p. Q; A1 T9 Y) Y/ z) t
- , _: Q& e; Z6 ?
- myfree(result);//释放result的空间,必须要有,要不然内存里会失去一段空间,最后系统崩溃
" E; I" T2 N7 a; e
9 B& g3 `; d3 u1 H- h& x: l- return cJSONROOM;//不要指望着返回一个局部变量的地址,这是非常危险的,因为函数调用完毕后这个地址指向的内容就消失了。所以这个函数在设计的时候返回了一个全局变量的地址。1 I/ C( M9 H' g+ Y- C) {2 j1 y
- : n2 m) b+ v7 h. v/ z9 `
- }
复制代码注意:malloc在STM32平台上使用的时候需要改动一下,关于内存操作的改动见下面的代码。使用字符数组会提高程序的可靠性,之前一直用字符指针,可能没有用好,中途会挂掉,后来发现提前建立一个数组,在这段空间进行数据的操作还是比较稳定的。 - #include "malloc.h"
6 h2 k$ t1 `. z
/ F5 Y" k9 l+ p/ \3 \; N* X- //内存池(4字节对齐)
2 r6 A* v/ h, r+ t! r) K8 T - __align(4) u8 membase[MEM_MAX_SIZE]; //内部SRAM内存池$ o; n X3 o. |* Z
- //内存管理表
( n, V+ N' K' e$ k' a4 w- o& O - u16 memmapbase[MEM_ALLOC_TABLE_SIZE]; //内部SRAM内存池MAP7 C& C3 E/ k5 r7 ]: o
- //内存管理参数
; ~" k$ j6 j8 \0 f* R5 c- l; { - const u32 memtblsize = MEM_ALLOC_TABLE_SIZE; //内存表大小& b7 K9 G+ f. h" m& l: `
- const u32 memblksize = MEM_BLOCK_SIZE; //内存分块大小
* P0 C/ y# I* d% B2 `+ b - const u32 memsize = MEM_MAX_SIZE; //内存总大小
. x& h; E1 G% q$ V% X - 5 {) v5 ^, V4 H9 D7 _# t1 J
! u+ K5 _ Q1 Z- //内存管理控制器+ F& W3 Q8 L+ ?( @; i+ _
- struct _m_mallco_dev mallco_dev=5 }. I5 m" t$ z0 |' V; S
- {
; s3 b( N. `2 c+ y, l3 x, Q - mem_init, //内存初始化
1 _9 b k+ X- o! O7 y1 d7 ^/ N - mem_perused, //内存使用率0 @2 X0 Y$ i g. b- ^+ D( h7 @
- membase, //内存池
, ~# R+ s, R5 r; m# h2 r/ B - memmapbase, //内存管理状态表; R* z+ ^9 r0 S' m. X6 S9 h
- 0, //内存管理未就绪1 v+ q. r5 L! ?. b
- };
$ W( n2 `" J1 x. f' L. T8 l
8 K% i& K* e8 V; z, k- //复制内存
/ A8 C' `' t9 ` - //*des:目的地址
. U; w; I0 C5 A4 b, X& A P) u2 J - //*src:源地址
" A& O, E( R- K - //n:需要复制的内存长度(字节为单位)" F1 o& e9 I$ g- V( k
- void mymemcpy(void *des,void *src,u32 n)
& Q8 ^; j' q, b- c) ^( @ - {
. D3 V( p) E4 n: Q" }. w- p - u8 *xdes=des;$ w) B8 b) Z( ~
- u8 *xsrc=src;# `$ X; Z v% Y7 D
- while(n--)*xdes++=*xsrc++;
$ P, n1 m8 }0 m3 \ - }; f6 {7 Q: `' Q5 g
1 W9 T! ^% F3 i8 \4 [' n a/ l- //设置内存9 }; d/ ^0 D6 j, [1 o0 x# q
- //*s:内存首地址$ ]% g" d* c+ U7 \4 {
- //c :要设置的值" ~5 d0 m# _+ r) A
- //count:需要设置的内存大小(字节为单位)
8 b8 e& ~) Q0 D- k) h/ h8 C1 O3 G - void mymemset(void *s,u8 c,u32 count)$ P$ H3 A' }7 d e+ y
- {
% {" F* @ |7 J+ c% C: K9 a F - u8 *xs = s;
8 S6 m6 a8 }% D) D - while(count--)*xs++=c;4 c8 E* j% g& E% h/ i ~; O/ I
- }
$ V8 u% ], q2 g4 S
, Y. J5 T2 o, }- //内存管理初始化
- f8 M/ P6 u( a, t - //memx:所属内存块- H% F4 L) X( }# g+ \3 X7 x9 o( F
- void mem_init(void)
1 V. H/ z+ E. X - {+ ^( ]+ B* g' Z5 c$ X
- mymemset(mallco_dev.memmap, 0,memtblsize*2);//内存状态表数据清零
: q( V% A% J2 \6 |% A& o% k - mymemset(mallco_dev.membase, 0,memsize); //内存池所有数据清零0 `# `' P1 C. K' B' C1 u7 R$ Q
- mallco_dev.memrdy=1; //内存管理初始化OK
- z) C, ~- F' V9 q8 {; P9 S - }5 S3 _ h/ N. `5 n
$ m/ @) F5 a7 i8 W- //获取内存使用率3 K0 P$ g! V6 H* H
- //memx:所属内存块 Y* i+ a2 w2 R! t* m' L
- //返回值:使用率(0~100)
% u6 e( v, {" p - u8 mem_perused(void)+ J8 L+ _, y3 K7 a
- {/ u8 x+ o9 _( j% B: \2 j' I8 B
- u32 used=0;
3 J* d7 c( k; W5 v: }4 {7 S - u32 i;7 M5 Y( m8 J, N5 k+ r8 B8 M
- for(i=0;i<memtblsize;i++)9 m( S0 Z% G9 ?0 ]- O
- {
6 M/ y+ c& x5 d+ R+ J3 b - if(mallco_dev.memmap[i])used++;1 C x# x: v) n! g) m/ t
- } r" r# E, N4 C, o; P
- return (used*100)/(memtblsize);3 ^2 u5 @2 k( n" k4 o8 l" |
- }
R7 F! }& f5 [# ?7 F: t- @
1 @& c5 g( g) @; Y- //内存分配(内部调用)" v: w1 \, a6 O3 b3 e
- //memx:所属内存块
) H" N+ j+ \& A b& t - //size:要分配的内存大小(字节)
4 _# V- e) P& Z) ~% m% A2 {7 r - //返回值:0XFFFFFFFF,代表错误;其他,内存偏移地址
* L" q" k3 b% Y5 v6 b* Y- m, g H - u32 mem_malloc(u32 size)' C9 Y% a' A& C8 N, ?; Z
- {
$ q3 w: Z3 |7 ]! [ - signed long offset=0;
! \ m4 K3 {% Z# s - u16 nmemb; //需要的内存块数9 E4 [/ g% O8 T9 P
- u16 cmemb=0;//连续空内存块数+ I9 E' e; f0 X7 j8 u1 w0 l
- u32 i;, k) S. Y$ h( @% h5 M
- if(!mallco_dev.memrdy)mallco_dev.init();//未初始化,先执行初始化; ]2 x7 u) l8 c& J/ v( |
- if(size==0)return 0XFFFFFFFF;//不需要分配
2 ^' Q" A. W2 t) P, j9 D
) m5 s2 T5 I3 W/ `3 X% B7 _- nmemb=size/memblksize; //获取需要分配的连续内存块数! w0 l; J" C) s: E3 y! W2 ~
- if(size%memblksize)nmemb++;1 E. I4 h- `9 ~$ @2 Q
- for(offset=memtblsize-1;offset>=0;offset--)//搜索整个内存控制区
2 \% a) B2 E5 t2 g - {
" |+ k+ q9 Q2 S L! [# p7 Z - if(!mallco_dev.memmap[offset])cmemb++;//连续空内存块数增加$ X _' Z& A: g' a4 [
- else cmemb=0; //连续内存块清零
0 [( e2 E4 _1 w3 E3 y - if(cmemb==nmemb) //找到了连续nmemb个空内存块
! |, U" F4 D% V- y: d5 E) m/ x - {
# n9 U3 L. V6 k: @" @0 M! O" w - for(i=0;i<nmemb;i++) //标注内存块非空
7 M: y" s) i# g5 b- q9 e - {
; {4 i/ b: [( ~" u+ e - mallco_dev.memmap[offset+i]=nmemb;0 _& b: a. T0 @: i
- }7 V! K+ b5 v2 H( |8 s- c
- return (offset*memblksize);//返回偏移地址
# K3 w# b% a+ h! R3 H - }$ Z" F9 Q0 Q! \6 o$ T# d' L
- } k0 h! G9 W( i9 p* {
- return 0XFFFFFFFF;//未找到符合分配条件的内存块
4 n# H# h3 D) p+ s( w) F4 k9 s+ u - }
2 F% }1 O1 v' J- `( N& G - ; `8 L' D( L, L- ~; _. W: @% f
- //释放内存(内部调用)2 `8 H# s9 B3 |. [, j
- //memx:所属内存块+ D: i |) G. p! D* ~
- //offset:内存地址偏移
) p1 g+ F% z; }7 p - //返回值:0,释放成功;1,释放失败;
; [$ a% y0 Y7 G+ n' c. V, Y+ Q1 s - u8 mem_free(u32 offset)
$ l6 Q. ~6 M% M6 O9 H7 R - {
( J+ B0 e- e% |$ w' A5 ]- q - int i;
; f% g, F( ~7 m( g s4 S* B9 b. P+ p - if(!mallco_dev.memrdy)//未初始化,先执行初始化
: d8 G8 q6 i$ e3 T - {0 p. U9 j% h3 |: z
- mallco_dev.init();
2 l, S% f6 ?: a3 s% s( b$ r% \ - return 1;//未初始化
/ G1 |8 e1 v, [# Q - } y+ \ F0 U9 U$ m! f& z
- if(offset<memsize)//偏移在内存池内.) B: Y; g" x0 F3 E* \* I
- {0 p5 K, T W( U8 W) n9 U
- int index=offset/memblksize; //偏移所在内存块号码' A) l& Y: |6 `) z& j
- int nmemb=mallco_dev.memmap[index]; //内存块数量- P5 p+ X/ {& J1 s" q0 H
- for(i=0;i<nmemb;i++) //内存块清零 E6 y# Y5 a1 K
- {8 D+ u0 @0 M5 ?
- mallco_dev.memmap[index+i]=0;
6 g( ] T8 j6 f& Q. c - }- `, o2 @% m' w* L% W
- return 0;1 O Q+ E2 Z( A
- }else return 2;//偏移超区了.9 Y7 f7 G8 K! [9 S l0 [7 ~
- }1 U* u& z2 d7 I
- 8 l/ l: t: x, A( g& n. M
- //释放内存(外部调用); f1 O3 q4 c3 m
- //memx:所属内存块
8 h/ V8 `6 h; R3 e" f - //ptr:内存首地址- Y8 z9 p; F& N0 ]! Q
- void myfree(void *ptr)% f" G5 E1 X0 P. s5 H* U
- {. W, T6 ~1 ]% w0 ^
- u32 offset;) i# g4 y( e( T8 \ _9 R& l
- if(ptr==NULL)return;//地址为0.' B7 x+ n9 V: m, G) T" V
- offset=(u32)ptr-(u32)mallco_dev.membase;' B8 f. g* I q/ R9 }1 E4 L- h/ h/ r
- mem_free(offset);//释放内存1 b/ Y: N( i4 a8 b
- }# L8 E4 J7 M6 B8 c6 X# h
- : v8 \( X! R7 f1 J1 Q
- //分配内存(外部调用)# g! @& {: D& X- y+ O5 R1 ?$ _
- //memx:所属内存块
* p$ \) g& I- K5 M5 p - //size:内存大小(字节) s) N) `7 q1 i n6 Y4 \6 b
- //返回值:分配到的内存首地址.
& M. J- h/ i$ I - void *mymalloc(u32 size)# w9 u f3 Q% e6 a9 E" S# Y
- {" X6 y* C# \- A
- u32 offset;
9 F5 V8 s, H/ Q: N" f - offset=mem_malloc(size);( |) G' M* T+ i' w) d
- if(offset==0XFFFFFFFF)return NULL;1 D& K6 h; U% n7 Y& Y8 |( w
- else return (void*)((u32)mallco_dev.membase+offset);% F# w$ F$ `& G9 r/ I P
- }/ P- }$ X8 p& ]. B: _; w: a
- 2 @9 W" s B( c! T
- //重新分配内存(外部调用)
- B9 `/ C1 }: [2 r - //memx:所属内存块, [( n" l% w2 S
- //*ptr:旧内存首地址
' w2 b) m* p( O. W$ n- ?4 U - //size:要分配的内存大小(字节)
$ L2 I7 F, m' n3 a, W - //返回值:新分配到的内存首地址.
8 K( B; Y/ D R8 F$ C# P# e - void *myrealloc(void *ptr,u32 size)
. e$ U) E5 n2 L3 j8 ]& | - {9 m) B+ F# E v" [
- u32 offset;
# ~8 w: r' i: f$ n - offset=mem_malloc(size);
% P2 S8 U' x, R' e V- ~ - if(offset==0XFFFFFFFF)return NULL;# t. R$ I& s/ @0 H4 G8 ^
- else
' s9 H( [7 y m5 s - {% f3 {. }1 x# D) P
- mymemcpy((void*)((u32)mallco_dev.membase+offset),ptr,size); //拷贝旧内存内容到新内存1 O+ P) s0 R- e( S1 Z5 _
- myfree(ptr); //释放旧内存
3 m, w% H, d' j9 Q `; ?3 ?5 g - return (void*)((u32)mallco_dev.membase+offset); //返回新内存首地址- ~$ i% V+ B: W+ x4 [: u f& s
- }7 G# d4 z2 A8 {3 ]$ C
- }
复制代码malloc.h内容如下: - #ifndef __MALLOC_H
5 Y5 ^8 [4 o9 J+ e$ ^ - #define __MALLOC_H
$ h6 h' u3 G! Q - #include "stm32f10x.h"
5 O+ O! N, @2 V- r q6 B, I5 U - 8 ?( }) }$ ]! @% c
- #ifndef NULL
1 W+ x/ J- J% A7 A x8 g { - #define NULL 0
1 B: x2 V M: T/ X - #endif+ y- S: A& q* B K# B! Q% `; C
- / q$ F" |# S: A# B* R2 @
- #define MEM_BLOCK_SIZE 32 //内存块大小为32字节
Y9 a! [* X, n: j; W! n - #define MEM_MAX_SIZE 16*1024 //最大管理内存 2K( a$ e7 ] ^' J0 E
- #define MEM_ALLOC_TABLE_SIZE MEM_MAX_SIZE/MEM_BLOCK_SIZE //内存表大小( N' { _& ^ X
- 3 N2 j# D) R) j
- //内存管理控制器
9 g! k( h1 S- S" E# ? - struct _m_mallco_dev
! ?% G- ^1 m/ y/ X& |$ @ - {
( b, J7 ~, o* r9 M+ Z! x, Y - void (*init)(void); //初始化7 W0 h7 K( O) W7 _0 `5 R
- u8 (*perused)(void); //内存使用率- O1 ^5 l! F D5 @4 j! L
- u8 *membase; //内存池- }! e1 [) i7 t
- u16 *memmap; //内存管理状态表5 v/ ]! Q9 l$ S; l
- u8 memrdy; //内存管理是否就绪
* q6 O& ^1 x+ t1 h0 u0 J2 a; Z - };
4 z5 t/ i& ~4 j1 c7 {6 } - " A3 \' v/ m! A* X
- extern struct _m_mallco_dev mallco_dev; //在mallco.c里面定义8 h: e( z% N' d
# c- |0 h; ^' r2 V: y/ d u- void mymemset(void *s,u8 c,u32 count); //设置内存
: x3 z! p& _" h4 \3 u - void mymemcpy(void *des,void *src,u32 n);//复制内存
( h! v1 [% a: I" u5 x - 5 z! V4 Y8 \+ B+ j4 q/ q& h
- void mem_init(void); //内存管理初始化函数
6 @. O9 t% O8 e1 V$ u* Z! A# h( l - u32 mem_malloc(u32 size); //内存分配
4 I# K* I; r# D" }. T6 T9 ] - u8 mem_free(u32 offset); //内存释放
+ Z# U. Q Z6 B2 m9 x6 t" f. n - u8 mem_perused(void); //获得内存使用率
4 c" v5 T( g# J. H9 l1 } - ////////////////////////////////////////////////////////////////////////////////7 v8 o4 H6 |9 E* g; b7 s. k# [% o7 V
- //用户调用函数# z8 w) j# v6 W0 p5 e/ V
- void myfree(void *ptr); //内存释放
) ~1 y( ]/ _+ Q0 b* |1 e* s8 E - void *mymalloc(u32 size); //内存分配+ H7 s1 q# ^- Y; k2 M) w+ q) T
- void *myrealloc(void *ptr,u32 size);//重新分配内存2 b# L: T" O8 {! e
- #endif
复制代码cJSON.c的内容也全在下面 - 1 /*0 T- I; t5 g( t# ?
- 2 Copyright (c) 2009 Dave Gamble8 f) i( P' {' \2 a; ~. `) A7 q
- 3
$ n7 e J2 V0 p7 r; _1 W( i3 ?7 G" \' Q - 4 Permission is hereby granted, free of charge, to any person obtaining a copy
]% r8 E6 p' ?5 q - 5 of this software and associated documentation files (the "Software"), to deal8 I y9 P! h) t. Q( }8 `* A7 R
- 6 in the Software without restriction, including without limitation the rights9 C* T, A% g& y# n% c( W. d
- 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell- U2 L: X4 s" Q: C
- 8 copies of the Software, and to permit persons to whom the Software is* ]3 J, M: G+ c- B
- 9 furnished to do so, subject to the following conditions:
9 I* o, P* Z W0 `' R4 C' ~ - 10
' Z( s+ W2 p2 J3 ^& K) S% D - 11 The above copyright notice and this permission notice shall be included in/ ]( F; u) y) Q4 J0 y$ R6 k8 ?
- 12 all copies or substantial portions of the Software.4 |5 s, o& R& y8 t
- 13 9 L& C! x+ p8 Q, O4 S
- 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
z% s$ K& k+ h6 o7 \ - 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% N& q/ v% ]) N6 B$ x' m - 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE4 [7 q o* H+ i! C0 l5 q
- 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER0 Q, @) Q$ D3 {% w1 e( O
- 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,: A2 B: T7 C5 y) f% @- r
- 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN% H2 {2 |, D& n: ?# x4 f
- 20 THE SOFTWARE.
$ B# U3 g/ L+ t( p - 21 */
. F" w: h5 v3 z0 o3 r* R4 ` - 22 7 s d" h; N/ c7 v
- 23 /* cJSON */
4 V- w/ l9 ?4 \; |. H7 U- K' t - 24 /* JSON parser in C. */
+ e0 l# c( Y6 s" ? - 25 / Z9 }" m) e1 U) A, T$ [ V
- 26 #include <string.h>& f" [! s0 v; x# b3 N
- 27 #include <stdio.h>0 f9 ~$ f$ L+ q% t9 ^3 o
- 28 #include <math.h>$ ]& g; r/ D9 }/ H: k, X; q( D
- 29 #include <stdlib.h>3 n6 w# _3 S6 E- c. C0 O+ |
- 30 #include <float.h>
) t* Y* M9 }5 Y* y - 31 #include <limits.h>
7 \# ~: W% @6 Q. w" ^ - 32 #include <ctype.h>
4 l! _3 U: X# F6 w9 H: M - 33 #include "cJSON.h"
9 T+ x8 m( ?, M2 Z' f; d1 r - 34
" w3 _2 ~! \1 s) ] - 35 #include "malloc.h"
* s. \* t* I" S9 S - 36 ! ]5 P4 z4 ?/ o. v# H9 c
- 37 static const char *ep;
' p" ^- p/ T/ \) S/ Q - 38 3 m2 @2 F$ Y. M- ?( M
- 39 const char *cJSON_GetErrorPtr(void) {return ep;}
6 v& B+ |& u; p0 G+ h% L$ M/ l - 40 N5 ?2 ~3 X/ v' G$ Q4 {+ {
- 41 static int cJSON_strcasecmp(const char *s1,const char *s2)3 v1 F3 c# d% h: P
- 42 {
, k6 R9 q2 ~1 x9 _: t/ } - 43 if (!s1) return (s1==s2)?0:1;if (!s2) return 1;7 L$ {$ _* T% U% E! }1 t4 ]* q
- 44 for(; tolower(*s1) == tolower(*s2); ++s1, ++s2) if(*s1 == 0) return 0;
$ M9 U4 O- n) Z; G. { - 45 return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
; v5 i5 ~# S; e; r$ t; P - 46 }) ]1 M3 B& u. u: M
- 47
9 C8 r2 n! A. @4 O - 48 static void *(*cJSON_malloc)(size_t sz) = mymalloc;9 k( o1 I' j) W, p8 D
- 49 static void (*cJSON_free)(void *ptr) = myfree;
4 i& } \3 X2 t) k) r6 b - 50
?3 L' \8 q& I2 P' \ - 51 static char* cJSON_strdup(const char* str) M& |# T- t- W: G% T
- 52 {0 c3 \) L- \1 j; c$ t7 d
- 53 size_t len;7 s9 F. d" Q+ Y
- 54 char* copy;+ b, j3 t) ?+ p! Y7 C# F8 K6 U" ]
- 55
: n a- |, c* O" z ~, N' A - 56 len = strlen(str) + 1;6 a9 H8 Q' j/ s9 Y
- 57 if (!(copy = (char*)cJSON_malloc(len))) return 0;
7 w" J2 o$ m1 N0 K - 58 memcpy(copy,str,len);
0 J% {6 q0 i$ ?: N: w - 59 return copy;
% K) ]' H! I0 |- X9 U - 60 }/ d7 p: ^- {! h8 H" s: |, S
- 61
6 Q( A8 L+ B+ ~: Q - 62 void cJSON_InitHooks(cJSON_Hooks* hooks)
+ ^0 G( Z4 O6 J; U9 \! i1 x+ y0 n - 63 { e0 I. ` B- [! Y0 T- l# \
- 64 if (!hooks) { /* Reset hooks */4 i& ~' f9 s% q5 G
- 65 cJSON_malloc = malloc;
4 P9 b4 S# P* s/ ~& B2 T - 66 cJSON_free = free;
+ t! h1 P: M, `! E) ?4 ~" x3 F( V - 67 return;$ }0 H2 z2 L7 }! h0 M+ O% k
- 68 }
! b4 \5 s( A1 f2 W( E - 69
- `, g9 `8 B/ Z/ i - 70 cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc;4 i2 F5 }- \1 }( s% t5 X. z# p* T
- 71 cJSON_free = (hooks->free_fn)?hooks->free_fn:free;
K7 e; K$ {8 u3 h - 72 }4 m$ Y' z% E& ]+ D, f( l" B
- 73 6 S& e! v0 d* M! F3 i( |0 d
- 74 /* Internal constructor. */
+ \) u/ N% U( v. ~/ N% F" \. W - 75 static cJSON *cJSON_New_Item(void)5 h6 k# v6 [7 e- E# E( a
- 76 {
- Q0 u, F2 F5 B! h6 l& Z# n - 77 cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON));/ C z" c0 \' o# ?. t
- 78 if (node) memset(node,0,sizeof(cJSON));
9 }6 I0 ~5 u: s W8 G - 79 return node;* X6 A3 m1 w+ N) ?$ h
- 80 } d5 P! n7 E" l& p2 s q: T
- 81
/ E. t# s; \/ H Q5 I) i - 82 /* Delete a cJSON structure. */2 t7 q# [+ q' y& V/ v& w- H- V
- 83 void cJSON_Delete(cJSON *c)
+ G/ n/ k) R# z - 84 {
: a% j4 w0 ^# ~7 K7 b+ H% N, j - 85 cJSON *next;' K* n1 ~ ]+ K0 Z& p
- 86 while (c)2 [ }5 d, G9 n* [5 \
- 87 {% q7 [; @+ h( X1 R0 D2 R8 A7 @ \
- 88 next=c->next;. S1 K" L& P. X
- 89 if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child);7 A( v$ j5 N* Z
- 90 if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring);
! o8 _# t9 P i - 91 if (c->string) cJSON_free(c->string);
( o. O2 E6 u' e7 O - 92 cJSON_free(c);
! @4 B% Q* ?7 D3 j# w - 93 c=next;
! R0 C( {, i+ f7 Z, \/ | - 94 }
. ]. _8 ~7 u# L% M* b/ N - 95 }. I% z; H. M& [& _
- 96
" g' b" B' r1 R/ F( J( G - 97 /* Parse the input text to generate a number, and populate the result into item. */6 n$ S) y& G& g* o2 j
- 98 static const char *parse_number(cJSON *item,const char *num)$ c) ]- z- a4 Y+ I* V6 X; v; N- _
- 99 {
! I: r3 k: U* _( n' X8 Q; K* s - 100 double n=0,sign=1,scale=0;int subscale=0,signsubscale=1;
! O* @% V4 Z2 Z - 101 , D& B/ ?& y, E
- 102 if (*num=='-') sign=-1,num++; /* Has sign? */) P2 M$ _: M2 t5 D8 W
- 103 if (*num=='0') num++; /* is zero */( `: a$ c; d. [
- 104 if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9'); /* Number? */: _( \: \3 f6 q" i% l) O
- 105 if (*num=='.' && num[1]>='0' && num[1]<='9') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');} /* Fractional part? */
% T; A# e( ~ E0 S' K - 106 if (*num=='e' || *num=='E') /* Exponent? */
- I6 Z2 v0 G0 m( j - 107 { num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; /* With sign? */
" q5 F7 n9 Q$ M4 F: R0 e - 108 while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0'); /* Number? */' p+ W3 U& Q2 g0 ?/ ?( r! O
- 109 }+ z, k+ |; u: b7 P& E
- 110
( C1 E% h( w9 Y+ l' ]/ X7 T4 k - 111 n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */
+ ~/ M$ j" q E% T3 L4 X - 112
* }( a3 P' }" K6 {) ~ - 113 item->valuedouble=n;
* J1 Y7 D" F: {. a - 114 item->valueint=(int)n;
3 d1 n+ r/ M8 [! w9 w - 115 item->type=cJSON_Number;6 D5 y% n, Q& e4 T2 j- N
- 116 return num;; X8 v9 W) ]& n" `
- 117 }! ?& ^; J- n3 k% }2 o+ g0 L# [
- 118 + s: W: d+ n" F4 ^( a! d
- 119 /* Render the number nicely from the given item into a string. */7 _/ c* X6 `# t
- 120 static char *print_number(cJSON *item)% U" I1 R$ P# s
- 121 {
8 ^. D) e2 M: c0 V. _: W/ L0 U) ` - 122 char *str;
# T6 Q' a$ K- c" i% n$ w Y' k - 123 double d=item->valuedouble;
+ v0 V9 Y" R! m - 124 if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)2 ?; ]# T: n8 a; a, B1 A1 n
- 125 {+ Z$ f& `+ y& ^- g0 I8 i" s
- 126 str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */4 T+ j0 d4 `8 o1 q
- 127 if (str) sprintf(str,"%d",item->valueint);4 i e7 ]/ o7 ~5 E
- 128 }
' {* J0 e7 K, G4 I/ C% _% d - 129 else- N8 E1 P! g) y( x6 C
- 130 { A5 {! S0 |- ?2 [: W+ Z
- 131 str=(char*)cJSON_malloc(64); /* This is a nice tradeoff. */: O$ Y( o8 T2 ^/ U) d
- 132 if (str)
9 e$ G" B, v+ T1 n - 133 {5 u* g$ r- v9 E! G6 H! G! u
- 134 if (fabs(floor(d)-d)<=DBL_EPSILON && fabs(d)<1.0e60)sprintf(str,"%.0f",d);
% p2 P3 n+ F7 I1 k( B7 i - 135 else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d);
0 O$ M `7 c8 |8 x1 [! n - 136 else sprintf(str,"%f",d);1 v6 g E9 d- f- n. n5 s) b
- 137 }
" Z' L' Z* f3 r1 @. @ - 138 }
" W, Y5 ` y: y$ M - 139 return str;; S; G# N( O7 M
- 140 }- s* t7 `( O# b/ R! _
- 141 ! _5 d- v, A7 t2 S% s
- 142 static unsigned parse_hex4(const char *str)
/ c/ _! m0 ^ w$ o- u - 143 {( S5 D$ T# A4 C1 v8 Z) A' O h; i
- 144 unsigned h=0;2 [8 i) q7 R! O+ k3 x
- 145 if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
" C" o) x8 G" K) |, I/ k - 146 h=h<<4;str++;
9 [; ~' a, ^. x6 K - 147 if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; s- r- P: U, P5 J$ y1 s2 u, u, D
- 148 h=h<<4;str++;
v+ T+ \9 w5 {: R2 x2 N: ~ - 149 if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;5 B. G( |2 y* v
- 150 h=h<<4;str++;
, E* u7 @) r. V8 K4 Q - 151 if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
/ a! V$ n% T; x% w - 152 return h;. o2 B- r8 c, F+ ~# T
- 153 }
; t- g$ e( j ?0 a- l - 154 & Y3 O& v3 w6 q4 P% e8 |
- 155 /* Parse the input text into an unescaped cstring, and populate item. */) t+ n2 [# m4 e, L
- 156 static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };2 m, G7 L! T; K# r
- 157 static const char *parse_string(cJSON *item,const char *str)8 D. ^. y; c5 K1 ~* o
- 158 {) L" N1 P, L9 R2 K6 ~+ K6 O- a9 W# }
- 159 const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc,uc2;
6 Z1 Y- u' G; p8 i, W - 160 if (*str!='"') {ep=str;return 0;} /* not a string! */+ g7 _/ s6 E8 M/ \* ~" J: e
- 161 ) g; h9 J3 j* C1 Y0 w6 I
- 162 while (*ptr!='"' && *ptr && ++len) if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */
0 \# e/ `' {) C7 c2 O1 } - 163 ( x9 z- n" O7 @, V6 W( n+ d8 L
- 164 out=(char*)cJSON_malloc(len+1); /* This is how long we need for the string, roughly. */8 s/ _6 H& l) B- D+ \) ]
- 165 if (!out) return 0;
! [; u9 _$ |" x- T - 166
$ j+ e" S' j2 k. H. _ - 167 ptr=str+1;ptr2=out;( q, z/ `1 } k8 M8 I0 V5 d
- 168 while (*ptr!='"' && *ptr)
& {1 R. Y6 A' W: f. b$ H - 169 {( X5 E( U4 P' g
- 170 if (*ptr!='\\') *ptr2++=*ptr++;, f9 Q) X! h9 q$ o+ ~0 w
- 171 else$ H' `# e& s) s" K X/ r. N
- 172 {
) O, I {# |& R A - 173 ptr++;
# \7 e, v0 Y% H6 i o - 174 switch (*ptr)
- ^* T% J% w3 W1 J - 175 {& ~# m4 ? i! N+ `
- 176 case 'b': *ptr2++='\b'; break;/ @ M5 k E; ?: q! ^ v g
- 177 case 'f': *ptr2++='\f'; break;2 N% m# E; w9 s
- 178 case 'n': *ptr2++='\n'; break;' A, R2 {. `# F" ?) r
- 179 case 'r': *ptr2++='\r'; break; Y9 p; \! r; y& M# {# |- O
- 180 case 't': *ptr2++='\t'; break;) D# a4 R8 O! `
- 181 case 'u': /* transcode utf16 to utf8. */
0 M' l" s8 z% ^% B1 H, I* [ - 182 uc=parse_hex4(ptr+1);ptr+=4; /* get the unicode char. */, S* W- b) I! Q/ R. G5 u z
- 183 4 a0 G8 V# |( }) ^1 a/ }6 |
- 184 if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0) break; /* check for invalid. */
1 l" q5 e* @( N' f, H1 Q - 185 0 D; ]* T w/ X' U: r- \# q3 P
- 186 if (uc>=0xD800 && uc<=0xDBFF) /* UTF16 surrogate pairs. */
1 f8 d5 z6 m# R3 h: \+ t - 187 {8 S4 Q; d6 `. l- M5 @4 `
- 188 if (ptr[1]!='\\' || ptr[2]!='u') break; /* missing second-half of surrogate. */
^3 v: Y) X2 R# U6 x# W+ r - 189 uc2=parse_hex4(ptr+3);ptr+=6;+ ?1 n5 d4 j5 Y0 S& s& }
- 190 if (uc2<0xDC00 || uc2>0xDFFF) break; /* invalid second-half of surrogate. */! [1 X) A; Y u. \ J
- 191 uc=0x10000 + (((uc&0x3FF)<<10) | (uc2&0x3FF));- D0 S! d2 O6 y/ Q+ {* M& ?
- 192 }+ X4 G6 N5 b8 s2 X7 m! H8 r; @4 q
- 193
9 m8 D5 X) ^/ c0 z$ l - 194 len=4;if (uc<0x80) len=1;else if (uc<0x800) len=2;else if (uc<0x10000) len=3; ptr2+=len;0 c2 R( t) W+ A, T1 y, r! ]
- 195
/ |1 u' H b7 y9 ^5 \( T3 R( H- l - 196 switch (len) {$ R+ N9 ?9 a- r1 K( E1 S. ^
- 197 case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;" u8 G' ?8 [' W& z4 \8 Q
- 198 case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
' n) P: H+ {) d7 A: i) {/ f( u - 199 case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
& [& w8 V- d( T! m - 200 case 1: *--ptr2 =(uc | firstByteMark[len]);
" L) @. b7 r- `: I$ C$ ~. _: @' ^ - 201 }1 @4 a9 D% A$ t* N3 Q" f
- 202 ptr2+=len;
) Q4 E3 A$ O# |& Q4 D - 203 break;
2 c, G" z; G2 m7 x$ V; t - 204 default: *ptr2++=*ptr; break;/ U' y$ ^! ?$ \6 Z% ?
- 205 }
0 b* P$ m+ h- o* f) B3 a - 206 ptr++;0 E$ Q8 A8 O; K( M; H% x
- 207 }
( V* L9 H" {$ J9 s0 e - 208 }
7 v9 {6 @2 `. S8 P' \ J% e! H" A8 i5 J - 209 *ptr2=0;' s6 r* \0 F5 O9 y
- 210 if (*ptr=='"') ptr++;0 Z, _8 a( N# o0 {0 l9 [& j
- 211 item->valuestring=out;# d# o- o, |2 H- c' \
- 212 item->type=cJSON_String;
0 N2 m% l' h# \- t - 213 return ptr;
" I8 w' P' |9 X2 F* I7 g1 K - 214 }, J) J8 b( o, v: P. C
- 215
* V# A2 |- S5 _0 b6 n! t) K - 216 /* Render the cstring provided to an escaped version that can be printed. */- z* G+ m1 o; I- h, }) n
- 217 static char *print_string_ptr(const char *str)
* [/ w2 X% v- i7 |) ~+ y - 218 {) L6 E# V' U7 @/ W6 @
- 219 const char *ptr;char *ptr2,*out;int len=0;unsigned char token;' I' Y4 z- H0 r, f5 v5 I: [: r
- 220
3 F; c9 X5 }" E1 I0 b - 221 if (!str) return cJSON_strdup("");1 q" H7 d1 d* k
- 222 ptr=str;while ((token=*ptr) && ++len) {if (strchr(""\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;}
; F$ l) f8 B( N. q2 I - 223 , `7 [- f$ h+ c: I7 D s' L
- 224 out=(char*)cJSON_malloc(len+3);, x# q+ T. g5 \5 d/ a
- 225 if (!out) return 0;: }& X7 M! m' x, @. V
- 226
$ y. d3 _4 D: ^7 {. @( a8 m - 227 ptr2=out;ptr=str;0 C& `5 `) s' U
- 228 *ptr2++='"';1 Q! a' U! X9 Y( L
- 229 while (*ptr)/ o4 P3 f( _8 B- e; q" x
- 230 {
+ D! B, J( y) Q' M0 N - 231 if ((unsigned char)*ptr>31 && *ptr!='"' && *ptr!='\\') *ptr2++=*ptr++;4 g* R5 e! [% ?5 n! y2 F
- 232 else
" x% f( o0 m9 s7 n7 J) A' Y: x - 233 {
- ~5 {8 j6 o, m- y: G1 E @. p - 234 *ptr2++='\\';4 N8 ~9 E. D2 I2 h$ c# N- d7 b' d
- 235 switch (token=*ptr++); C* e* g7 X' T9 d) ~* j: z
- 236 {' c: ~, |/ p1 L/ P
- 237 case '\\': *ptr2++='\\'; break;
0 E: m! m; m: }: l4 y3 J - 238 case '"': *ptr2++='"'; break;" ?+ t5 j" N1 \1 N0 @% j
- 239 case '\b': *ptr2++='b'; break;
9 I9 q5 t3 a8 L7 b8 R- _ - 240 case '\f': *ptr2++='f'; break;
+ h/ ^* f& m; {, p; L0 H1 V - 241 case '\n': *ptr2++='n'; break;9 c$ F6 ~4 X7 @ F+ t3 O
- 242 case '\r': *ptr2++='r'; break;& Y* r" R( K. u! u. Z& H
- 243 case '\t': *ptr2++='t'; break;
- q7 E4 m" E8 M/ H* h - 244 default: sprintf(ptr2,"u%04x",token);ptr2+=5; break; /* escape and print */
# \* K S$ }+ x2 |4 n - 245 }& a0 q1 q: c% i; f
- 246 }
8 ]5 H" n9 j& d0 o - 247 }' X3 x1 \6 ^5 V: G8 V" m. p/ c
- 248 *ptr2++='"';*ptr2++=0;0 F; `$ E, q7 V" Q
- 249 return out;% T" b: X& T) z$ M, d* l, A
- 250 }0 o6 W @+ ]! C$ x& \
- 251 /* Invote print_string_ptr (which is useful) on an item. */( E( G: G1 L7 ]* N/ U
- 252 static char *print_string(cJSON *item) {return print_string_ptr(item->valuestring);}4 c1 V, b$ J |2 x+ ?) E
- 253 & J% b, q& J" w( C8 y6 y
- 254 /* Predeclare these prototypes. */* j) K! N. g3 O- s! }
- 255 static const char *parse_value(cJSON *item,const char *value);
! `5 o- e! |$ r8 J l - 256 static char *print_value(cJSON *item,int depth,int fmt);
+ S5 @% `7 G1 _' o2 t' W( ], U% t - 257 static const char *parse_array(cJSON *item,const char *value);, w) e/ q3 o) q
- 258 static char *print_array(cJSON *item,int depth,int fmt);
$ N8 F# `7 g4 M+ H: ~( p - 259 static const char *parse_object(cJSON *item,const char *value);
+ N0 ^6 T- V! i# _* b2 V - 260 static char *print_object(cJSON *item,int depth,int fmt);
. U* D, P+ u0 {/ @, _ - 261 ' O, h" Z! L0 \3 K+ i
- 262 /* Utility to jump whitespace and cr/lf */& e& `5 C2 H2 Z$ Z* X9 t
- 263 static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;}
# W& ^# Z3 L; z) w/ m( a - 264 , C; u( _5 O9 e& @/ \* \
- 265 /* Parse an object - create a new root, and populate. */
7 d4 ^5 ^3 B/ T; E7 f& l/ v - 266 cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated)
' U3 Q- G# L5 N" H/ c - 267 {+ d( \8 z U7 J4 \- r/ j
- 268 const char *end=0;
3 H+ z* H% |4 u0 C" [ - 269 cJSON *c=cJSON_New_Item();
* O- _. T l2 E9 I. q - 270 ep=0;
T' p1 x4 N$ b9 \1 J - 271 if (!c) return 0; /* memory fail */
2 n9 Y8 }$ F# j - 272 # C! y1 s8 w7 V' `
- 273 end=parse_value(c,skip(value));) U- S' e1 g# ~5 H% C
- 274 if (!end) {cJSON_Delete(c);return 0;} /* parse failure. ep is set. */
0 R- ^2 g# I2 O8 T. P" V& E - 275 ( k: v! o2 g9 S9 w
- 276 /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
O6 ], n9 G6 k$ x: b - 277 if (require_null_terminated) {end=skip(end);if (*end) {cJSON_Delete(c);ep=end;return 0;}}
+ D; j V$ b6 J |% M: f - 278 if (return_parse_end) *return_parse_end=end;$ H/ {% v m, L7 o
- 279 return c;% w7 ~) Z' y! D) ?/ F4 u
- 280 }
# n2 }3 j. l B - 281 /* Default options for cJSON_Parse */
# d+ t7 T M! p6 s - 282 cJSON *cJSON_Parse(const char *value) {return cJSON_ParseWithOpts(value,0,0);}" e. N" l+ u/ I! E0 D
- 283 , G" c0 k1 a. |
- 284 /* Render a cJSON item/entity/structure to text. */
. R* i0 n, ~ n0 B4 k0 S/ m$ ~ - 285 char *cJSON_Print(cJSON *item) {return print_value(item,0,1);}
: @+ r/ {; `( L6 \ - 286 char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0);}
. P o( y Z$ E; z - 287
" K# } y$ I# v& ?" J! s - 288 /* Parser core - when encountering text, process appropriately. */
) X6 o" b3 M3 L. m - 289 static const char *parse_value(cJSON *item,const char *value)* U: V4 r& s9 t
- 290 {
+ t7 o" e6 [( G: N4 ] - 291 if (!value) return 0; /* Fail on null. */
/ v7 h* Z: W' o6 a: Y - 292 if (!strncmp(value,"null",4)) { item->type=cJSON_NULL; return value+4; }7 a* ~: r( o* T0 n/ l* S) C
- 293 if (!strncmp(value,"false",5)) { item->type=cJSON_False; return value+5; }! M, t& M* ^0 [, V( Y; m
- 294 if (!strncmp(value,"true",4)) { item->type=cJSON_True; item->valueint=1; return value+4; }4 }" `( |( D3 _4 \% \
- 295 if (*value=='"') { return parse_string(item,value); }) k( y9 d/ a& M
- 296 if (*value=='-' || (*value>='0' && *value<='9')) { return parse_number(item,value); }; f! f( y3 l" @, u/ {7 W
- 297 if (*value=='[') { return parse_array(item,value); }' k" j) M3 d7 b$ j4 ^' i
- 298 if (*value=='{') { return parse_object(item,value); }
/ c% |* {; k' i; A - 299
" o5 y3 F- c! y- i( M - 300 ep=value;return 0; /* failure. */
9 c# C% X' I. q- h - 301 }! X V' [; a) C2 u
- 302 ( _' q5 P4 ~- n+ r& y2 S$ B: U
- 303 /* Render a value to text. */
7 i- [- F0 j8 m/ ^7 N6 C - 304 static char *print_value(cJSON *item,int depth,int fmt)
+ R' x6 I- b. K+ W - 305 {2 j% [8 N9 H4 @! ?/ T# U4 m- Z- d
- 306 char *out=0;
D% Q8 y5 X3 v. Y" R& x7 {; V/ O - 307 if (!item) return 0; ?( T0 o8 W) W/ F% K2 q6 C' S
- 308 switch ((item->type)&255)& `# v; N" ^$ f% @' y) i* ^/ E
- 309 {
' t7 U+ w i( ^/ D$ l - 310 case cJSON_NULL: out=cJSON_strdup("null"); break;2 b2 ~3 f3 ~% \2 t
- 311 case cJSON_False: out=cJSON_strdup("false");break;
/ p( |6 Y9 E4 v - 312 case cJSON_True: out=cJSON_strdup("true"); break;
7 z- |1 q7 r( \( L8 o' r' P - 313 case cJSON_Number: out=print_number(item);break;! l: V0 ?& p% F6 t3 s& H& C |
- 314 case cJSON_String: out=print_string(item);break;
$ }8 Q% q. ^3 P" B& v- I4 O - 315 case cJSON_Array: out=print_array(item,depth,fmt);break;+ G& j. L' @9 {! e$ P
- 316 case cJSON_Object: out=print_object(item,depth,fmt);break;
- u+ H& X# C: r# Z# R) P- _' t - 317 }( V" {# {" }* ]
- 318 return out;
6 s7 p [% l2 s: H- W - 319 }
" o7 p7 z" ?# _( ? - 320 [) ?4 [5 j3 B, Y+ c
- 321 /* Build an array from input text. */; x7 @5 U5 E5 _6 ?! K% Q2 z
- 322 static const char *parse_array(cJSON *item,const char *value)
8 S% g t: p' d/ ^- V - 323 {. J( H7 e/ e# R3 w( Z: G8 ]
- 324 cJSON *child;
" Y1 d9 w( i" V, x/ ^7 j& R - 325 if (*value!='[') {ep=value;return 0;} /* not an array! */% q% I; ]% k d1 Q) L4 Q
- 326 8 e! t2 m0 j) }9 y" ?4 ]
- 327 item->type=cJSON_Array;9 B, P+ K `$ f0 |3 ]
- 328 value=skip(value+1);
# m4 l) V* u e$ ?: h3 p - 329 if (*value==']') return value+1; /* empty array. */. L9 ~0 z8 M% c" ^& V. k
- 330 ) R7 n% {. \! r
- 331 item->child=child=cJSON_New_Item();
* B# G* l- S8 O! ? - 332 if (!item->child) return 0; /* memory fail */
+ V' v+ ]/ q- k/ P* J - 333 value=skip(parse_value(child,skip(value))); /* skip any spacing, get the value. */
* H; y; [/ z* ^8 l# F ]5 H - 334 if (!value) return 0;
$ f2 R6 S) @9 {/ M6 ~$ } - 335
7 Z; Y% |: D5 S: Z* k# j& D8 a - 336 while (*value==',')
6 u. b# z# t( r1 b" ?" H - 337 { B. S5 Q1 i( X% f
- 338 cJSON *new_item;2 P. C5 ~0 I$ t2 z) S+ U
- 339 if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */
( X" G: F) q( @ O% T' K7 G1 } - 340 child->next=new_item;new_item->prev=child;child=new_item;
. x1 }/ F9 s8 d* W# R7 W - 341 value=skip(parse_value(child,skip(value+1)));
; k4 k: o/ f; q! G9 j s, @, J8 O: w - 342 if (!value) return 0; /* memory fail */% R; K$ R; f, i( H
- 343 }) m" q% K) {1 ~; X3 {1 R8 o
- 344 ! l8 p! c3 o! r# T ~- I
- 345 if (*value==']') return value+1; /* end of array */: n( F; {: t1 G5 s; r) A* c
- 346 ep=value;return 0; /* malformed. */
+ O7 i# G, l- X; s/ _ - 347 }
- r( V$ X" Q! S: X+ i+ [& ] - 348 8 A; y" Q2 h+ {4 J, Y4 b" {# W
- 349 /* Render an array to text */
" b9 `8 w( @, R/ }1 s - 350 static char *print_array(cJSON *item,int depth,int fmt)
7 X: ~5 M d4 `6 T" S1 u2 K5 O - 351 {6 U8 P0 w# r0 [1 o
- 352 char **entries;) g% l1 g+ T9 T; f8 J) e k" y7 ^
- 353 char *out=0,*ptr,*ret;int len=5;! v9 l/ w w6 g. b; K, B6 |
- 354 cJSON *child=item->child;. g& ^; y5 K8 J& _0 \
- 355 int numentries=0,i=0,fail=0;8 ~2 ]$ K! b% b# J1 E
- 356
0 ~) ^( O" T1 q3 D2 [3 G+ Q - 357 /* How many entries in the array? */
4 N% R. A% C: X5 \ - 358 while (child) numentries++,child=child->next;
7 U. t1 ?; w6 M" I - 359 /* Explicitly handle numentries==0 */
% a9 X( [8 _* _; c - 360 if (!numentries)
- K2 E2 [ T9 Z' i3 w3 ? - 361 {! z9 K: \: X4 F
- 362 out=(char*)cJSON_malloc(3);
+ i Y6 i( A( ?7 L1 ^ - 363 if (out) strcpy(out,"[]");
# x- |: J3 o9 g& c/ ~8 H - 364 return out;* Y% q* y7 Y" @! e$ v# ~% b5 W
- 365 }
0 `( C! l+ g% }- K' ~ - 366 /* Allocate an array to hold the values for each */+ O3 ^& z" y6 W5 C( M5 ?" ?* l
- 367 entries=(char**)cJSON_malloc(numentries*sizeof(char*));: u! S, t! u, U7 ~' H; l* K' p1 e
- 368 if (!entries) return 0;
( _. D. }7 b" M - 369 memset(entries,0,numentries*sizeof(char*));
l' \& y3 M; E/ q - 370 /* Retrieve all the results: */7 t9 v1 B7 v+ ~! H
- 371 child=item->child;
+ M! ?7 B0 K. F9 [% Q+ Y - 372 while (child && !fail)
3 ?3 Y* x; k6 m - 373 {
, E0 e9 |/ `, ?" B( w9 l - 374 ret=print_value(child,depth+1,fmt);) S H8 ?- k) T9 ?
- 375 entries[i++]=ret;
3 y1 F* q/ ?: S$ \& Z5 H - 376 if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1;/ d+ H: y' [4 s7 s* x( F/ q1 f
- 377 child=child->next;% K2 X7 V1 S |
- 378 }3 i# V1 u& [ K8 Z5 M! l" D
- 379 ) N. s' N7 c' m' r' i' U0 j
- 380 /* If we didn't fail, try to malloc the output string */
7 j S) r" C- h; m+ P - 381 if (!fail) out=(char*)cJSON_malloc(len);' P( J0 l m8 c1 O# p
- 382 /* If that fails, we fail. */# r( i* W" w1 S |+ `" o' N3 T
- 383 if (!out) fail=1;
' Z6 M0 h$ X( b+ N! X# p - 384 " ]: Y5 i6 ?/ o5 j7 N3 H2 T. a
- 385 /* Handle failure. */
7 C; k1 T- n& t( x3 g' k5 {, {, ` - 386 if (fail)
1 f2 U" f, ?8 r2 o; c# O - 387 {
( J+ U" m1 r1 O - 388 for (i=0;i<numentries;i++) if (entries[i]) cJSON_free(entries[i]);
0 Q; }+ b& z- u& v& {& Z - 389 cJSON_free(entries);4 f6 F d9 i5 I# j( @: d4 ^2 o2 Y
- 390 return 0;7 `# w. G5 l- ?7 d! R
- 391 }
3 I4 u1 W% [2 [7 C0 j! N+ X. Y - 392
* U. |7 z4 a; U0 k - 393 /* Compose the output array. */
( x) }# ?5 E+ |8 e. \8 M - 394 *out='[';
1 U9 V3 J' _3 }% j# I7 T6 Y - 395 ptr=out+1;*ptr=0;
# b1 l' S5 h: t% }2 o+ r. {3 j - 396 for (i=0;i<numentries;i++) J' {# b. `( S5 [ E! Y
- 397 {) ^& I: q7 o" Q
- 398 strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
! k% q) S8 }3 X, d8 x# p7 a - 399 if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;}) W/ ~0 C4 N" Y/ x$ h& G( F) M: v; ?
- 400 cJSON_free(entries[i]);
) V% Z+ |( m. B, ?# ?; r4 O; N - 401 }/ v" O2 U; Z: \( D; P( J( d* M
- 402 cJSON_free(entries);
% R) {# u; _+ S4 m( A" m - 403 *ptr++=']';*ptr++=0;4 d, c& p5 g. ^7 J
- 404 return out;
8 o: R5 Y$ y8 D1 [& m/ x6 Y - 405 }, v$ w- y# P9 \' T- X
- 406
. y0 R# q+ [# x8 }8 F% M; S - 407 /* Build an object from the text. */8 q5 [- R5 y0 @# n; e
- 408 static const char *parse_object(cJSON *item,const char *value); d8 e' x4 W9 R" O
- 409 {
1 I" A5 ?1 ^6 `: j: n - 410 cJSON *child;
! K2 M, Y* S& ^5 T8 t- U - 411 if (*value!='{') {ep=value;return 0;} /* not an object! */
3 M" N. x% J3 i" D - 412 8 U0 A9 e* O. I9 d% z/ d- p
- 413 item->type=cJSON_Object;/ h( e6 j% s8 c4 A6 Z: b. [" D L& G
- 414 value=skip(value+1);
5 r3 A* D4 u* i' j, X4 L$ _ - 415 if (*value=='}') return value+1; /* empty array. */* G5 Q5 E. V+ `$ ^ j- \
- 416
8 |6 ^- E; }/ X P2 d; J7 R - 417 item->child=child=cJSON_New_Item();3 f( S9 A* m' {/ }+ v5 l5 e! U
- 418 if (!item->child) return 0;
* z5 l2 E( r. ~& } - 419 value=skip(parse_string(child,skip(value)));
& L7 \! W+ Q9 Y7 @9 m8 C - 420 if (!value) return 0;4 O7 g3 r# U% O- f6 m
- 421 child->string=child->valuestring;child->valuestring=0; r2 m, k6 G2 y/ c5 g
- 422 if (*value!=':') {ep=value;return 0;} /* fail! */1 o6 {. b, m: s* ?/ w
- 423 value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */
' _% s" S- A5 h0 G' }$ G - 424 if (!value) return 0;
# F$ W4 M# d* X - 425 # m. a) V' H3 B; i
- 426 while (*value==',')
, `6 e+ \& r. n - 427 {, z7 V# r; g2 ~/ e; O! b
- 428 cJSON *new_item;
6 o) d* \* p' E+ K( c - 429 if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */' H! O6 L- W; X' I
- 430 child->next=new_item;new_item->prev=child;child=new_item;: ~4 \/ I1 I/ b# i) H5 g/ O
- 431 value=skip(parse_string(child,skip(value+1)));2 @1 x$ s4 z3 @
- 432 if (!value) return 0;& L( O4 j7 i; o0 W+ q
- 433 child->string=child->valuestring;child->valuestring=0;$ l, i* {8 `9 v+ _. }
- 434 if (*value!=':') {ep=value;return 0;} /* fail! */
5 _! n' \+ x& Z8 D1 |& H9 x - 435 value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */9 T4 K( f0 J: x5 E7 @3 a" p3 q
- 436 if (!value) return 0;3 N7 o/ [/ m- p
- 437 }
' E4 q6 J* Q4 p0 G5 m/ g8 C ] - 438
/ o3 J S. C. s! ?4 p - 439 if (*value=='}') return value+1; /* end of array */* D0 Z4 A4 R/ ^, h8 X4 B
- 440 ep=value;return 0; /* malformed. */
3 f/ A6 h, X7 z8 b* g/ Q7 Q - 441 }. W4 B8 z8 e, b {' n
- 442
6 |0 `9 Z( |/ m( | b- y0 V1 H/ C - 443 /* Render an object to text. */, { G! H& ]* W* o
- 444 static char *print_object(cJSON *item,int depth,int fmt)5 N" ^$ [4 c- Q. \/ l& {
- 445 {
2 v6 p# z3 G1 o2 F+ K/ D- s. @- ~ - 446 char **entries=0,**names=0;
9 W8 j8 ?1 c8 i - 447 char *out=0,*ptr,*ret,*str;int len=7,i=0,j;
2 s( S: k- E; g- R! k - 448 cJSON *child=item->child;& b4 [1 }# j& R* j: e
- 449 int numentries=0,fail=0;
( I) A/ o; y; V - 450 /* Count the number of entries. */, \5 @1 d4 O P( L6 n5 ~& ]/ I
- 451 while (child) numentries++,child=child->next;5 R* p# F* H: |, B) |8 @1 c
- 452 /* Explicitly handle empty object case */
$ J* W5 E, B, k) z6 P) g6 T - 453 if (!numentries)/ m1 e* m* `1 `! j% x, Y9 a
- 454 {/ g% y" r7 g" h8 Y1 u0 K
- 455 out=(char*)cJSON_malloc(fmt?depth+4:3);2 m5 f3 r' P" J. C% v, ?) S
- 456 if (!out) return 0;! E+ N( Z) @! e3 `6 `& ~
- 457 ptr=out;*ptr++='{';
9 w3 `5 s2 i1 y, R; h, }* ~ - 458 if (fmt) {*ptr++='\n';for (i=0;i<depth-1;i++) *ptr++='\t';}
& Z% W! G3 X2 k s; G- ` ^ ^ - 459 *ptr++='}';*ptr++=0;
8 p) ~6 T0 e2 Y$ k/ E - 460 return out;
; F. Q5 ?) t# T2 K! n! D5 O - 461 }% V: A: B$ J7 F/ A
- 462 /* Allocate space for the names and the objects */
. d t4 B2 y$ V - 463 entries=(char**)cJSON_malloc(numentries*sizeof(char*));
4 S" ?' L/ S6 O; e. G+ q - 464 if (!entries) return 0;8 B: C+ B* t `1 B( Z Y1 P
- 465 names=(char**)cJSON_malloc(numentries*sizeof(char*));
( H6 N ^; M* s1 j$ ~! z" u- W$ c4 | - 466 if (!names) {cJSON_free(entries);return 0;}- |+ ]5 \' W1 z" Z+ U2 R7 I
- 467 memset(entries,0,sizeof(char*)*numentries);
3 A+ ]8 ]9 a% e8 \" V- z" T( ^ - 468 memset(names,0,sizeof(char*)*numentries);
- t7 `+ W' j% V: |! ?6 H! h. K - 469
1 W& g* o$ h- m; T3 ^ - 470 /* Collect all the results into our arrays: */$ o2 ^/ F8 {& T' r8 `: L3 f0 H, x
- 471 child=item->child;depth++;if (fmt) len+=depth;
# }" v# O- l3 ] - 472 while (child)
- }3 N7 |+ H, p0 i - 473 {
3 K1 d4 s4 I, |6 @4 k - 474 names[i]=str=print_string_ptr(child->string);
2 _3 s. ~6 u& l* p - 475 entries[i++]=ret=print_value(child,depth,fmt);3 H9 l5 e0 T4 ~3 K2 u. D
- 476 if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1;! N {# }) O( z4 e8 h" b5 Y9 t
- 477 child=child->next;+ y) q- h! q2 l O, r' |" r
- 478 }
# g0 T2 L5 ~) s5 }, A" R - 479 ' Y) N: F1 a9 K
- 480 /* Try to allocate the output string */+ p/ F/ p5 F. y+ ]1 }4 o3 f
- 481 if (!fail) out=(char*)cJSON_malloc(len);
( B$ i5 F. `8 q+ w q' ^+ A' D - 482 if (!out) fail=1;
7 j! c! R z' i* s0 O1 E# Z, G - 483 % K. T4 u" O5 w
- 484 /* Handle failure */ J$ b1 F, g4 }5 o$ H5 O
- 485 if (fail)
. Y% w2 ^0 V! g# |9 \3 r - 486 {
) m/ d, k* t5 d5 @2 H - 487 for (i=0;i<numentries;i++) {if (names[i]) cJSON_free(names[i]);if (entries[i]) cJSON_free(entries[i]);}! C! F! q, c3 j# s; |
- 488 cJSON_free(names);cJSON_free(entries);
& v4 j7 B; F8 `, d4 `$ x - 489 return 0;
/ `+ v" x. H' l1 U6 Q0 V9 ` - 490 }: q! _3 B9 S: w% G0 C6 x* n
- 491 $ \) A7 q( K5 C: X% v
- 492 /* Compose the output: */
+ n6 p# e4 b6 u - 493 *out='{';ptr=out+1;if (fmt)*ptr++='\n';*ptr=0;6 @( q' }. ~9 A, D; q- c
- 494 for (i=0;i<numentries;i++)
6 j3 }% `6 q u& R, N) V! e - 495 {! p" m9 v; `; y- H4 |
- 496 if (fmt) for (j=0;j<depth;j++) *ptr++='\t';
7 D3 a3 l* p+ S1 A: J( W - 497 strcpy(ptr,names[i]);ptr+=strlen(names[i]);5 Z' L. j7 Y0 S6 Y, Q2 J
- 498 *ptr++=':';if (fmt) *ptr++='\t';
) M. H. ^$ f# z8 k5 O; Y- A g. g - 499 strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
. i5 @+ K, [- L6 D0 i - 500 if (i!=numentries-1) *ptr++=',';% t' K7 h3 R8 n$ H0 p6 |5 B
- 501 if (fmt) *ptr++='\n';*ptr=0;
; q$ U: u8 I/ g7 x% O x - 502 cJSON_free(names[i]);cJSON_free(entries[i]);
o. ]( w) P/ B/ e" e, \5 N$ Z - 503 }; U. u/ j3 V ]# ?
- 504
) ]! n2 S, s$ p1 I' r - 505 cJSON_free(names);cJSON_free(entries);: U2 \( k, F4 n- V* U( z5 Z
- 506 if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t';
4 |: Q( o. j" y - 507 *ptr++='}';*ptr++=0;: } S/ X* h# f7 \8 X
- 508 return out;
5 ~+ ] a( |) y' H9 v* D - 509 }
: r5 v- E+ `2 c6 B7 Q9 O+ P5 B - 510 3 V3 l4 _* d3 `0 m
- 511 /* Get Array size/item / object item. */# b9 G$ f( L1 U q- V, A
- 512 int cJSON_GetArraySize(cJSON *array) {cJSON *c=array->child;int i=0;while(c)i++,c=c->next;return i;}" u% P/ a6 H/ e
- 513 cJSON *cJSON_GetArrayItem(cJSON *array,int item) {cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;}
* y- F/ _; ^* w6 n# J - 514 cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;}
- i, w+ S- j, I& @ o) @ - 515
! v6 \+ k0 h, C2 q: h - 516 /* Utility for array list handling. */
0 t$ c1 X- |# O7 O5 Y! g; {% n0 l5 v - 517 static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;}2 m) b# |( W) E' z @& b9 _
- 518 /* Utility for handling references. */
7 B ~- g; s: c - 519 static cJSON *create_reference(cJSON *item) {cJSON *ref=cJSON_New_Item();if (!ref) return 0;mymemcpy(ref,item,sizeof(cJSON));ref->string=0;ref->type|=cJSON_IsReference;ref->next=ref->prev=0;return ref;}
) H% [. ^/ @" W' {8 `0 k8 G: g6 E - 520
6 a, [+ { P: z - 521 /* Add item to array/object. */
+ `! c; R7 c6 c; D0 B& j - 522 void cJSON_AddItemToArray(cJSON *array, cJSON *item) {cJSON *c=array->child;if (!item) return; if (!c) {array->child=item;} else {while (c && c->next) c=c->next; suffix_object(c,item);}}$ ~5 a- M. T. x% j2 G
- 523 void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item) {if (!item) return; if (item->string) cJSON_free(item->string);item->string=cJSON_strdup(string);cJSON_AddItemToArray(object,item);}
V2 O: o; y9 R) H3 Y( L5 P - 524 void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {cJSON_AddItemToArray(array,create_reference(item));}9 t! f* c! b5 ]/ R, C
- 525 void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item) {cJSON_AddItemToObject(object,string,create_reference(item));}
+ {4 j. B2 _, b! J8 y1 }' K - 526
+ q1 v9 j/ K' E6 w) b1 E - 527 cJSON *cJSON_DetachItemFromArray(cJSON *array,int which) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0;9 n7 O' `! r( R, L: s! g9 J
- 528 if (c->prev) c->prev->next=c->next;if (c->next) c->next->prev=c->prev;if (c==array->child) array->child=c->next;c->prev=c->next=0;return c;}
/ M0 i1 ?' M4 ], f/ O - 529 void cJSON_DeleteItemFromArray(cJSON *array,int which) {cJSON_Delete(cJSON_DetachItemFromArray(array,which));}7 q* ?9 J \! R& H
- 530 cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string) {int i=0;cJSON *c=object->child;while (c && cJSON_strcasecmp(c->string,string)) i++,c=c->next;if (c) return cJSON_DetachItemFromArray(object,i);return 0;}
7 \8 b0 w+ b5 i- Q* h! a - 531 void cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));} {+ [5 m3 j1 o, o) K8 U1 n6 \
- 532
u: j. ?) Z3 e Q0 U. p - 533 /* Replace array/object items with new ones. */
/ W5 _9 ^( e: \2 y - 534 void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return;! s8 a3 o9 ~4 O9 c% w5 [% k
- 535 newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem;
3 T6 K* W' J! y3 C% ^7 X - 536 if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);}3 e# ]' t5 K3 j. W
- 537 void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=object->child;while(c && cJSON_strcasecmp(c->string,string))i++,c=c->next;if(c){newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}}
, o8 Z8 Q* z1 }9 k; ?1 m: I3 A - 538 1 g) R; h' C6 a; I2 e& F
- 539 /* Create basic types: */+ Y( W; R) h: ]- v7 A
- 540 cJSON *cJSON_CreateNull(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_NULL;return item;}
1 H* B2 a9 U; ?+ _7 K - 541 cJSON *cJSON_CreateTrue(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_True;return item;}
. h+ A; b; s D d- R - 542 cJSON *cJSON_CreateFalse(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_False;return item;}+ G4 r( g3 @9 j& a F$ s/ U: A* n
- 543 cJSON *cJSON_CreateBool(int b) {cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;}
7 U! T* G, {6 _) e - 544 cJSON *cJSON_CreateNumber(double num) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_Number;item->valuedouble=num;item->valueint=(int)num;}return item;}1 c ]' f+ D4 X: i5 @6 D& F& V# ^# ?: o( [
- 545 cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;}
0 H% ~/ ~$ j9 t n' o% q7 u - 546 cJSON *cJSON_CreateArray(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;}/ ~9 X- W. ]& S! e9 C, {0 H5 q, P
- 547 cJSON *cJSON_CreateObject(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;}; X" ~1 r0 w9 v4 }$ l8 t4 O; r
- 548 . o8 `8 a3 D; W* q
- 549 /* Create Arrays: */0 y5 Y9 X! |& b3 l, {/ J; R
- 550 cJSON *cJSON_CreateIntArray(const int *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
9 N/ `/ ^- H( q3 W - 551 cJSON *cJSON_CreateFloatArray(const float *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}: T( y& j1 ?; r$ x, p
- 552 cJSON *cJSON_CreateDoubleArray(const double *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
" A* S( M( H. \- s* ]$ V - 553 cJSON *cJSON_CreateStringArray(const char **strings,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateString(strings[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}# O# G% r- ~! m0 [) w& e# Q0 j
- 554
; Q8 i! I, j: r& X7 @% c% G$ k - 555 /* Duplication */: n( [' [2 ^6 }8 q2 j3 O1 G& B
- 556 cJSON *cJSON_Duplicate(cJSON *item,int recurse)& g4 o" m3 P7 _
- 557 {
3 ~" e* Z% s' _( s2 R - 558 cJSON *newitem,*cptr,*nptr=0,*newchild;
J o" ?4 y8 z; |; S - 559 /* Bail on bad ptr */+ | T4 A: l0 x
- 560 if (!item) return 0;
@! F& U# p5 Y- Q - 561 /* Create new item */
9 q, M7 }) A3 m- Y - 562 newitem=cJSON_New_Item();1 I7 F" P+ j: B+ S: I/ w
- 563 if (!newitem) return 0;' I. Q2 q. O) D6 H9 s% w
- 564 /* Copy over all vars */
0 e; V0 Z Z% C - 565 newitem->type=item->type&(~cJSON_IsReference),newitem->valueint=item->valueint,newitem->valuedouble=item->valuedouble;
" t: a# N d7 p) J5 D8 p - 566 if (item->valuestring) {newitem->valuestring=cJSON_strdup(item->valuestring); if (!newitem->valuestring) {cJSON_Delete(newitem);return 0;}}: ^0 T, {: |* y2 F
- 567 if (item->string) {newitem->string=cJSON_strdup(item->string); if (!newitem->string) {cJSON_Delete(newitem);return 0;}}! \# l& F$ ^' m
- 568 /* If non-recursive, then we're done! */
; S1 W# l% @2 t5 O. N: ^( J7 T4 W - 569 if (!recurse) return newitem;
8 s4 E. {+ d/ I/ ^ - 570 /* Walk the ->next chain for the child. */
" c* c- p8 t1 e2 `4 k - 571 cptr=item->child;
8 k3 X& p }# f, S) x - 572 while (cptr)8 v+ c' k' F1 c$ C9 T! c1 b# L. W
- 573 {
# v" c1 Z! G" `5 A1 F' e - 574 newchild=cJSON_Duplicate(cptr,1); /* Duplicate (with recurse) each item in the ->next chain */! y5 }5 @" K" g, }' z8 R, C
- 575 if (!newchild) {cJSON_Delete(newitem);return 0;}! x! A4 S! r4 C, W7 Z
- 576 if (nptr) {nptr->next=newchild,newchild->prev=nptr;nptr=newchild;} /* If newitem->child already set, then crosswire ->prev and ->next and move on */
# y0 Z8 r8 p, H" Y# [8 a - 577 else {newitem->child=newchild;nptr=newchild;} /* Set newitem->child and move to it */7 g9 v! |0 o$ f, W
- 578 cptr=cptr->next;2 t! J1 a/ n' n, v8 T! v
- 579 }
9 m) B+ b, x$ X$ j: W - 580 return newitem;7 r% ]( }& @8 M3 m, g
- 581 }/ `: T# d4 {/ P& @
- 582
/ U7 g: r8 t0 f$ F - 583 void cJSON_Minify(char *json)
4 R4 i, b9 |) G, c - 584 {7 R' w9 v" t# C I! v
- 585 char *into=json;
# @ p, G, j' ~ - 586 while (*json)
9 r ~! Y$ c4 u7 ^ - 587 {. p+ d2 J: |8 l6 h, e$ ~6 J
- 588 if (*json==' ') json++;8 }4 |: V3 r1 L+ [( y! Z% J
- 589 else if (*json=='\t') json++; // Whitespace characters.
% W: w7 s5 h6 f) F9 j) \+ e! l - 590 else if (*json=='\r') json++;# S- b: g5 t$ ^' C8 i1 W: f
- 591 else if (*json=='\n') json++;8 C9 @0 o* W- D) b% m) e
- 592 else if (*json=='/' && json[1]=='/') while (*json && *json!='\n') json++; // double-slash comments, to end of line.
( b7 f- T3 S# Y% _ - 593 else if (*json=='/' && json[1]=='*') {while (*json && !(*json=='*' && json[1]=='/')) json++;json+=2;} // multiline comments.: q [* o1 N6 l0 _4 B8 T- p; ?2 d
- 594 else if (*json=='"'){*into++=*json++;while (*json && *json!='"'){if (*json=='\\') *into++=*json++;*into++=*json++;}*into++=*json++;} // string literals, which are " sensitive.
$ x# ]. Y' `. f) u+ P- P - 595 else *into++=*json++; // All other characters.& {& n8 A" W+ o5 v
- 596 }
, @$ ?) S) P6 h1 E" f4 a( s6 { - 597 *into=0; // and null-terminate.
" f7 b, k7 \5 }1 k( L" C0 F5 Z - 598 }cJSON.h内容如下4 R& g/ r% ^# i: L
- 1 /*' t$ ^7 r/ H' H
- 2 Copyright (c) 2009 Dave Gamble
2 q3 M' U( ]4 @. ?/ [) O8 \ - 3 6 r# k: J. w7 K" p3 g( y6 c
- 4 Permission is hereby granted, free of charge, to any person obtaining a copy2 t6 A/ `' v& p( b
- 5 of this software and associated documentation files (the "Software"), to deal$ y/ }& _/ D2 Z" N* ^3 K
- 6 in the Software without restriction, including without limitation the rights
9 e8 N+ E) b d' k3 x. o - 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
) R/ u- V" L% J/ X5 [% J- a4 z - 8 copies of the Software, and to permit persons to whom the Software is& F/ \0 G! J4 f0 T" ]
- 9 furnished to do so, subject to the following conditions:# J5 K4 f; ]6 B
- 10 / g) k; I+ [- j* `$ p
- 11 The above copyright notice and this permission notice shall be included in
- l0 d1 \. E/ `; { - 12 all copies or substantial portions of the Software.
2 R, B, o* `, Z( z3 S - 13
/ U2 L8 K( P1 s P - 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR$ X7 F, E1 g! q+ o% U) B
- 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
N9 v1 O, x' M. n8 @) m" K - 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
6 R4 ]$ H2 z* Q7 _# c - 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER8 @4 r6 X* q8 n' K3 G* V- k
- 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
7 r# `9 b: W8 F: [$ m" k$ L - 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* i' H. ^. L: O3 g! s - 20 THE SOFTWARE.
+ K f: L8 L; ?6 r/ B6 ~ - 21 */
3 ~: I( F4 m& i. N$ C% d! ? - 22
5 k; Q+ [/ c# l/ G9 E$ X/ Y - 23 #ifndef cJSON__h
, T' w% ?7 O4 k! h7 @ - 24 #define cJSON__h: e# \& s* g9 N8 P
- 25 5 o! A5 q8 \. O. o! X
- 26 #include <stddef.h>1 F. i) K. X4 \+ g# I6 \. }: h
- 27 ) `% f4 @) V1 y, @& n8 t; u; Z
- 28 #ifdef __cplusplus
L: n) U. L7 b6 {6 E8 q - 29 extern "C"5 f3 P9 s5 p. W3 U6 k# B! Y9 {
- 30 {. h' I5 x' F5 b ]/ Z3 r0 n3 a
- 31 #endif- j- V' @3 V- ]# |3 w+ O
- 32 8 D7 Q) S, `8 s
- 33 /* cJSON Types: */% m* ^2 d% B/ T2 J
- 34 #define cJSON_False 0
' d @& C, b7 x" v/ L$ `5 ` - 35 #define cJSON_True 1! _9 ]0 ?& ^. K0 U
- 36 #define cJSON_NULL 21 Y, w* p0 A- ~' L4 a7 }, X
- 37 #define cJSON_Number 36 v M7 E3 B) `% y8 C/ n) [6 ]
- 38 #define cJSON_String 4
; K9 m2 [4 m" n3 y3 l6 t: ` - 39 #define cJSON_Array 54 H$ [9 \% h2 P6 A1 @
- 40 #define cJSON_Object 6/ b) L& j- g% l! [2 Z7 K
- 41 4 y, n2 C [7 {3 r j
- 42 #define cJSON_IsReference 256
. X4 `6 ^' g! l* p3 [ - 43
a0 ] V! Q4 l7 z: _ - 44 /* The cJSON structure: */
5 p$ d8 m* R5 v0 K/ o! [ E- ?; { - 45 typedef struct cJSON {
* u+ I9 Z, A+ T5 d1 \; B2 j - 46 struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */" |$ I* y6 X* W( Q& v
- 47 struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */& ?: W% a$ L- h$ O0 ~- B
- 48
! n/ b5 V1 {+ r$ Q! q7 o0 i' m - 49 int type; /* The type of the item, as above. */
( E* q/ E$ s! @: z3 P9 m+ t - 50 # u% @. g1 B k, J% ~) v6 p
- 51 char *valuestring; /* The item's string, if type==cJSON_String */( Q; w. G/ W" H0 {- c6 {* s
- 52 int valueint; /* The item's number, if type==cJSON_Number */
& S0 s7 ] t# i - 53 double valuedouble; /* The item's number, if type==cJSON_Number */: m$ \, v& n- x, C
- 54
8 Z7 ^, r3 C; ]1 s - 55 char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */' Q+ E+ t- C% w* r
- 56 } cJSON;" B6 f" l% M5 N
- 57 , E0 o; r9 F" V% W* O
- 58 6 t2 g1 D9 U1 \% s
- 59 2 @9 ]1 p4 y3 |2 d0 p
- 60 typedef struct cJSON_Hooks {
0 V z/ l) [' |& g" p* d# A - 61 void *(*malloc_fn)(size_t sz);, j/ y+ z, l; T6 B+ T5 e: [
- 62 void (*free_fn)(void *ptr);
( S% @1 g# Q$ D - 63 } cJSON_Hooks;( \( L9 u: \1 k0 }0 A' P
- 64 + {- }& ^9 p8 U1 c! e8 p9 C
- 65 /* Supply malloc, realloc and free functions to cJSON */6 F5 q4 D8 V D2 m; |
- 66 extern void cJSON_InitHooks(cJSON_Hooks* hooks);, x! T' _1 ~! N
- 67
" \/ T2 i4 `) w5 V3 p/ p9 B2 W - 68
# q' k7 Q, [6 c2 m ]9 H1 v - 69 /* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
/ k! V; B4 x/ B - 70 extern cJSON *cJSON_Parse(const char *value);
! b* V4 b$ [$ w! O- o( P. ] - 71 /* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
+ c! h& }1 v5 ]+ P" A% r - 72 extern char *cJSON_Print(cJSON *item);
: D3 r- ?% S2 f& \ - 73 /* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */7 a2 r4 `. a& z3 E0 Z y
- 74 extern char *cJSON_PrintUnformatted(cJSON *item);4 W/ d: l9 y* o9 M
- 75 /* Delete a cJSON entity and all subentities. */
: J* R9 c6 d* @) [ - 76 extern void cJSON_Delete(cJSON *c);% M$ F' v, |3 U& a' f1 K3 c; w- U
- 77 ! U: A- u/ y+ A: y# T, l0 S0 z
- 78 /* Returns the number of items in an array (or object). */. y# c# b3 Z0 o, W
- 79 extern int cJSON_GetArraySize(cJSON *array);8 s k3 i$ R1 Z6 K" P
- 80 /* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */6 J9 d7 z; k0 U& M
- 81 extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
- E0 A1 j0 y* R" f. c4 z8 Y - 82 /* Get item "string" from object. Case insensitive. */4 x, J0 d" u) v8 h( r, {6 w
- 83 extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
`8 t& e7 @) k5 K - 84 , _! \- Q2 q- u0 ~6 t
- 85 /* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
: X' O6 H) v7 h5 s- u - 86 extern const char *cJSON_GetErrorPtr(void);, K; x' f. O) G3 p% }+ \6 V, ^
- 87
7 [) V/ b( s# `* P% X - 88 /* These calls create a cJSON item of the appropriate type. */6 {2 B9 T/ X$ e( x* W5 I/ \# f
- 89 extern cJSON *cJSON_CreateNull(void);
4 ^: r) A% _2 t$ `! J - 90 extern cJSON *cJSON_CreateTrue(void);# u# S" a- d6 _9 x# U
- 91 extern cJSON *cJSON_CreateFalse(void);
# B) C. V( Q: v/ V- o! d - 92 extern cJSON *cJSON_CreateBool(int b);9 K0 ?, b7 E7 |- ?/ j
- 93 extern cJSON *cJSON_CreateNumber(double num);7 E+ v: z7 a: Y4 H& F# K% \9 V4 W
- 94 extern cJSON *cJSON_CreateString(const char *string);" B5 e, Q ?3 o- l i
- 95 extern cJSON *cJSON_CreateArray(void);
5 S, B6 l/ B& I! O4 G - 96 extern cJSON *cJSON_CreateObject(void);
3 P* v6 }! Q! d" s1 { ?4 x7 A! O - 97 1 V' H9 c: z1 X" H9 c) o- V, H' j
- 98 /* These utilities create an Array of count items. *// ^+ k2 X9 { I
- 99 extern cJSON *cJSON_CreateIntArray(const int *numbers,int count);
! Q" R6 v( I" k" l! M0 w1 F - 100 extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count);
4 h. c7 l! O$ `! w3 ^: I1 f - 101 extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count);
8 B1 ] ~8 `5 U g# ^ - 102 extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
. A6 B w! K& K$ K6 ] - 103
9 p$ } b5 Y2 B& ]' `* P - 104 /* Append item to the specified array/object. */. ^0 N, e5 B4 O
- 105 extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);8 S' m4 Z7 c2 G1 _4 n
- 106 extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);; l# a% _! S0 R8 h
- 107 /* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
0 j$ N5 w" A) O, H - 108 extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
5 I7 d; Z' `& G5 g7 t - 109 extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);& l% W0 i+ g% M4 [( H
- 110
- @ f. u3 o, V- ?; A; D1 K - 111 /* Remove/Detatch items from Arrays/Objects. */% M$ j/ ~ F. _- ]
- 112 extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which);( r1 @+ }- X/ x7 c+ h
- 113 extern void cJSON_DeleteItemFromArray(cJSON *array,int which);) P5 N, X9 B2 F( L
- 114 extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string);
6 r, m; [8 f9 y - 115 extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string);) N2 @" _% F/ z
- 116 : C6 C6 k; Q9 o. K( z, L
- 117 /* Update array items. */
" o/ }! T# H2 N V& @; s. R - 118 extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
/ u7 C3 Z' v; Z/ B5 c7 x) n& i* O - 119 extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
s: b* y$ W4 q' ?% h% _6 ?0 _+ b - 120 8 @ j" L9 h& ]' {; B- d
- 121 /* Duplicate a cJSON item */
7 X' v: ]; Q( X8 R7 V5 G, ? - 122 extern cJSON *cJSON_Duplicate(cJSON *item,int recurse);
' c- U+ u8 x4 n, m4 O - 123 /* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
7 G. g9 f% E, Q8 m - 124 need to be released. With recurse!=0, it will duplicate any children connected to the item.
5 v4 A w1 X+ G2 X/ Q8 K - 125 The item->next and ->prev pointers are always zero on return from Duplicate. */
- l; Q* f& `, W4 N( ^) i - 126
9 u. s1 Z5 {: Q3 K: h - 127 /* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
7 d6 ?4 }4 Z4 s' Y8 `; Z - 128 extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated);, E" F, t: z/ t( p& m0 D
- 129
, B# R- i! B2 Z. y- o - 130 extern void cJSON_Minify(char *json);
4 h8 O' i0 O0 T# A; z& z4 U2 v - 131 + N- _9 U# }) [4 N
- 132 /* Macros for creating things quickly. */# e9 [ c" E! S
- 133 #define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
+ Y. _, ?/ K. v - 134 #define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())2 v' e* ]7 N0 u. |/ o% l8 B
- 135 #define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())6 _2 U: u I* m$ _, z& u
- 136 #define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))7 R j$ W0 X) d
- 137 #define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
4 b$ F0 `' r+ A$ j6 J9 D - 138 #define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
# z" ~' w* r8 y - 139
# B! S* r9 J" T; Y. U: _ - 140 /* When assigning an integer value, it needs to be propagated to valuedouble too. */5 J& W `1 [8 `1 L/ R8 r
- 141 #define cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val))) y/ E9 Q* I n3 p
- 142
* ~6 A- P5 X. u& S - 143 #ifdef __cplusplus: b$ c% c5 c8 L: ?) p
- 144 }1 ^( G* ]* \& i s* a6 o- b* }
- 145 #endif
5 p! s9 r. _4 u+ j' R. @, Q0 C - 146 9 b" ?$ V- N: o" J# `
- 147 #endif
复制代码- /* t- }; P5 ~' w0 J+ P1 Z) S
- Copyright (c) 2009 Dave Gamble
: G' w z0 A# Q/ w3 D r1 B
+ w$ ]7 i) |9 k- Permission is hereby granted, free of charge, to any person obtaining a copy4 |2 B7 Y$ K2 [' F3 G4 |" z
- of this software and associated documentation files (the "Software"), to deal* u. @5 K9 P- |1 D9 b! k
- in the Software without restriction, including without limitation the rights
% h$ k$ i8 r! Q) x. B$ p/ x - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell' Q, F4 j. r4 d* Y$ V
- copies of the Software, and to permit persons to whom the Software is
8 {& _, J& q. \- |4 l1 C1 s - furnished to do so, subject to the following conditions:. O/ ]9 \* Y9 m; a( H) m, _
- ! C T8 c; H- L4 K
- The above copyright notice and this permission notice shall be included in7 t/ F* L; }# g) b0 z
- all copies or substantial portions of the Software.
! U* W, |8 n6 r# p/ v) b9 Q - & F; d/ F" G4 ~, X4 ?' i# ~! F( X
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR" n. Q8 o1 Z! f# R( i1 K
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,2 F. B9 f, J3 f. j
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE( l9 \; F! x3 ?& W
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER8 g+ I: j% h3 S' i
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,9 m' E% x! y4 H2 l8 n
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN7 V1 t7 @& }# r' a/ s- p {
- THE SOFTWARE.% u: G' d8 E0 l/ f! ~6 P
- */
% o% g0 i- S N/ m& M8 A; O
{+ |4 T( \+ [" U; y' {1 B; d- #ifndef cJSON__h/ r1 _9 t# C+ w) Q( |
- #define cJSON__h
: z% `. P# j' L# M- L5 F+ K Y' Y
# |; j- G8 a( C. R- #include <stddef.h>
! |' u) `' W* s* {8 ~+ X2 G - 9 S2 K% F" h! a
- #ifdef __cplusplus. ~% P4 t9 B; W1 \9 ?/ x9 w `- `
- extern "C"
0 R5 a! L$ I) b* d3 ~ - {4 ]: p: _1 _. e' g% H- z
- #endif( `2 u* J: u) A, R7 d
- : |6 d/ H3 d V5 c' ^! b- ^/ e' i& U( {
- /* cJSON Types: */
% M& Q- K" H( d" Y - #define cJSON_False 0
8 Q/ v" j6 }& V5 T4 y$ m - #define cJSON_True 1: V$ {) V+ k/ m' C
- #define cJSON_NULL 2. O' u0 ?! k N3 _
- #define cJSON_Number 3* A4 V) }* W! P: {
- #define cJSON_String 4
: A0 H" c$ i8 Y) p; x - #define cJSON_Array 50 f/ y) M, U5 L+ I& Z! E1 d
- #define cJSON_Object 6
, v. ~. w8 X5 h - 8 L7 R( w2 n: _+ T! J, S' n9 C
- #define cJSON_IsReference 256* f1 f9 F" u: _# ]8 U! J
$ w! ]( j2 [) w2 F; g- /* The cJSON structure: */
2 V* M! Z. i+ R5 X4 Y3 R - typedef struct cJSON {
0 [3 g1 K5 W# X' g2 Y% y5 W - struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */6 R" M% d3 Y V8 w
- struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */& a6 O+ s/ h: Z+ W
- : C6 {+ X0 S3 J( m/ Y; ~
- int type; /* The type of the item, as above. */
6 {1 x3 b% q, q - n# ?6 V( s- p
- char *valuestring; /* The item's string, if type==cJSON_String */5 S# B6 J1 \9 z! U
- int valueint; /* The item's number, if type==cJSON_Number */! K4 u5 R! z& y0 s7 t" M/ P* z
- double valuedouble; /* The item's number, if type==cJSON_Number */
# Y0 H. i; O( R% } - ' \! @( u( o \9 J" T- v
- char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
8 R# {) l% _5 G% W/ c2 h ~4 | - } cJSON;
+ w+ D) |6 b r% A7 S4 N
* t: U: l T4 f N% A% \+ V
& h/ G: ?- _& M7 ?+ C! ]. [& p
1 z4 p- f/ J! H) a) u- typedef struct cJSON_Hooks {
& B& W9 Q% R4 N6 E" g& T( b S; R( T - void *(*malloc_fn)(size_t sz);
6 s3 n- n P2 _1 p2 ^# W' x/ [7 ~6 e - void (*free_fn)(void *ptr);
) ]) K$ c0 S0 W - } cJSON_Hooks;4 u$ b F6 m- d0 D) l0 O
* |- @: K' \6 \) }1 p" W) z- /* Supply malloc, realloc and free functions to cJSON */
3 }2 A8 F6 k4 ?% c" [ - extern void cJSON_InitHooks(cJSON_Hooks* hooks);
T& a- y. H' |' p+ ~ - : ^8 M$ T/ j% B A& n# }+ ~
- # M; H7 W! g2 L! [3 L
- /* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */: h ?7 k2 C0 R4 v) F2 \: V' P; _
- extern cJSON *cJSON_Parse(const char *value);7 N. B9 Z% v6 @2 d2 G$ t! S! g- T
- /* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
1 H' V- U/ Q! a+ u f3 k ^# H - extern char *cJSON_Print(cJSON *item);0 `6 s1 b, S$ V
- /* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
* f0 |2 G C3 P* T" ?- `1 Q - extern char *cJSON_PrintUnformatted(cJSON *item);
# l9 y1 ~, z# W+ c3 R$ ] - /* Delete a cJSON entity and all subentities. */
7 d/ E* B1 c4 q; Q - extern void cJSON_Delete(cJSON *c);
4 Z; I5 [% K+ h/ J% r O6 ` - : |" H" X; ^( H1 E7 w+ c
- /* Returns the number of items in an array (or object). */
8 [" h$ ]6 C2 {# _" f1 F - extern int cJSON_GetArraySize(cJSON *array);
6 `) v) B. L; x3 h+ e2 [ - /* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
- E% f+ C# P5 ~/ {7 `* w - extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
" f7 y6 z' B" M+ Y& A - /* Get item "string" from object. Case insensitive. */3 ^! J+ l0 w( U' p# P3 c+ j
- extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);7 @5 z7 B: ~8 c
- ( a% v( C/ A6 t! E+ n4 M
- /* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
8 M8 {2 s' r0 A - extern const char *cJSON_GetErrorPtr(void);* ]1 [) e2 X3 u$ h
+ o' z' W' p. x6 m% H! z/ q4 r- /* These calls create a cJSON item of the appropriate type. *// `( d; S! z: C, H( A+ d0 s
- extern cJSON *cJSON_CreateNull(void);& B; n# H; g9 |! K, ~
- extern cJSON *cJSON_CreateTrue(void); v7 f8 I4 h6 {# {6 R' H4 m
- extern cJSON *cJSON_CreateFalse(void);& |4 f3 i; U$ a8 j; Q
- extern cJSON *cJSON_CreateBool(int b);
+ e6 C3 t1 _& Q/ {" V& e/ N& ?, l - extern cJSON *cJSON_CreateNumber(double num);/ ~" h3 F" @& A+ C' I4 G
- extern cJSON *cJSON_CreateString(const char *string);
$ r% C0 Q( r$ r - extern cJSON *cJSON_CreateArray(void);
% o- A; {) n) e) @! @3 Z& x: U - extern cJSON *cJSON_CreateObject(void);$ ?2 @/ B0 b: g' L! R
- 1 i0 I4 o1 i' T% I$ { T1 K% o6 ?9 x$ O
- /* These utilities create an Array of count items. */
* U. }3 I) O' V2 |. m1 g3 P9 M& S, M - extern cJSON *cJSON_CreateIntArray(const int *numbers,int count);
: ^% C9 ^$ [- o - extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count);
% K T' @1 p5 x: b/ E - extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count);* w- Y4 |; ]8 k, t/ Q) p
- extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
: |, {* b) W& C/ W3 i3 B, m
% A8 B, _( k6 L5 J/ S+ p' K- /* Append item to the specified array/object. */% K! ]: x% p3 F$ q" {1 S) X
- extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
2 D( k0 N( o4 E3 a - extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);( E1 S, u U j q9 o7 k
- /* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
) i4 E* n7 w4 ~2 L. P - extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
& d$ }5 f% I/ v9 l - extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);# }# J9 K$ W- j2 D6 C* w( j: W
0 [- i8 i. F+ l! e# d- P( Q- /* Remove/Detatch items from Arrays/Objects. */2 T9 c1 h) C. d) ]+ b) n9 G% j
- extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which);
% W" L8 _; H6 u0 g! `9 }. G - extern void cJSON_DeleteItemFromArray(cJSON *array,int which);( x, P% _2 K+ r$ d) ^5 f; O. a
- extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string);
4 x3 V7 ?5 K& ]. j, H - extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string);: T3 T7 k2 H' j
- ) v* u2 _( n+ s$ O$ p
- /* Update array items. */
* O5 E) _1 m4 E; m7 s3 P - extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);9 x: B$ R; B- P) r/ R$ {
- extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
; O% I/ G0 \, P. b+ w - - n) ~4 O+ F, _% t- c) z ]
- /* Duplicate a cJSON item */
; s0 L7 ~+ O6 m - extern cJSON *cJSON_Duplicate(cJSON *item,int recurse);2 I+ z% M3 d" Q; g& K: A6 j
- /* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
# B6 ~, }, A3 h/ J - need to be released. With recurse!=0, it will duplicate any children connected to the item.6 \+ h6 F7 ~# S$ q, U5 L2 x' r- a
- The item->next and ->prev pointers are always zero on return from Duplicate. */
+ ~, N. U. `4 z$ E m - 3 l4 a& z1 K: E( W; R1 _
- /* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */! _( d! T' i5 {
- extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated);
3 L+ ?& r$ h+ W
. c G" b# T ?/ [- extern void cJSON_Minify(char *json);
3 ]% E& o+ u) S( X: v, j - 6 j) s( n+ j6 m/ B+ _
- /* Macros for creating things quickly. */
2 d$ X, `5 I+ q - #define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())' S( k# h; C- C. t0 N
- #define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
0 |4 Q8 u' {$ U- R& |* ~ - #define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
9 E& m& |1 [/ m - #define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
5 n$ d7 V" n3 [4 X+ d - #define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
$ h/ l. I; D& v6 A0 `3 v1 }( a - #define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s)) z) ^- p) w: O% {/ Y s$ D$ q
- R" q9 R, U; T5 B' H7 U: F! l/ O p- /* When assigning an integer value, it needs to be propagated to valuedouble too. */* e- ^( x! c& L& z
- #define cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val)); F x$ X6 m0 ^7 L8 J. f
, T9 g! V$ {2 p/ r) b# h( ^5 u- #ifdef __cplusplus
R4 R' t- P, H* ^9 w! ?, ]4 G - }
! F* ~7 M' E1 A' D9 I( E - #endif
0 b' H" ~* x+ ?8 J8 R - ! _5 C( R& L" x. m8 }' o
- #endif
复制代码下面是JSON数据的解析部分,简单说来就是对号入座。 - /**************************************************************************
2 L% ^* n+ w/ M/ ~$ `+ u! R - 函数功能:JSON字符串解析,按照命令格式,解析出上位机发送的命令
, U% }) x4 `$ k( j - 入口参数:JSON格式的字符串,8 t; {* {& Z9 U s8 O% I: G% p
- 返回 值:指令类型* h- H" i. U; Y5 _- O: s; P' n8 P
- 注 意:从服务器的socket得到的字符串是加密了的字符串,需要首先解密后才能放入此函数
9 R5 f% C; l) D; f; W - **************************************************************************/0 |4 s' k1 L& R2 o3 I! O7 o
- CommandType GetCommandFromServer(char * JsonDataFromSocket)3 g; X/ t" O9 |$ ^" h0 ]4 |
- {1 Z! U3 f. n9 Q! `4 q& o
9 R! T' j$ [: X- // ROBOCmd_TypeDef cmd;//使用的时候用全部结构体代替6 b# y$ Y* Z* R# F) T( {
- |9 G4 Z) f) L- U* e1 _
- // Hello_Ack_TypeDef hello_ack;//使用的时候用全局接头体代替
) u( @" a3 |2 D+ {6 L - CommandType cmdtype;) Q9 b3 \7 Q5 d3 V
- RobotJSON_CMD_TypeDef json_cmd;
w% P f; i0 j1 [$ {8 P6 x - HelloJSON_Ack_TypeDef json_ack;
3 K: w# v# a7 L# h
$ S' |( a+ b1 u: L# g3 L& d- cJSON *root,*command_root;/ G |% Y9 L4 q- g4 z4 t
- ) ]9 }: ?+ Y, `- V! G* v5 [- T
- root = cJSON_Parse(JsonDataFromSocket); //实例化JSON对象(静态对象)1 h( {) v- o: t0 X
- if(!root)" ]7 S1 }+ l" O+ ?1 J- Z
- {
* g: o. v6 V, \- |8 H$ I3 S - //printf("get cJSON faild !\n");6 w) o" |: S9 P+ Q6 f+ N$ a( m
- cmdtype=NOCOMMAND;//如果没有,就直接返回0$ g0 H4 }* F3 v& f: }4 R" u& ^) r
- }( @2 x/ C2 t; g# d
- else* W! h, ^: {' K
- {, G9 a3 K/ |- E$ A% g' f
- command_root = cJSON_GetObjectItem(root, "RobotCommand"); //取RobotCommand键值对
: C* X% T$ j0 X2 ^4 j - if(!command_root)1 t* }# L3 s4 Y( z% F
- {
, O6 F' L- Z; F - //printf("No RobotCommand !\n");
& H7 C" E4 i7 Z* A4 d; d - + O, P6 l \! [
- command_root = cJSON_GetObjectItem(root, "HelloACK"); //取hellACK键值对
8 }% U1 S+ a6 V M; F* ?1 f# Y6 M - if(!command_root)- C: _# d$ } R5 g: Y9 M4 o6 s
- {
0 W- s6 G, c* T: D - //将函数的返回值改下 既不是RobotCommand也不是HelloACK的情况就返回0( t% v9 f0 m9 }9 ]
- cmdtype=NOCOMMAND;//如果没有,就直接返回0; | o- I i! u3 }3 x, T9 h9 E
- }
: k; l5 ^6 U2 r4 J - else{4 R7 _! p+ `: z8 Y" B8 I" X4 ?
- ) e+ |+ F+ Y1 N, G$ C
6 P( b9 _. [1 u1 K$ p- json_ack.ID = cJSON_GetObjectItem(command_root,"ID");+ f- t, k1 T8 G. O$ ^% L8 R: |+ n0 K) L
- if(!json_ack.ID)
% x; ~( E0 X* s - {$ `( A. X7 i/ Y+ A2 ^& u& F
- cmdtype=NOCOMMAND;//如果没有,就直接返回0
) [' j% E5 Q3 c+ d% p3 r" L$ G$ L - }
$ n! Q6 L; ?( u! w2 d4 O) y8 ^ - else1 T& k+ i; J% O, c
- {
4 \9 R) T7 B! n& ? - strcpy(Ack_From_Server.ID,json_ack.ID->valuestring);
. g: ~% I/ r6 ]6 ^ - }6 M. r' V7 a5 G! H' R
- json_ack.Time = cJSON_GetObjectItem(command_root,"Time");" t$ x' ]( ?' v' i4 H6 \& y% G8 O
- if(!json_ack.Time)
3 H: ]3 e9 ?8 W: Y+ v - {$ s: L2 @# L! e; U8 W) w# d8 L5 l# V
- cmdtype=NOCOMMAND;//如果没有,就直接返回01 _# S+ G$ q# S$ \- l
- }0 I! C3 K% ^. q$ J+ Q( i
- else- v+ _1 \6 p5 `: J
- {8 I! F: Z& q5 t( O* u% s. ^& P# [9 s) _
- strcpy(Ack_From_Server.Time,json_ack.Time->valuestring);9 S/ v, a F# |: Z
- }/ @' }0 n" P* o6 y2 s
- & ]0 g0 S! h. v# F& {# p& a6 _( D
- cmdtype=HELLO_ACK;//如果没有,就直接返回0
3 p9 _! C2 G1 L2 J* @ - }
$ ^# f" q2 B8 l" ^1 s - } N" b$ T6 N7 h2 }/ g6 e
- else4 |# ~# }" {6 w. r8 A, U# B
- {
3 t' X0 B# d: c1 e; q" A3 M
5 W. E# L8 y! j, w' a# ~- //运动命令9 Y& w: M& e9 K6 L4 q
- json_cmd.Command =cJSON_GetObjectItem(command_root,"Command");7 W! S! }8 @! F; q
- if(!json_cmd.Command)
% b) Z& p; E. D+ W- K6 Y - {
2 I* @5 G4 E. v% t2 l* {8 m - //printf("no Command!\n");
9 P, v) q- e( W. x' O9 R# c - //cmdtype=NOCOMMAND;//如果没有,就直接返回0
5 V4 U1 B9 N1 X, h, N - }
! E9 \: o8 [0 N - else
- y9 _/ b0 ^8 `$ e - { \, @$ Z2 C- D! J3 E
- //printf("Command is %s\r\n", robocmd.Command->valuestring);8 P9 g" E, |9 l8 g' e; b
- strcpy(Cmd_From_Server.Command,json_cmd.Command->valuestring);
p" n7 e3 s0 P6 `2 A* `6 Z, t - //此时应该发送对应命令的OK程序,Set或者Auto或者。。。具体见协议
9 d- g! f; F M0 D" t
" P8 _0 ]$ x- M: D# M- }# r2 O2 r- b+ L7 ^6 w, a6 b
- //速度指令
: X* {8 U3 R( l' g' o# L - json_cmd.Speed=cJSON_GetObjectItem(command_root,"Speed");
1 B" L. y5 P; L; L - if(!json_cmd.Speed)" ~! _, ]3 D/ Z
- {+ C, W) |7 z, I2 B- D
- //printf("no Speed!\n");2 }& Z( Z- w$ W
- // cmdtype=NOCOMMAND;//如果没有,就直接返回0
+ q' z7 C/ S7 r; g6 F2 _1 l - }
, L. N% s- t2 r7 {, [0 M - else
: {8 J. v( w2 E6 o, r - {" _; |" \) ^* d" |- `$ H
- //printf("Speed is %d\r\n", json_cmd.Speed->valueint);
+ a* [- @& y! L3 N7 K3 b - Cmd_From_Server.Speed=json_cmd.Speed->valueint;
: r7 a& Q9 y0 O$ P6 H" I - }
9 o6 @! i3 p" o2 W2 h/ e% K- x - b2 p U( E5 E0 d) V, s. M
- //运行次数
& N [2 N: I/ l# J- g) D6 Y4 Z - json_cmd.RunCount = cJSON_GetObjectItem(command_root,"RunCount");
! x+ g- q, g' O |' l - if(!json_cmd.RunCount)
" [1 G" p* n }* n+ M - {
. K; H5 _0 Q) g( w/ Q - //printf("no RunCount!\n");
. }' D! Q, z7 \0 U% a' n5 `8 k0 m - // cmdtype=NOCOMMAND;//如果没有,就直接返回0' o8 t+ d A- z; m1 i- P- ~
- }8 Z+ d$ f) a4 K( K+ t0 e) v
- else
! j2 Y% y) p; h3 } - {6 ]- k3 N" j/ |
- //printf("RunCount is %d\r\n",json_cmd.RunCount->valueint);# F) d. `+ ]/ g9 b
- Cmd_From_Server.RunCount=json_cmd.RunCount->valueint;
9 j& S; C# U' p) e+ U" G - }
@% T5 [0 D/ t Y - //开始位置
: ]) j. z" F1 g- d0 B' p - json_cmd.StartPosition = cJSON_GetObjectItem(command_root,"StartPosition");
/ ?. Q) N$ b9 y* H' |8 t* E - if(!json_cmd.StartPosition)+ b* L, h) E' T- }
- {
- E, J7 [; K( C4 I6 B0 t - //printf("no StartPosition!\n"); I6 P$ W4 }* x/ m) A
- // cmdtype=NOCOMMAND;//如果没有,就直接返回0
8 o7 `# W: T# |9 w& i' t0 B - }! H* T0 m, }" _ R' x. v% q2 V
- else8 a& ~* Z1 s9 J4 E* f: _1 j2 @
- {
& F5 V+ v& ?, B% \/ Y - //printf("StartPosition is %d\r\n",json_cmd.StartPosition->valueint);6 o4 g) I! t, X9 W/ F
- Cmd_From_Server.StartPosition=json_cmd.StartPosition->valueint;& _; V4 C V3 _# l# _: y- s
- }
8 c+ ?: S5 J" q! |& L3 s. s - //结束位置6 ]) h( S* e: a! d$ x
- json_cmd.EndPosition = cJSON_GetObjectItem(command_root,"EndPosition");
) P- y: c- K) Q1 n8 n7 V - if(!json_cmd.EndPosition)
; w @2 B/ n: i - {
7 I' r! A t" M7 K, ~7 L - //printf("no EndPosition!\n");7 N( A; D: T* {
- // cmdtype=NOCOMMAND;//如果没有,就直接返回08 [" v' m) u# A. h4 v! O( G7 r+ P
- }
- u5 q1 m3 i" n( W: r+ H0 e - else
7 S* _, D5 }: `' s - {
4 N5 J) C7 g- { - //printf("EndPosition is %d\r\n",json_cmd.EndPosition->valueint);
4 y/ V7 r) L# P7 S2 G$ _' e - Cmd_From_Server.EndPosition=json_cmd.EndPosition->valueint;. D! d+ P) z5 F' L
- }
& @/ o; y$ S: n8 w: P0 w8 j4 }- L/ f - //目标位置TargetPosition
' `3 T( v \/ g( B! ?* K( ^) h$ w3 V - json_cmd.TargetPosition = cJSON_GetObjectItem(command_root,"TargetPosition");* q3 E4 M ~0 W9 J3 x" G# g
- if(!json_cmd.TargetPosition)
6 v. F# w( t, a: ^ v/ D, Y - {
$ W; f$ q ^# o' ~& | - //printf("no TargetPosition!\n");
" C) X8 G) j; w$ J4 @& O - // cmdtype=NOCOMMAND;//如果没有,就直接返回0
: X3 Z+ Q2 u# o& c( t - }9 D0 p( j; q# ?; M% a
- else" N* U: J% w V; T
- {
2 w8 c9 J) U6 \( z - //printf("TargetPosition is %d\r\n",json_cmd.TargetPosition->valueint);! v* f7 b2 o; {
- Cmd_From_Server.TargetPosition=json_cmd.TargetPosition->valueint;; _2 N K3 F; @7 s4 J! |
- }# g# r4 A. W, T2 ~# R
- ! @3 s- p, r9 P
- //*工作模式WorkMode;* Z% S9 |5 b$ o) Y
- json_cmd.WorkMode= cJSON_GetObjectItem(command_root,"WorkMode");
$ ]& J; S9 x. x' N - if(!json_cmd.WorkMode)% _( [2 U( D% o
- {1 X& s+ Q& f: S1 @
- //printf("no WorkMode!\n");
% j3 b; l, ]8 H( @* u - // cmdtype=NOCOMMAND;//如果没有,就直接返回0
, D5 M5 Y& ^( ?- Z - }6 m: b7 x8 b0 S/ Q
- else' m7 [! d' l' {. a3 b; Z
- {
" D% B% _: B% p# g6 i& C5 r0 k - //printf("WorkMode is %s\r\n",json_cmd.WorkMode->valuestring);
8 H: a# N# |8 }( J3 G9 f - strcpy(Cmd_From_Server.WorkMode,json_cmd.WorkMode->valuestring);; Z* t! D3 U) s2 X- c
- }
" t) y0 X4 G# J5 l. i - //step
( w; V, p5 q2 ~1 Z - json_cmd.Step= cJSON_GetObjectItem(command_root,"Step");
- I! d C3 m! c - if(!json_cmd.Step)
' ~7 j. [% M& e. o2 J - {2 d# I3 S# V: Y( }2 K3 {; K
- //printf("no Step!\n");' C$ f, J* y6 f( Y4 l7 R' s/ \
- // cmdtype=NOCOMMAND;//如果没有,就直接返回0
' J% q2 w6 \, ?' [" h - }
: R# I/ G1 N$ b8 j - else
' \* U/ y4 G7 S7 L+ W0 M - {
- c9 D3 s4 H `" @9 Z - //printf("Step is %d\r\n",json_cmd.Step->valueint);
p3 F3 H O8 W) p" l# Q - Cmd_From_Server.Step=json_cmd.Step->valueint;2 ]2 `- `3 t4 q8 _ q3 Y# }$ V
- }2 o8 ~( b) \ B
- * Q- z4 x i8 k6 H% _4 w
- 7 Y- e# [* t5 Z3 p1 z
- cmdtype= COMMAND;( L6 X* @" i/ |( E0 m# Q: `
" v& {6 r! G( g% U- }2 G2 _! d K* E( f1 `
- }
7 G* t' F! x/ _/ ]' W n* ]0 h3 L! R - ' h$ F9 D: p' ], }2 i% V) S
- cJSON_Delete(root);; a9 E; B _; x
- ; N9 J* e# f' z3 `8 Y& ~
- return cmdtype;
: y1 n5 V. b$ b# x; G - }
复制代码
# S1 }( Y) g: p! d* d: A l4 T- d0 a
% \: D2 _7 `9 o" w |