1 // Copyright (c) 2018-2020 Cesanta Software Limited
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #ifndef MJSON_ENABLE_PRINT
30 #define MJSON_ENABLE_PRINT 1
33 #ifndef MJSON_ENABLE_RPC
34 #define MJSON_ENABLE_RPC 1
37 #ifndef MJSON_ENABLE_BASE64
38 #define MJSON_ENABLE_BASE64 1
41 #ifndef MJSON_ENABLE_MERGE
42 #define MJSON_ENABLE_MERGE 1
45 #ifndef MJSON_ENABLE_PRETTY
46 #define MJSON_ENABLE_PRETTY 1
49 #ifndef MJSON_ENABLE_NEXT
50 #define MJSON_ENABLE_NEXT 1
53 #ifndef MJSON_RPC_LIST_NAME
54 #define MJSON_RPC_LIST_NAME "rpc.list"
57 #ifndef MJSON_DYNBUF_CHUNK
58 #define MJSON_DYNBUF_CHUNK 256 // Allocation granularity for print_dynamic_buf
62 #define MJSON_REALLOC realloc
69 #define MJSON_ERROR_INVALID_INPUT (-1)
70 #define MJSON_ERROR_TOO_DEEP (-2)
71 #define MJSON_TOK_INVALID 0
72 #define MJSON_TOK_KEY 1
73 #define MJSON_TOK_STRING 11
74 #define MJSON_TOK_NUMBER 12
75 #define MJSON_TOK_TRUE 13
76 #define MJSON_TOK_FALSE 14
77 #define MJSON_TOK_NULL 15
78 #define MJSON_TOK_ARRAY 91
79 #define MJSON_TOK_OBJECT 123
80 #define MJSON_TOK_IS_VALUE(t) ((t) > 10 && (t) < 20)
82 typedef int (*mjson_cb_t)(int event, const char *buf, int offset, int len,
85 #ifndef MJSON_MAX_DEPTH
86 #define MJSON_MAX_DEPTH 20
89 int mjson(const char *buf, int len, mjson_cb_t cb, void *ud);
90 int mjson_find(const char *buf, int len, const char *jp, const char **tp,
92 int mjson_get_number(const char *buf, int len, const char *path, double *v);
93 int mjson_get_bool(const char *buf, int len, const char *path, int *v);
94 int mjson_get_string(const char *buf, int len, const char *path, char *to,
96 int mjson_get_hex(const char *buf, int len, const char *path, char *to, int n);
99 int mjson_next(const char *buf, int len, int offset, int *key_offset,
100 int *key_len, int *val_offset, int *val_len, int *vale_type);
103 #if MJSON_ENABLE_BASE64
104 int mjson_get_base64(const char *buf, int len, const char *path, char *dst,
106 int mjson_base64_dec(const char *src, int src_len, char *dst, int dst_len);
109 #if MJSON_ENABLE_PRINT
110 typedef int (*mjson_print_fn_t)(const char *buf, int len, void *fn_data);
111 typedef int (*mjson_vprint_fn_t)(mjson_print_fn_t fn, void *, va_list *);
113 struct mjson_fixedbuf {
118 int mjson_printf(mjson_print_fn_t fn, void *fn_data, const char *fmt, ...);
119 int mjson_vprintf(mjson_print_fn_t fn, void *fn_data, const char *fmt,
121 int mjson_print_str(mjson_print_fn_t fn, void *fn_data, const char *buf,
123 int mjson_print_int(mjson_print_fn_t fn, void *fn_data, int value,
125 int mjson_print_long(mjson_print_fn_t fn, void *fn_data, long value,
127 int mjson_print_buf(mjson_print_fn_t fn, void *fn_data, const char *buf,
129 int mjson_print_dbl(mjson_print_fn_t fn, void *fn_data, double d, int width);
131 int mjson_print_null(const char *ptr, int len, void *fn_data);
132 int mjson_print_fixed_buf(const char *ptr, int len, void *fn_data);
133 int mjson_print_dynamic_buf(const char *ptr, int len, void *fn_data);
135 int mjson_snprintf(char *buf, size_t len, const char *fmt, ...);
136 char *mjson_aprintf(const char *fmt, ...);
138 #if MJSON_ENABLE_PRETTY
139 int mjson_pretty(const char *s, int n, const char *pad, mjson_print_fn_t fn,
143 #if MJSON_ENABLE_MERGE
144 int mjson_merge(const char *s, int n, const char *s2, int n2,
145 mjson_print_fn_t fn, void *fn_data);
148 #endif // MJSON_ENABLE_PRINT
152 void jsonrpc_init(mjson_print_fn_t response_cb, void *fn_data);
153 int mjson_globmatch(const char *s1, int n1, const char *s2, int n2);
155 struct jsonrpc_request {
156 struct jsonrpc_ctx *ctx;
157 const char *frame; // Points to the whole frame
158 int frame_len; // Frame length
159 const char *params; // Points to the "params" in the request frame
160 int params_len; // Length of the "params"
161 const char *id; // Points to the "id" in the request frame
162 int id_len; // Length of the "id"
163 const char *method; // Points to the "method" in the request frame
164 int method_len; // Length of the "method"
165 mjson_print_fn_t fn; // Printer function
166 void *fn_data; // Printer function data
167 void *userdata; // Callback's user data as specified at export time
170 struct jsonrpc_method {
173 void (*cb)(struct jsonrpc_request *);
174 struct jsonrpc_method *next;
177 // Main RPC context, stores current request information and a list of
178 // exported RPC methods.
180 struct jsonrpc_method *methods;
181 mjson_print_fn_t response_cb;
182 void *response_cb_data;
185 // Registers function fn under the given name within the given RPC context
186 #define jsonrpc_ctx_export(ctx, name, fn) \
188 static struct jsonrpc_method m = {(name), sizeof(name) - 1, (fn), 0}; \
189 m.next = (ctx)->methods; \
190 (ctx)->methods = &m; \
193 void jsonrpc_ctx_init(struct jsonrpc_ctx *ctx, mjson_print_fn_t response_cb,
194 void *response_cb_data);
195 void jsonrpc_return_error(struct jsonrpc_request *r, int code,
196 const char *message, const char *data_fmt, ...);
197 void jsonrpc_return_success(struct jsonrpc_request *r, const char *result_fmt,
199 void jsonrpc_ctx_process(struct jsonrpc_ctx *ctx, const char *req, int req_sz,
200 mjson_print_fn_t fn, void *fn_data, void *userdata);
202 extern struct jsonrpc_ctx jsonrpc_default_context;
203 extern void jsonrpc_list(struct jsonrpc_request *r);
205 #define jsonrpc_export(name, fn) \
206 jsonrpc_ctx_export(&jsonrpc_default_context, (name), (fn))
208 #define jsonrpc_process(buf, len, fn, fnd, ud) \
209 jsonrpc_ctx_process(&jsonrpc_default_context, (buf), (len), (fn), (fnd), (ud))
211 #define JSONRPC_ERROR_INVALID -32700 /* Invalid JSON was received */
212 #define JSONRPC_ERROR_NOT_FOUND -32601 /* The method does not exist */
213 #define JSONRPC_ERROR_BAD_PARAMS -32602 /* Invalid params passed */
214 #define JSONRPC_ERROR_INTERNAL -32603 /* Internal JSON-RPC error */
216 #endif // MJSON_ENABLE_RPC