My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
jsmn_parser.h
Go to the documentation of this file.
1 /*
2 START OF LICENSE STUB
3  DeDOS: Declarative Dispersion-Oriented Software
4  Copyright (C) 2017 University of Pennsylvania, Georgetown University
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 END OF LICENSE STUB
19 */
28 #ifndef JSMN_PARSER_H_
29 #define JSMN_PARSER_H_
30 #include "jsmn.h"
31 
32 #include <stdlib.h>
33 #include <stdio.h>
34 
39 #define ASSERT_JSMN_TYPE(tok, toktype, j) \
40  if ((tok)->type != toktype){ \
41  log_error("Got type %d, expected %d while parsing %.50s", \
42  (tok)->type, toktype, &j[(tok)->start]); \
43  return -1; \
44  } \
45 
46 
49 struct json_state {
50  void *data;
52  struct json_state *next;
54 };
55 
66 typedef int (*jsmn_parsing_fn)(jsmntok_t **tok, char *j, struct json_state *state,
67  struct json_state **saved);
68 
76 #define PARSE_FN(fn_name) \
77  static int fn_name(jsmntok_t **tok__, char *j__, \
78  struct json_state *in__, struct json_state **saved__)
79 
81 #define GET_PARSE_OBJ() \
82  (in__->data)
83 
85 #define GET_STR_TOK() \
86  tok_to_str(*tok__, j__)
87 
89 #define GET_INT_TOK() \
90  tok_to_int(*tok__, j__)
91 
93 #define GET_LONG_TOK() \
94  tok_to_long(*tok__, j__)
95 
103 #define PARSE_OBJ_LIST_FN(fn_name, init_fn) \
104  static int fn_name(jsmntok_t **tok__, char *j__, \
105  struct json_state *in__, struct json_state **saved__) { \
106  return parse_jsmn_obj_list(tok__, j__, in__, saved__, init_fn); \
107  }
108 
114 #define INIT_OBJ_FN(fn_name) \
115  static struct json_state fn_name(struct json_state *in__, int index__)
116 
120 #define GET_OBJ_INDEX() index__
121 
128 #define RETURN_OBJ(data__, type__) \
129  struct json_state out_obj__ = { \
130  .data = data__, \
131  .parent_type = type__ \
132  }; \
133  return out_obj__;
134 
143 #define PARSE_OBJ_FN(fn_name, parent_obj_type, parent_obj_field, new_type) \
144  static int fn_name(jsmntok_t **tok__, char *j__, \
145  struct json_state *in__, struct json_state **saved__) { \
146  parent_obj_type *type_out__ = in__->data; \
147  struct json_state state_out__ = { \
148  .data = &type_out__->parent_obj_field, \
149  .parent_type = new_type, \
150  .tok = *tok__ \
151  }; \
152  if (parse_jsmn_obj(tok__, j__, &state_out__, saved__) < 0) { \
153  return -1; \
154  } \
155  return 0; \
156  }
157 
164 #define START_ITER_TOK_LIST(i) \
165  int tok_size__ = (*tok__)->size; \
166  for (i = 0, ++(*tok__); i< tok_size__; ++(*tok__), i++)
167 
172 #define END_ITER_TOK_LIST(i) \
173  *tok__ = (*tok__) - (tok_size__ - i + 1);
174 
178 struct key_mapping {
179  char *key;
184 };
185 
196 int parse_file_into_obj(const char *filename, void *obj, struct key_mapping *km);
197 
208 int parse_str_into_obj(char *contents, void *obj, struct key_mapping *km);
209 
210 
214 int jsmn_ignore(jsmntok_t **tok, char *j, struct json_state *in, struct json_state **saved);
215 
223 typedef struct json_state (*jsmn_initializer)(struct json_state *state, int index);
224 
225 
227 int parse_jsmn_obj(jsmntok_t **tok, char *j, struct json_state *in,
228  struct json_state **saved);
229 
231 int parse_jsmn_obj_list(jsmntok_t **tok, char *j, struct json_state *in,
232  struct json_state **saved, jsmn_initializer init);
233 
235 void *get_root_jsmn_obj();
236 
238 char *tok_to_str(jsmntok_t *tok, char *j);
239 
241 int tok_to_int(jsmntok_t *tok, char *j);
242 
244 long tok_to_long(jsmntok_t *tok, char *j);
245 
246 #endif
jsmn_parsing_fn parse
The function to be called when this string is seen at the appropriate place in the input JSON...
Definition: jsmn_parser.h:182
JSON token description.
Definition: jsmn.h:61
int parse_str_into_obj(char *contents, void *obj, struct key_mapping *km)
Using the provided functions, parses the JSON present in in the provided string and stores the result...
Definition: jsmn_parser.c:171
int jsmn_ignore(jsmntok_t **tok, char *j, struct json_state *in, struct json_state **saved)
Can be used as a jsmn_parsing_fn that ignores any value passed into it.
Definition: jsmn_parser.c:120
int parse_jsmn_obj_list(jsmntok_t **tok, char *j, struct json_state *in, struct json_state **saved, jsmn_initializer init)
SHOULD NOT BE USED DIRECTLY.
Definition: jsmn_parser.c:318
char * key
The string in the JSON file.
Definition: jsmn_parser.h:179
int parse_file_into_obj(const char *filename, void *obj, struct key_mapping *km)
Using the provided functions, parses the JSON present in 'filename' and stores the resulting object i...
Definition: jsmn_parser.c:135
char * tok_to_str(jsmntok_t *tok, char *j)
SHOULD NOT BE USED DIRECTLT.
Definition: jsmn_parser.c:69
Structure to hold state while parsing JSON.
Definition: jsmn_parser.h:49
int parent_type
The type of object that is being create.
Definition: jsmn_parser.h:51
int(* jsmn_parsing_fn)(jsmntok_t **tok, char *j, struct json_state *state, struct json_state **saved)
Typedef for a jsmn parsing function.
Definition: jsmn_parser.h:66
Structure to map a key + state to a function.
Definition: jsmn_parser.h:178
int parent_type
The current type, as set by a previous parsing fn.
Definition: jsmn_parser.h:180
struct json_state(* jsmn_initializer)(struct json_state *state, int index)
Typedef for a json_state initializer, used when iterating over lists of objects.
Definition: jsmn_parser.h:223
int tok_to_int(jsmntok_t *tok, char *j)
SHOULD NOT BE USED DIRECTLY.
Definition: jsmn_parser.c:42
struct json_state * next
Definition: jsmn_parser.h:52
void * get_root_jsmn_obj()
Returns the initial object passed into the JSON parser.
Definition: jsmn_parser.c:92
long tok_to_long(jsmntok_t *tok, char *j)
SHOULD NOT BE USED DIRECTLY.
Definition: jsmn_parser.c:55
void * data
The object currently under construction.
Definition: jsmn_parser.h:50
int parse_jsmn_obj(jsmntok_t **tok, char *j, struct json_state *in, struct json_state **saved)
SHOULD NOT BE USED DIRECTLY.
Definition: jsmn_parser.c:358
jsmntok_t * tok
When saving state, provides a linked-list.
Definition: jsmn_parser.h:53
state
Definition: http_parser.c:298