xref: /netbsd-src/external/bsd/wpa/dist/src/utils/json.h (revision bb6183629cf165db498d8e1f4e2de129f7efb21c)
10a73ee0aSchristos /*
20a73ee0aSchristos  * JavaScript Object Notation (JSON) parser (RFC7159)
30a73ee0aSchristos  * Copyright (c) 2017, Qualcomm Atheros, Inc.
40a73ee0aSchristos  *
50a73ee0aSchristos  * This software may be distributed under the terms of the BSD license.
60a73ee0aSchristos  * See README for more details.
70a73ee0aSchristos  */
80a73ee0aSchristos 
90a73ee0aSchristos #ifndef JSON_H
100a73ee0aSchristos #define JSON_H
110a73ee0aSchristos 
120a73ee0aSchristos struct json_token {
130a73ee0aSchristos 	enum json_type {
140a73ee0aSchristos 		JSON_VALUE,
150a73ee0aSchristos 		JSON_OBJECT,
160a73ee0aSchristos 		JSON_ARRAY,
170a73ee0aSchristos 		JSON_STRING,
180a73ee0aSchristos 		JSON_NUMBER,
190a73ee0aSchristos 		JSON_BOOLEAN,
200a73ee0aSchristos 		JSON_NULL,
210a73ee0aSchristos 	} type;
220a73ee0aSchristos 	enum json_parsing_state {
230a73ee0aSchristos 		JSON_EMPTY,
240a73ee0aSchristos 		JSON_STARTED,
250a73ee0aSchristos 		JSON_WAITING_VALUE,
260a73ee0aSchristos 		JSON_COMPLETED,
270a73ee0aSchristos 	} state;
280a73ee0aSchristos 	char *name;
290a73ee0aSchristos 	char *string;
300a73ee0aSchristos 	int number;
310a73ee0aSchristos 	struct json_token *parent, *child, *sibling;
320a73ee0aSchristos };
330a73ee0aSchristos 
340a73ee0aSchristos void json_escape_string(char *txt, size_t maxlen, const char *data, size_t len);
350a73ee0aSchristos struct json_token * json_parse(const char *data, size_t data_len);
360a73ee0aSchristos void json_free(struct json_token *json);
370a73ee0aSchristos struct json_token * json_get_member(struct json_token *json, const char *name);
380a73ee0aSchristos struct wpabuf * json_get_member_base64url(struct json_token *json,
390a73ee0aSchristos 					  const char *name);
40*bb618362Schristos struct wpabuf * json_get_member_base64(struct json_token *json,
41*bb618362Schristos 				       const char *name);
420a73ee0aSchristos void json_print_tree(struct json_token *root, char *buf, size_t buflen);
43*bb618362Schristos void json_add_int(struct wpabuf *json, const char *name, int val);
44*bb618362Schristos void json_add_string(struct wpabuf *json, const char *name, const char *val);
45*bb618362Schristos int json_add_string_escape(struct wpabuf *json, const char *name,
46*bb618362Schristos 			   const void *val, size_t len);
47*bb618362Schristos int json_add_base64url(struct wpabuf *json, const char *name, const void *val,
48*bb618362Schristos 		       size_t len);
49*bb618362Schristos int json_add_base64(struct wpabuf *json, const char *name, const void *val,
50*bb618362Schristos 		    size_t len);
51*bb618362Schristos void json_start_object(struct wpabuf *json, const char *name);
52*bb618362Schristos void json_end_object(struct wpabuf *json);
53*bb618362Schristos void json_start_array(struct wpabuf *json, const char *name);
54*bb618362Schristos void json_end_array(struct wpabuf *json);
55*bb618362Schristos void json_value_sep(struct wpabuf *json);
560a73ee0aSchristos 
570a73ee0aSchristos #endif /* JSON_H */
58