1 #ifndef R_PJ_H
2 #define R_PJ_H 1
3 #define R_PRINT_JSON_DEPTH_LIMIT 128
4 
5 #include <r_util/r_strbuf.h>
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 /* new encoding options of j commands */
12 typedef enum PJEncodingStr {
13 	PJ_ENCODING_STR_DEFAULT = 0,
14 	PJ_ENCODING_STR_BASE64,
15 	PJ_ENCODING_STR_HEX,
16 	PJ_ENCODING_STR_ARRAY,
17 	PJ_ENCODING_STR_STRIP
18 } PJEncodingStr;
19 
20 typedef enum PJEncodingNum {
21 	PJ_ENCODING_NUM_DEFAULT = 0,
22 	PJ_ENCODING_NUM_STR,
23 	PJ_ENCODING_NUM_HEX
24 } PJEncodingNum;
25 
26 typedef struct pj_t {
27 	RStrBuf sb;
28 	bool is_first;
29 	bool is_key;
30 	char braces[R_PRINT_JSON_DEPTH_LIMIT];
31 	int level;
32 	PJEncodingStr str_encoding;
33 	PJEncodingNum num_encoding;
34 } PJ;
35 
36 /* lifecycle */
37 R_API PJ *pj_new(void);
38 R_API PJ *pj_new_with_encoding(PJEncodingStr str_encoding, PJEncodingNum num_encoding);
39 R_API void pj_free(PJ *j);
40 R_API void pj_reset(PJ *j); // clear the pj contents, but keep the buffer allocated to re-use it
41 R_API char *pj_drain(PJ *j);
42 /* encode the pj data as a string */
43 R_API const char *pj_string(PJ *pj);
44 // R_API void pj_print(PJ *j, PrintfCallback cb);
45 
46 /* nesting */
47 //R_API PJ *pj_begin(char type, PrintfCallback cb);
48 /* close the current json list or array */
49 R_API PJ *pj_end(PJ *j);
50 R_API void pj_raw(PJ *j, const char *k);
51 
52 /* object, array */
53 /* open new json list { */
54 R_API PJ *pj_o(PJ *j);
55 /* open new array [ */
56 R_API PJ *pj_a(PJ *j);
57 
58 /* keys, values */
59 /* new key with no value "name": */
60 R_API PJ *pj_k(PJ *j, const char *k);
61 /* "name":"null" */
62 R_API PJ *pj_knull(PJ *j, const char *k);
63 /* unsigned "name":n */
64 R_API PJ *pj_kn(PJ *j, const char *k, ut64 n);
65 /* signed "name":n */
66 R_API PJ *pj_kN(PJ *j, const char *k, st64 n);
67 /* literal key "name":"key" */
68 R_API PJ *pj_ks(PJ *j, const char *k, const char *v);
69 
70 /* begin named array entry: "name": [...] */
71 R_API PJ *pj_ka(PJ *j, const char *k);
72 /* begin named json entry: "name": {...} */
73 R_API PJ *pj_ko(PJ *j, const char *k);
74 
75 /* named entry for primitive types */
76 R_API PJ *pj_ki(PJ *j, const char *k, int d);
77 R_API PJ *pj_kd(PJ *j, const char *k, double d);
78 R_API PJ *pj_kf(PJ *j, const char *k, float d);
79 R_API PJ *pj_kb(PJ *j, const char *k, bool v);
80 
81 /* named "null" */
82 R_API PJ *pj_null(PJ *j);
83 
84 /* append all uchars in v as signed ints (?) */
85 R_API PJ *pj_r(PJ *j, const unsigned char *v, size_t v_len);
86 
87 /* named entry with pj_r */
88 R_API PJ *pj_kr(PJ *j, const char *k, const unsigned char *v, size_t v_len);
89 
90 /* string, escaped for json */
91 R_API PJ *pj_s(PJ *j, const char *k);
92 /* string, raw */
93 R_API PJ *pj_j(PJ *j, const char *k);
94 /* string, encoded */
95 R_API PJ *pj_se(PJ *j, const char *k);
96 /* ut64, encoded */
97 R_API PJ *pj_ne(PJ *j, ut64 n);
98 
99 /* formatted primitive types */
100 R_API PJ *pj_n(PJ *j, ut64 n);
101 R_API PJ *pj_N(PJ *j, st64 n);
102 R_API PJ *pj_i(PJ *j, int d);
103 R_API PJ *pj_d(PJ *j, double d);
104 R_API PJ *pj_f(PJ *j, float d);
105 R_API PJ *pj_b(PJ *j, bool v);
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif
112 
113