Rizin
unix-like reverse engineering framework and cli tools
rz_json.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2020 thestr4ng3r <info@florianmaerkl.de>
2 // SPDX-FileCopyrightText: 2020 Yaroslav Stavnichiy <yarosla@gmail.com>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #ifndef RZ_JSON_H
6 #define RZ_JSON_H
7 
8 #include <rz_types.h>
9 #include <rz_util/rz_pj.h>
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 /*
16  * rz_json is a JSON parsing API,
17  * heavily based on nxjson by Yaroslav Stavnichiy <yarosla@gmail.com>,
18  * which is available under LGPLv3 or later.
19  *
20  * rz_json does NOT format json, it only parses. To format json, see rz_pj.h instead.
21  * It operates in-place, which means the parsed string will be MODIFIED.
22  * This means all string values in RzJson point directly into the input string,
23  * removing the need to copy them.
24  *
25  * It also supports both line and block style comments.
26  */
27 
28 typedef enum rz_json_type_t {
30  RZ_JSON_OBJECT, // properties can be found in child nodes
31  RZ_JSON_ARRAY, // items can be found in child nodes
32  RZ_JSON_STRING, // value can be found in the str_value field
33  RZ_JSON_INTEGER, // value can be found in the num.u_value/num.s_value fields
34  RZ_JSON_DOUBLE, // value can be found in the num.dbl_value field
35  RZ_JSON_BOOLEAN // value can be found in the num.u_value field
37 
38 typedef struct rz_json_t {
39  RzJsonType type; // type of json node, see above
40  const char *key; // key of the property; for object's children only
41  union {
42  const char *str_value; // text value of STRING node
43  struct {
44  union {
45  ut64 u_value; // the value of INTEGER or BOOLEAN node
47  };
48  double dbl_value; // the value of DOUBLE node
49  } num;
50  struct { // children of OBJECT or ARRAY
51  size_t count;
52  struct rz_json_t *first;
53  struct rz_json_t *last;
55  };
56  struct rz_json_t *next; // points to next child
58 
60 
61 RZ_API void rz_json_free(RzJson *js);
62 
63 RZ_API const RzJson *rz_json_get(const RzJson *json, const char *key); // get object's property by key
64 RZ_API const RzJson *rz_json_item(const RzJson *json, size_t idx); // get array element by index
65 RZ_API const RzJson *rz_json_get_path(const RzJson *json, const char *path); // reach into an object by (simple) JSON path like [0].methods[1].flags[0]
66 RZ_API RZ_OWN char *rz_json_as_string(const RzJson *json, bool with_key); // shows the string representation of RzJson
67 
68 #ifdef __cplusplus
69 }
70 #endif
71 
72 #endif /* NXJSON_H */
#define RZ_API
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
int idx
Definition: setup.py:197
RZ_API const RzJson * rz_json_get_path(const RzJson *json, const char *path)
Definition: json_parser.c:419
RZ_API const RzJson * rz_json_get(const RzJson *json, const char *key)
Definition: json_parser.c:405
rz_json_type_t
Definition: rz_json.h:28
@ RZ_JSON_INTEGER
Definition: rz_json.h:33
@ RZ_JSON_ARRAY
Definition: rz_json.h:31
@ RZ_JSON_DOUBLE
Definition: rz_json.h:34
@ RZ_JSON_OBJECT
Definition: rz_json.h:30
@ RZ_JSON_BOOLEAN
Definition: rz_json.h:35
@ RZ_JSON_STRING
Definition: rz_json.h:32
@ RZ_JSON_NULL
Definition: rz_json.h:29
struct rz_json_t RzJson
RZ_API RzJson * rz_json_parse(char *text)
Definition: json_parser.c:382
enum rz_json_type_t RzJsonType
RZ_API void rz_json_free(RzJson *js)
Definition: json_parser.c:45
RZ_API RZ_OWN char * rz_json_as_string(const RzJson *json, bool with_key)
Definition: json_parser.c:546
RZ_API const RzJson * rz_json_item(const RzJson *json, size_t idx)
Definition: json_parser.c:409
#define RZ_OWN
Definition: rz_types.h:62
#define st64
Definition: rz_types_base.h:10
size_t count
Definition: rz_json.h:51
struct rz_json_t::@304::@307 children
const char * str_value
Definition: rz_json.h:42
struct rz_json_t * last
Definition: rz_json.h:53
const char * key
Definition: rz_json.h:40
struct rz_json_t * first
Definition: rz_json.h:52
double dbl_value
Definition: rz_json.h:48
struct rz_json_t * next
Definition: rz_json.h:56
RzJsonType type
Definition: rz_json.h:39
st64 s_value
Definition: rz_json.h:46
ut64 u_value
Definition: rz_json.h:45
struct rz_json_t::@304::@306 num
ut64(WINAPI *w32_GetEnabledXStateFeatures)()