xref: /freebsd/contrib/wpa/src/utils/json.h (revision 71e72c9e91c4b8007a4292e09669e8b549c29e97)
1 /*
2  * JavaScript Object Notation (JSON) parser (RFC7159)
3  * Copyright (c) 2017, Qualcomm Atheros, Inc.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #ifndef JSON_H
10 #define JSON_H
11 
12 struct json_token {
13 	enum json_type {
14 		JSON_VALUE,
15 		JSON_OBJECT,
16 		JSON_ARRAY,
17 		JSON_STRING,
18 		JSON_NUMBER,
19 		JSON_DOUBLE,
20 		JSON_BOOLEAN,
21 		JSON_NULL,
22 	} type;
23 	enum json_parsing_state {
24 		JSON_EMPTY,
25 		JSON_STARTED,
26 		JSON_WAITING_VALUE,
27 		JSON_COMPLETED,
28 	} state;
29 	char *name;
30 	char *string;
31 	int number;
32 	double dnumber;
33 	struct json_token *parent, *child, *sibling;
34 };
35 
36 void json_escape_string(char *txt, size_t maxlen, const char *data, size_t len);
37 struct json_token * json_parse(const char *data, size_t data_len);
38 void json_free(struct json_token *json);
39 struct json_token * json_get_member(struct json_token *json, const char *name);
40 struct wpabuf * json_get_member_base64url(struct json_token *json,
41 					  const char *name);
42 struct wpabuf * json_get_member_base64(struct json_token *json,
43 				       const char *name);
44 void json_print_tree(struct json_token *root, char *buf, size_t buflen);
45 void json_add_int(struct wpabuf *json, const char *name, int val);
46 void json_add_double(struct wpabuf *json, const char *name, double val);
47 void json_add_string(struct wpabuf *json, const char *name, const char *val);
48 int json_add_string_escape(struct wpabuf *json, const char *name,
49 			   const void *val, size_t len);
50 int json_add_base64url(struct wpabuf *json, const char *name, const void *val,
51 		       size_t len);
52 int json_add_base64(struct wpabuf *json, const char *name, const void *val,
53 		    size_t len);
54 void json_start_object(struct wpabuf *json, const char *name);
55 void json_end_object(struct wpabuf *json);
56 void json_start_array(struct wpabuf *json, const char *name);
57 void json_end_array(struct wpabuf *json);
58 void json_value_sep(struct wpabuf *json);
59 
60 #endif /* JSON_H */
61