1 /*************************************************************************//**
3 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 ****************************************************************************/
28 * JSON type identifier. Basic types are:
32 * o Other primitive: number, boolean (true/false) or null
43 /* Not enough tokens were provided */
44 JSMN_ERROR_NOMEM = -1,
45 /* Invalid character inside JSON string */
46 JSMN_ERROR_INVAL = -2,
47 /* The string is not a full JSON packet, more bytes expected */
51 typedef struct arrayValues {
55 typedef struct keyValResult {
62 * JSON token description.
63 * @param type type (object, array, string etc.)
64 * @param start start position in JSON data string
65 * @param end end position in JSON data string
72 //#ifdef JSMN_PARENT_LINKS
78 * JSON parser. Contains an array of token blocks available. Also stores
79 * the string being parsed now and current position in that string
82 unsigned int pos; /* offset in the JSON string */
83 unsigned int toknext; /* next token to allocate */
84 int toksuper; /* superior token node, e.g parent object or array */
87 #define TOKEN_EQ(t, tok_start, tok_end, tok_type) \
88 ((t).start == tok_start \
89 && (t).end == tok_end \
90 && (t).type == (tok_type))
92 #define TOKEN_STRING(js, t, s) \
93 (strncmp(js+(t).start, s, (t).end - (t).start) == 0 \
94 && strlen(s) == (t).end - (t).start)
96 #define TOKEN_COPY(js, t, s) \
97 strncpy(s, js+(t).start, (t).end - (t).start)
99 #define TOKEN_PRINT(t) \
100 printf("start: %d, end: %d, type: %d, size: %d\n", \
101 (t).start, (t).end, (t).type, (t).size)
105 * Create JSON parser over an array of tokens
107 void jsmn_init(jsmn_parser *parser);
110 * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
111 * a single JSON object.
113 int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
114 jsmntok_t *tokens, unsigned int num_tokens);
116 int jsoneq(const char *json, jsmntok_t *tok, const char *s);
118 void printToken(char * js, jsmntok_t * tokens, int numToken);
120 int getStringToken(char * js, jsmntok_t * tokens, int numToken, char * param, char * pParam, char * value, int maxValueSize);
122 int getIntToken(char * js, jsmntok_t * tokens, int numToken, char * param, char * pParam, int * value);
124 int getArrayTokens(char * js, jsmntok_t * tokens, int numToken, char * param, char * pParam, ARRAYVAL * arrayValue, int * numElements);
126 int isTokenPresent(char * js, jsmntok_t * tokens, int numToken, char * param, char * pParam);
128 int read_keyVal_params(char * js, jsmntok_t * tokens, int numToken, char * param, char * pParam, KEYVALRESULT * keyValueResultList, int * numElements);
134 #endif /* __JSMN_H_ */