1 /* radare - LGPL - Copyright 2020 - thestr4ng3r, Yaroslav Stavnichiy */
2 
3 #ifndef R_JSON_H
4 #define R_JSON_H
5 
6 #include <r_types.h>
7 
8 #ifdef  __cplusplus
9 extern "C" {
10 #endif
11 
12 /*
13  * r_json is a JSON parsing API,
14  * heavily based on nxjson by Yaroslav Stavnichiy <yarosla@gmail.com>,
15  * which is available under LGPLv3 or later.
16  *
17  * r_json does NOT format json, it only parses. To format json, see pj.h instead.
18  * It operates in-place, which means the parsed string will be MODIFIED.
19  * This means all string values in RJson point directly into the input string,
20  * removing the need to copy them.
21  *
22  * It also supports both line and block style comments.
23  */
24 
25 typedef enum r_json_type_t {
26 	R_JSON_NULL,
27 	R_JSON_OBJECT,  // properties can be found in child nodes
28 	R_JSON_ARRAY,   // items can be found in child nodes
29 	R_JSON_STRING,  // value can be found in the str_value field
30 	R_JSON_INTEGER, // value can be found in the num.u_value/num.s_value fields
31 	R_JSON_DOUBLE,  // value can be found in the num.dbl_value field
32 	R_JSON_BOOLEAN  // value can be found in the num.u_value field
33 } RJsonType;
34 
35 typedef struct r_json_t {
36 	RJsonType type;             // type of json node, see above
37 	const char *key;            // key of the property; for object's children only
38 	union {
39 		const char *str_value;  // text value of STRING node
40 		struct {
41 			union {
42 				ut64 u_value;   // the value of INTEGER or BOOLEAN node
43 				st64 s_value;
44 			};
45 			double dbl_value;   // the value of DOUBLE node
46 		} num;
47 		struct {                // children of OBJECT or ARRAY
48 			size_t count;
49 			struct r_json_t *first;
50 			struct r_json_t *last;
51 		} children;
52 	};
53 	struct r_json_t *next;    // points to next child
54 } RJson;
55 
56 R_API RJson *r_json_parse(char *text);
57 
58 R_API void r_json_free(RJson *js);
59 
60 R_API const RJson *r_json_get(const RJson *json, const char *key); // get object's property by key
61 R_API const RJson *r_json_item(const RJson *json, size_t idx); // get array element by index
62 
63 #ifdef  __cplusplus
64 }
65 #endif
66 
67 #endif  /* NXJSON_H */
68