2017-08-28 18:06:01 +02:00
|
|
|
#ifndef LIGHTNING_COMMON_JSON_H
|
|
|
|
#define LIGHTNING_COMMON_JSON_H
|
2016-01-21 21:11:48 +01:00
|
|
|
#include "config.h"
|
2016-08-31 08:36:08 +02:00
|
|
|
#include <bitcoin/pubkey.h>
|
2018-03-26 02:08:15 +02:00
|
|
|
#include <ccan/take/take.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <ccan/tal/tal.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
#define JSMN_STRICT 1
|
2017-08-28 18:11:01 +02:00
|
|
|
# include <external/jsmn/jsmn.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-03-26 02:08:47 +02:00
|
|
|
struct json_escaped;
|
2016-01-21 21:11:48 +01:00
|
|
|
struct json_result;
|
2017-08-28 18:04:01 +02:00
|
|
|
struct short_channel_id;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
/* Include " if it's a string. */
|
|
|
|
const char *json_tok_contents(const char *buffer, const jsmntok_t *t);
|
|
|
|
|
|
|
|
/* Include " if it's a string. */
|
|
|
|
int json_tok_len(const jsmntok_t *t);
|
|
|
|
|
|
|
|
/* Is this a string equal to str? */
|
|
|
|
bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str);
|
|
|
|
|
|
|
|
/* Extract number from this (may be a string, or a number literal) */
|
2018-08-10 22:16:22 +02:00
|
|
|
bool json_to_number(const char *buffer, const jsmntok_t *tok,
|
|
|
|
unsigned int *num);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Extract number from this (may be a string, or a number literal) */
|
2018-08-13 18:16:41 +02:00
|
|
|
bool json_to_u64(const char *buffer, const jsmntok_t *tok,
|
|
|
|
uint64_t *num);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-09-06 09:17:48 +02:00
|
|
|
/* Extract double from this (must be a number literal) */
|
|
|
|
bool json_tok_double(const char *buffer, const jsmntok_t *tok, double *num);
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Extract satoshis from this (may be a string, or a decimal number literal) */
|
|
|
|
bool json_tok_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
|
|
|
|
uint64_t *satoshi);
|
|
|
|
|
2018-07-20 03:14:02 +02:00
|
|
|
/* Extract double in range [0.0, 100.0] */
|
|
|
|
bool json_tok_percent(const char *buffer, const jsmntok_t *tok, double *num);
|
|
|
|
|
2016-05-26 07:55:24 +02:00
|
|
|
/* Extract boolean this (must be a true or false) */
|
|
|
|
bool json_tok_bool(const char *buffer, const jsmntok_t *tok, bool *b);
|
|
|
|
|
2018-07-31 06:11:01 +02:00
|
|
|
/* Extract sha256 hash */
|
|
|
|
bool json_tok_sha256(const char *buffer, const jsmntok_t * tok,
|
|
|
|
struct sha256 *hash);
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Is this the null primitive? */
|
|
|
|
bool json_tok_is_null(const char *buffer, const jsmntok_t *tok);
|
|
|
|
|
|
|
|
/* Returns next token with same parent. */
|
|
|
|
const jsmntok_t *json_next(const jsmntok_t *tok);
|
|
|
|
|
|
|
|
/* Get top-level member. */
|
|
|
|
const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[],
|
|
|
|
const char *label);
|
|
|
|
|
|
|
|
/* Get index'th array member. */
|
2016-08-31 09:49:40 +02:00
|
|
|
const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
/* If input is complete and valid, return tokens. */
|
|
|
|
jsmntok_t *json_parse_input(const char *input, int len, bool *valid);
|
|
|
|
|
|
|
|
/* Creating JSON strings */
|
|
|
|
|
|
|
|
/* '"fieldname" : [ ' or '[ ' if fieldname is NULL */
|
|
|
|
void json_array_start(struct json_result *ptr, const char *fieldname);
|
|
|
|
/* '"fieldname" : { ' or '{ ' if fieldname is NULL */
|
|
|
|
void json_object_start(struct json_result *ptr, const char *fieldname);
|
|
|
|
/* ' ], ' */
|
|
|
|
void json_array_end(struct json_result *ptr);
|
|
|
|
/* ' }, ' */
|
|
|
|
void json_object_end(struct json_result *ptr);
|
|
|
|
|
|
|
|
struct json_result *new_json_result(const tal_t *ctx);
|
|
|
|
|
2018-01-29 01:30:15 +01:00
|
|
|
/* '"fieldname" : "value"' or '"value"' if fieldname is NULL. Turns
|
2018-03-26 02:08:47 +02:00
|
|
|
* any non-printable chars into JSON escapes, but leaves existing escapes alone.
|
2018-01-29 01:30:15 +01:00
|
|
|
*/
|
2016-01-21 21:11:48 +01:00
|
|
|
void json_add_string(struct json_result *result, const char *fieldname, const char *value);
|
2018-01-29 01:30:15 +01:00
|
|
|
|
2018-03-26 02:08:47 +02:00
|
|
|
/* '"fieldname" : "value"' or '"value"' if fieldname is NULL. String must
|
|
|
|
* already be JSON escaped as necessary. */
|
|
|
|
void json_add_escaped_string(struct json_result *result,
|
|
|
|
const char *fieldname,
|
|
|
|
const struct json_escaped *esc TAKES);
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* '"fieldname" : literal' or 'literal' if fieldname is NULL*/
|
|
|
|
void json_add_literal(struct json_result *result, const char *fieldname,
|
|
|
|
const char *literal, int len);
|
|
|
|
/* '"fieldname" : value' or 'value' if fieldname is NULL */
|
2018-02-07 14:06:36 +01:00
|
|
|
void json_add_double(struct json_result *result, const char *fieldname,
|
|
|
|
double value);
|
|
|
|
/* '"fieldname" : value' or 'value' if fieldname is NULL */
|
2016-01-21 21:11:48 +01:00
|
|
|
void json_add_num(struct json_result *result, const char *fieldname,
|
|
|
|
unsigned int value);
|
2016-01-21 21:11:48 +01:00
|
|
|
/* '"fieldname" : value' or 'value' if fieldname is NULL */
|
|
|
|
void json_add_u64(struct json_result *result, const char *fieldname,
|
|
|
|
uint64_t value);
|
2016-01-21 21:11:48 +01:00
|
|
|
/* '"fieldname" : true|false' or 'true|false' if fieldname is NULL */
|
|
|
|
void json_add_bool(struct json_result *result, const char *fieldname,
|
|
|
|
bool value);
|
|
|
|
/* '"fieldname" : "0189abcdef..."' or "0189abcdef..." if fieldname is NULL */
|
|
|
|
void json_add_hex(struct json_result *result, const char *fieldname,
|
|
|
|
const void *data, size_t len);
|
2018-07-28 07:53:33 +02:00
|
|
|
/* '"fieldname" : "0189abcdef..."' or "0189abcdef..." if fieldname is NULL */
|
|
|
|
void json_add_hex_talarr(struct json_result *result,
|
|
|
|
const char *fieldname,
|
|
|
|
const tal_t *data);
|
2016-01-21 21:11:48 +01:00
|
|
|
void json_add_object(struct json_result *result, ...);
|
|
|
|
|
|
|
|
const char *json_result_string(const struct json_result *result);
|
2017-08-28 18:06:01 +02:00
|
|
|
#endif /* LIGHTNING_COMMON_JSON_H */
|