yjson.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yjson.h 27182 2017-04-20 16:25:41Z seb $
4  *
5  * Simple JSON parser (actually a slightly enhanced lexer)
6  *
7  * - - - - - - - - - License information: - - - - - - - - -
8  *
9  * Copyright (C) 2011 and beyond by Yoctopuce Sarl, Switzerland.
10  *
11  * Yoctopuce Sarl (hereafter Licensor) grants to you a perpetual
12  * non-exclusive license to use, modify, copy and integrate this
13  * file into your software for the sole purpose of interfacing
14  * with Yoctopuce products.
15  *
16  * You may reproduce and distribute copies of this file in
17  * source or object form, as long as the sole purpose of this
18  * code is to interface with Yoctopuce products. You must retain
19  * this notice in the distributed source file.
20  *
21  * You should refer to Yoctopuce General Terms and Conditions
22  * for additional information regarding your rights and
23  * obligations.
24  *
25  * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
26  * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
27  * WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS
28  * FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO
29  * EVENT SHALL LICENSOR BE LIABLE FOR ANY INCIDENTAL, SPECIAL,
30  * INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA,
31  * COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR
32  * SERVICES, ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT
33  * LIMITED TO ANY DEFENSE THEREOF), ANY CLAIMS FOR INDEMNITY OR
34  * CONTRIBUTION, OR OTHER SIMILAR COSTS, WHETHER ASSERTED ON THE
35  * BASIS OF CONTRACT, TORT (INCLUDING NEGLIGENCE), BREACH OF
36  * WARRANTY, OR OTHERWISE.
37  *
38  *********************************************************************/
39 
40 #ifndef YJSON_H
41 #define YJSON_H
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 #include "ydef.h"
48 
49 //#define DEBUG_JSON_PARSE
50 #define YJSON_MAX_DEPTH 6
51 
52 typedef enum {
53  YJSON_HTTP_START, // about to parse HTTP header, up to first space before return code
54  YJSON_HTTP_READ_CODE, // reading HTTP return code
55  YJSON_HTTP_READ_MSG, // reading HTTP return message
56  YJSON_HTTP_SKIP, // skipping rest of HTTP header until double-CRLF
57  YJSON_START, // about to parse JSON reply
58  YJSON_PARSE_ANY, // parsing anything to come
59  YJSON_PARSE_SYMBOL, // parsing a symbol (boolean)
60  YJSON_PARSE_NUM, // parsing a number
61  YJSON_PARSE_STRING, // parsing a quoted string
62  YJSON_PARSE_STRINGQ, // parsing a quoted string, within quoted character
63  YJSON_PARSE_STRINGCONT, // parsing the continuation of a quoted string
64  YJSON_PARSE_STRINGCONTQ,// parsing the continuation of a quoted string, within quoted character
65  YJSON_PARSE_ARRAY, // parsing an unnamed array
66  YJSON_PARSE_STRUCT, // parsing a named structure
67  YJSON_PARSE_MEMBSTART, // parsing a structure member (before name)
68  YJSON_PARSE_MEMBNAME, // parsing a structure member name
69  YJSON_PARSE_MEMBCOL, // parsing the colon between member name and value
70  YJSON_PARSE_DONE, // parse completed, end of input data (or end of container)
71  YJSON_PARSE_ERROR, // dead end, parse error encountered
73 } yJsonState;
74 
75 #ifdef DEBUG_JSON_PARSE
76 extern const char* yJsonStateStr[];
77 #endif
78 
79 
80 
81 typedef struct {
82  _FAR const char *src; // pointer to source buffer to parse (initialized by caller)
83  _FAR const char *end; // pointer to end of source data (initialized by caller)
84  yJsonState st; // current state (initialized by caller)
85  yJsonState next; // next state (when returning with parse_avail)
86  yJsonState stack[YJSON_MAX_DEPTH]; // state stack for handling nested structures
87  int depth; // state stack depth
88  char token[62]; // parse buffer, also used to return tokens
89  char *pt; // pointer in token buffer
90  int skipcnt; // number of items to skip
91  int skipdepth; // stack depth at which skipping started
92 #ifndef YAPI_IN_YDEVICE
93  _FAR const char *state_start; // pointer to the start of the current state
94  _FAR const char *state_end; // pointer to end of of the current state
95 #endif
97 
98 
99 typedef enum {
100  YJSON_NEED_INPUT, // caller need to provide fresh input data
101  YJSON_PARSE_AVAIL, // caller need to process current state and token
102  YJSON_SUCCESS, // caller can take a break
103  YJSON_FAILED // caller should consider using better input data
104 } yJsonRetCode;
105 
106 // Parse JSON input stream until more data is needed or a token is returned
107 yJsonRetCode yJsonParse(yJsonStateMachine *j);
108 
109 // Mark next n JSON items in stream to be skipped (including content, in case items are containers)
110 void yJsonSkip(yJsonStateMachine *j, int nitems);
111 
112 #if 0
113 
114  typedef enum {
115  JZON_PARSE_SYNCRO = 0,
116  JZON_PARSE_ONLY_REF,
117  JZON_PARSE_ONLY_YZON
118  } yJzonSyncroState;
119 
120  typedef struct {
121  int depth; // state stack depth
122  yJzonSyncroState sst;
123  yJzonSyncroState sst_stack[YJSON_MAX_DEPTH];
124  yJsonState st; // current state (initialized by caller)
125  yJsonState next; // next state (when returning with parse_avail)
126  char token[62]; // parse buffer, also used to return tokens
127  yJsonStateMachine jzon; // reference state machine (initialized by caller)
128  yJsonStateMachine ref; // reference state machine (initialized by caller)
129  } yJsonStateMachineEx;
130 
131  void yJsonInitEx(yJsonStateMachineEx *j, const char *jzon, int jzon_len, const char *ref, int ref_len);
132 
133  // Parse JSON input stream until more data is needed or a token is returned
134  yJsonRetCode yJsonParseEx(yJsonStateMachineEx *j);
135 
136  // Mark next n JSON items in stream to be skipped (including content, in case items are containers)
137  void yJsonSkipEx(yJsonStateMachineEx *j, int nitems);
138 #endif
139 
140 #ifdef __cplusplus
141 }
142 #endif
143 #endif
144 
145 
_FAR const char * state_start
Definition: yjson.h:93
yJsonState next
Definition: yjson.h:85
_FAR const char * src
Definition: yjson.h:82
#define YJSON_MAX_DEPTH
Definition: yjson.h:50
_FAR const char * end
Definition: yjson.h:83
char * pt
Definition: yjson.h:89
yJsonRetCode yJsonParse(yJsonStateMachine *j)
Definition: yjson.c:83
yJsonState st
Definition: yjson.h:84
yJsonState
Definition: yjson.h:52
void yJsonSkip(yJsonStateMachine *j, int nitems)
Definition: yjson.c:367
#define _FAR
Definition: ydef.h:285
yJsonRetCode
Definition: yjson.h:99
_FAR const char * state_end
Definition: yjson.h:94


yoctopuce_altimeter
Author(s): Anja Sheppard
autogenerated on Mon Jun 10 2019 15:49:10