xref: /freebsd/contrib/wpa/src/utils/json.h (revision 85732ac8bccbc0adcf5a261ea1ffec8ca7b3a92d)
1*85732ac8SCy Schubert /*
2*85732ac8SCy Schubert  * JavaScript Object Notation (JSON) parser (RFC7159)
3*85732ac8SCy Schubert  * Copyright (c) 2017, Qualcomm Atheros, Inc.
4*85732ac8SCy Schubert  *
5*85732ac8SCy Schubert  * This software may be distributed under the terms of the BSD license.
6*85732ac8SCy Schubert  * See README for more details.
7*85732ac8SCy Schubert  */
8*85732ac8SCy Schubert 
9*85732ac8SCy Schubert #ifndef JSON_H
10*85732ac8SCy Schubert #define JSON_H
11*85732ac8SCy Schubert 
12*85732ac8SCy Schubert struct json_token {
13*85732ac8SCy Schubert 	enum json_type {
14*85732ac8SCy Schubert 		JSON_VALUE,
15*85732ac8SCy Schubert 		JSON_OBJECT,
16*85732ac8SCy Schubert 		JSON_ARRAY,
17*85732ac8SCy Schubert 		JSON_STRING,
18*85732ac8SCy Schubert 		JSON_NUMBER,
19*85732ac8SCy Schubert 		JSON_BOOLEAN,
20*85732ac8SCy Schubert 		JSON_NULL,
21*85732ac8SCy Schubert 	} type;
22*85732ac8SCy Schubert 	enum json_parsing_state {
23*85732ac8SCy Schubert 		JSON_EMPTY,
24*85732ac8SCy Schubert 		JSON_STARTED,
25*85732ac8SCy Schubert 		JSON_WAITING_VALUE,
26*85732ac8SCy Schubert 		JSON_COMPLETED,
27*85732ac8SCy Schubert 	} state;
28*85732ac8SCy Schubert 	char *name;
29*85732ac8SCy Schubert 	char *string;
30*85732ac8SCy Schubert 	int number;
31*85732ac8SCy Schubert 	struct json_token *parent, *child, *sibling;
32*85732ac8SCy Schubert };
33*85732ac8SCy Schubert 
34*85732ac8SCy Schubert void json_escape_string(char *txt, size_t maxlen, const char *data, size_t len);
35*85732ac8SCy Schubert struct json_token * json_parse(const char *data, size_t data_len);
36*85732ac8SCy Schubert void json_free(struct json_token *json);
37*85732ac8SCy Schubert struct json_token * json_get_member(struct json_token *json, const char *name);
38*85732ac8SCy Schubert struct wpabuf * json_get_member_base64url(struct json_token *json,
39*85732ac8SCy Schubert 					  const char *name);
40*85732ac8SCy Schubert void json_print_tree(struct json_token *root, char *buf, size_t buflen);
41*85732ac8SCy Schubert 
42*85732ac8SCy Schubert #endif /* JSON_H */
43