xref: /freebsd/contrib/wpa/src/utils/json.h (revision 12af59c2cf8dbede2c1386c4744e725b1e1f8bd9)
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_BOOLEAN,
20 		JSON_NULL,
21 	} type;
22 	enum json_parsing_state {
23 		JSON_EMPTY,
24 		JSON_STARTED,
25 		JSON_WAITING_VALUE,
26 		JSON_COMPLETED,
27 	} state;
28 	char *name;
29 	char *string;
30 	int number;
31 	struct json_token *parent, *child, *sibling;
32 };
33 
34 void json_escape_string(char *txt, size_t maxlen, const char *data, size_t len);
35 struct json_token * json_parse(const char *data, size_t data_len);
36 void json_free(struct json_token *json);
37 struct json_token * json_get_member(struct json_token *json, const char *name);
38 struct wpabuf * json_get_member_base64url(struct json_token *json,
39 					  const char *name);
40 struct wpabuf * json_get_member_base64(struct json_token *json,
41 				       const char *name);
42 void json_print_tree(struct json_token *root, char *buf, size_t buflen);
43 void json_add_int(struct wpabuf *json, const char *name, int val);
44 void json_add_string(struct wpabuf *json, const char *name, const char *val);
45 int json_add_string_escape(struct wpabuf *json, const char *name,
46 			   const void *val, size_t len);
47 int json_add_base64url(struct wpabuf *json, const char *name, const void *val,
48 		       size_t len);
49 int json_add_base64(struct wpabuf *json, const char *name, const void *val,
50 		    size_t len);
51 void json_start_object(struct wpabuf *json, const char *name);
52 void json_end_object(struct wpabuf *json);
53 void json_start_array(struct wpabuf *json, const char *name);
54 void json_end_array(struct wpabuf *json);
55 void json_value_sep(struct wpabuf *json);
56 
57 #endif /* JSON_H */
58