xref: /freebsd/contrib/wpa/src/utils/json.h (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
185732ac8SCy Schubert /*
285732ac8SCy Schubert  * JavaScript Object Notation (JSON) parser (RFC7159)
385732ac8SCy Schubert  * Copyright (c) 2017, Qualcomm Atheros, Inc.
485732ac8SCy Schubert  *
585732ac8SCy Schubert  * This software may be distributed under the terms of the BSD license.
685732ac8SCy Schubert  * See README for more details.
785732ac8SCy Schubert  */
885732ac8SCy Schubert 
985732ac8SCy Schubert #ifndef JSON_H
1085732ac8SCy Schubert #define JSON_H
1185732ac8SCy Schubert 
1285732ac8SCy Schubert struct json_token {
1385732ac8SCy Schubert 	enum json_type {
1485732ac8SCy Schubert 		JSON_VALUE,
1585732ac8SCy Schubert 		JSON_OBJECT,
1685732ac8SCy Schubert 		JSON_ARRAY,
1785732ac8SCy Schubert 		JSON_STRING,
1885732ac8SCy Schubert 		JSON_NUMBER,
1985732ac8SCy Schubert 		JSON_BOOLEAN,
2085732ac8SCy Schubert 		JSON_NULL,
2185732ac8SCy Schubert 	} type;
2285732ac8SCy Schubert 	enum json_parsing_state {
2385732ac8SCy Schubert 		JSON_EMPTY,
2485732ac8SCy Schubert 		JSON_STARTED,
2585732ac8SCy Schubert 		JSON_WAITING_VALUE,
2685732ac8SCy Schubert 		JSON_COMPLETED,
2785732ac8SCy Schubert 	} state;
2885732ac8SCy Schubert 	char *name;
2985732ac8SCy Schubert 	char *string;
3085732ac8SCy Schubert 	int number;
3185732ac8SCy Schubert 	struct json_token *parent, *child, *sibling;
3285732ac8SCy Schubert };
3385732ac8SCy Schubert 
3485732ac8SCy Schubert void json_escape_string(char *txt, size_t maxlen, const char *data, size_t len);
3585732ac8SCy Schubert struct json_token * json_parse(const char *data, size_t data_len);
3685732ac8SCy Schubert void json_free(struct json_token *json);
3785732ac8SCy Schubert struct json_token * json_get_member(struct json_token *json, const char *name);
3885732ac8SCy Schubert struct wpabuf * json_get_member_base64url(struct json_token *json,
3985732ac8SCy Schubert 					  const char *name);
40*c1d255d3SCy Schubert struct wpabuf * json_get_member_base64(struct json_token *json,
41*c1d255d3SCy Schubert 				       const char *name);
4285732ac8SCy Schubert void json_print_tree(struct json_token *root, char *buf, size_t buflen);
43*c1d255d3SCy Schubert void json_add_int(struct wpabuf *json, const char *name, int val);
44*c1d255d3SCy Schubert void json_add_string(struct wpabuf *json, const char *name, const char *val);
45*c1d255d3SCy Schubert int json_add_string_escape(struct wpabuf *json, const char *name,
46*c1d255d3SCy Schubert 			   const void *val, size_t len);
47*c1d255d3SCy Schubert int json_add_base64url(struct wpabuf *json, const char *name, const void *val,
48*c1d255d3SCy Schubert 		       size_t len);
49*c1d255d3SCy Schubert int json_add_base64(struct wpabuf *json, const char *name, const void *val,
50*c1d255d3SCy Schubert 		    size_t len);
51*c1d255d3SCy Schubert void json_start_object(struct wpabuf *json, const char *name);
52*c1d255d3SCy Schubert void json_end_object(struct wpabuf *json);
53*c1d255d3SCy Schubert void json_start_array(struct wpabuf *json, const char *name);
54*c1d255d3SCy Schubert void json_end_array(struct wpabuf *json);
55*c1d255d3SCy Schubert void json_value_sep(struct wpabuf *json);
5685732ac8SCy Schubert 
5785732ac8SCy Schubert #endif /* JSON_H */
58