1 #ifndef LIGHTNING_COMMON_JSON_H 2 #define LIGHTNING_COMMON_JSON_H 3 #include "config.h" 4 #include <ccan/crypto/sha256/sha256.h> 5 #include <ccan/tal/tal.h> 6 #include <common/errcode.h> 7 8 #define JSMN_STRICT 1 9 # include <external/jsmn/jsmn.h> 10 11 struct json_escape; 12 struct json_stream; 13 struct timeabs; 14 struct timespec; 15 16 /* Include " if it's a string. */ 17 const char *json_tok_full(const char *buffer, const jsmntok_t *t); 18 19 /* Include " if it's a string. */ 20 int json_tok_full_len(const jsmntok_t *t); 21 22 /* Is this a string equal to str? */ 23 bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str); 24 25 /* Is this a string equal to str of length len? */ 26 bool json_tok_strneq(const char *buffer, const jsmntok_t *tok, 27 const char *str, size_t len); 28 29 /* Does this string token start with prefix? */ 30 bool json_tok_startswith(const char *buffer, const jsmntok_t *tok, 31 const char *prefix); 32 33 /* Does this string token end with suffix? */ 34 bool json_tok_endswith(const char *buffer, const jsmntok_t *tok, 35 const char *suffix); 36 37 /* Allocate a tal string copy */ 38 char *json_strdup(const tal_t *ctx, const char *buffer, const jsmntok_t *tok); 39 40 /* Decode a hex-encoded binary */ 41 u8 *json_tok_bin_from_hex(const tal_t *ctx, const char *buffer, const jsmntok_t *tok); 42 43 /* Extract number from this (may be a string, or a number literal) */ 44 bool json_to_number(const char *buffer, const jsmntok_t *tok, 45 unsigned int *num); 46 47 /* Extract number from this (may be a string, or a number literal) */ 48 bool json_to_u64(const char *buffer, const jsmntok_t *tok, 49 uint64_t *num); 50 51 /* Extract signed 64 bit integer from this (may be a string, or a number literal) */ 52 bool json_to_s64(const char *buffer, const jsmntok_t *tok, s64 *num); 53 54 /* Extract number from this (may be a string, or a number literal) */ 55 bool json_to_u32(const char *buffer, const jsmntok_t *tok, 56 uint32_t *num); 57 58 /* Extract number from this (may be a string, or a number literal) */ 59 bool json_to_u16(const char *buffer, const jsmntok_t *tok, 60 uint16_t *num); 61 62 bool json_to_sha256(const char *buffer, const jsmntok_t *tok, struct sha256 *dest); 63 /* 64 * Extract a non-negative (either 0 or positive) floating-point number from this 65 * (must be a number literal), multiply it by 1 million and return it as an 66 * integer. Any fraction smaller than 0.000001 is ignored. 67 */ 68 bool json_to_millionths(const char *buffer, const jsmntok_t *tok, 69 u64 *millionths); 70 71 /* Extract signed integer from this (may be a string, or a number literal) */ 72 bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num); 73 74 /* Extract an error code from this (may be a string, or a number literal) */ 75 bool json_to_errcode(const char *buffer, const jsmntok_t *tok, errcode_t *errcode); 76 77 /* Extract boolean from this */ 78 bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b); 79 80 /* Is this a number? [0..9]+ */ 81 bool json_tok_is_num(const char *buffer, const jsmntok_t *tok); 82 83 /* Is this the null primitive? */ 84 bool json_tok_is_null(const char *buffer, const jsmntok_t *tok); 85 86 /* Returns next token with same parent (WARNING: slow!). */ 87 const jsmntok_t *json_next(const jsmntok_t *tok); 88 89 /* Get top-level member. */ 90 const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[], 91 const char *label); 92 93 /* Get top-level member, with explicit label length */ 94 const jsmntok_t *json_get_membern(const char *buffer, const jsmntok_t tok[], 95 const char *label, size_t len); 96 97 /* Get index'th array member. */ 98 const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index); 99 100 /* Allocate a starter array of tokens for json_parse_input */ 101 jsmntok_t *toks_alloc(const tal_t *ctx); 102 103 /* Reset a token array to reuse it. */ 104 void toks_reset(jsmntok_t *toks); 105 106 /** 107 * json_parse_input: parse and validate JSON. 108 * @parser: parser initialized with jsmn_init. 109 * @toks: tallocated array from toks_alloc() 110 * @input, @len: input string. 111 * @complete: set to true if the valid JSON is complete, or NULL if must be. 112 * 113 * This returns false if the JSON is invalid, true otherwise. 114 * If it returns true, *@complete indicates that (*@toks)[0] points to a 115 * valid, complete JSON element. If @complete is NULL, then incomplete 116 * JSON returns false (i.e. is considered invalid). 117 * 118 * *@toks is resized to the complete set of tokens, with a dummy 119 * terminator (type == -1) at the end. 120 * 121 * If it returns true, and *@complete is false, you can append more 122 * data to @input and call it again (with the same perser) and the parser 123 * will continue where it left off. 124 */ 125 bool json_parse_input(jsmn_parser *parser, 126 jsmntok_t **toks, 127 const char *input, int len, 128 bool *complete); 129 130 /* Simplified version of above which parses only a complete, valid 131 * JSON string */ 132 jsmntok_t *json_parse_simple(const tal_t *ctx, const char *input, int len); 133 134 /* Convert a jsmntype_t enum to a human readable string. */ 135 const char *jsmntype_to_string(jsmntype_t t); 136 137 /* Print a json value for debugging purposes. */ 138 void json_tok_print(const char *buffer, const jsmntok_t *params); 139 140 /* Return a copy of a json value as an array. */ 141 jsmntok_t *json_tok_copy(const tal_t *ctx, const jsmntok_t *tok); 142 143 /* 144 * Remove @num json values from a json array or object @obj. @tok points 145 * to the first value to remove. The array @tokens will be resized. 146 */ 147 void json_tok_remove(jsmntok_t **tokens, 148 jsmntok_t *obj_or_array, const jsmntok_t *tok, size_t num); 149 150 /* Guide is % for a token: each must be followed by JSON_SCAN(). 151 * Returns NULL on error (asserts() on bad guide). */ 152 const char *json_scan(const tal_t *ctx, 153 const char *buffer, 154 const jsmntok_t *tok, 155 const char *guide, 156 ...); 157 158 /* eg. JSON_SCAN(json_to_bool, &boolvar) */ 159 #define JSON_SCAN(fmt, var) \ 160 json_scan, \ 161 stringify(fmt), \ 162 ((var) + 0*sizeof(fmt((const char *)NULL, \ 163 (const jsmntok_t *)NULL, var) == true)), \ 164 (fmt) 165 166 /* eg. JSON_SCAN_TAL(tmpctx, json_strdup, &charvar) */ 167 #define JSON_SCAN_TAL(ctx, fmt, var) \ 168 (ctx), \ 169 stringify(fmt), \ 170 ((var) + 0*sizeof((*var) = fmt((ctx), \ 171 (const char *)NULL, \ 172 (const jsmntok_t *)NULL))), \ 173 (fmt) 174 175 /* Already-have-varargs version */ 176 const char *json_scanv(const tal_t *ctx, 177 const char *buffer, 178 const jsmntok_t *tok, 179 const char *guide, 180 va_list ap); 181 182 /* Iterator macro for array: i is counter, t is token ptr, arr is JSMN_ARRAY */ 183 #define json_for_each_arr(i, t, arr) \ 184 for (i = 0, t = (arr) + 1; i < (arr)->size; t = json_next(t), i++) 185 186 /* Iterator macro for object: i is counter, t is token ptr (t+1 is 187 * contents of obj member), obj is JSMN_OBJECT */ 188 #define json_for_each_obj(i, t, obj) \ 189 for (i = 0, t = (obj) + 1; i < (obj)->size; t = json_next(t+1), i++) 190 191 192 /* '"fieldname" : "value"' or '"value"' if fieldname is NULL. Turns 193 * any non-printable chars into JSON escapes, but leaves existing escapes alone. 194 */ 195 void json_add_string(struct json_stream *result, const char *fieldname, const char *value); 196 197 /* '"fieldname" : "value[:value_len]"' or '"value[:value_len]"' if 198 * fieldname is NULL. Turns any non-printable chars into JSON 199 * escapes, but leaves existing escapes alone. 200 */ 201 void json_add_stringn(struct json_stream *result, const char *fieldname, 202 const char *value TAKES, size_t value_len); 203 204 /* '"fieldname" : "value"' or '"value"' if fieldname is NULL. String must 205 * already be JSON escaped as necessary. */ 206 void json_add_escaped_string(struct json_stream *result, 207 const char *fieldname, 208 const struct json_escape *esc TAKES); 209 210 /* '"fieldname" : literal' or 'literal' if fieldname is NULL*/ 211 void json_add_literal(struct json_stream *result, const char *fieldname, 212 const char *literal, int len); 213 /* '"fieldname" : value' or 'value' if fieldname is NULL */ 214 void json_add_num(struct json_stream *result, const char *fieldname, 215 unsigned int value); 216 /* '"fieldname" : value' or 'value' if fieldname is NULL */ 217 void json_add_u64(struct json_stream *result, const char *fieldname, 218 uint64_t value); 219 /* '"fieldname" : value' or 'value' if fieldname is NULL */ 220 void json_add_s64(struct json_stream *result, const char *fieldname, 221 int64_t value); 222 /* '"fieldname" : value' or 'value' if fieldname is NULL */ 223 void json_add_u32(struct json_stream *result, const char *fieldname, 224 uint32_t value); 225 /* '"fieldname" : value' or 'value' if fieldname is NULL */ 226 void json_add_s32(struct json_stream *result, const char *fieldname, 227 int32_t value); 228 /* '"fieldname" : true|false' or 'true|false' if fieldname is NULL */ 229 void json_add_bool(struct json_stream *result, const char *fieldname, 230 bool value); 231 232 /* '"fieldname" : null' or 'null' if fieldname is NULL */ 233 void json_add_null(struct json_stream *stream, const char *fieldname); 234 235 /* '"fieldname" : "0189abcdef..."' or "0189abcdef..." if fieldname is NULL */ 236 void json_add_hex(struct json_stream *result, const char *fieldname, 237 const void *data, size_t len); 238 /* '"fieldname" : "0189abcdef..."' or "0189abcdef..." if fieldname is NULL */ 239 void json_add_hex_talarr(struct json_stream *result, 240 const char *fieldname, 241 const tal_t *data); 242 243 void json_add_timeabs(struct json_stream *result, const char *fieldname, 244 struct timeabs t); 245 246 /* used in log.c and notification.c*/ 247 void json_add_time(struct json_stream *result, const char *fieldname, 248 struct timespec ts); 249 250 /* Add ISO_8601 timestamp string, i.e. "2019-09-07T15:50+01:00" */ 251 void json_add_timeiso(struct json_stream *result, 252 const char *fieldname, 253 struct timeabs *time); 254 255 /* Add any json token */ 256 void json_add_tok(struct json_stream *result, const char *fieldname, 257 const jsmntok_t *tok, const char *buffer); 258 259 /* Add an error code */ 260 void json_add_errcode(struct json_stream *result, const char *fieldname, 261 errcode_t code); 262 263 /* Add "bolt11" or "bolt12" field, depending on invstring. */ 264 void json_add_invstring(struct json_stream *result, const char *invstring); 265 266 #endif /* LIGHTNING_COMMON_JSON_H */ 267