mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-03 10:46:58 +01:00
json: add json_to_bool() helper.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
024b1a8d54
commit
465d5d5649
3 changed files with 21 additions and 11 deletions
|
@ -2,6 +2,7 @@
|
|||
#include "json.h"
|
||||
#include <assert.h>
|
||||
#include <ccan/build_assert/build_assert.h>
|
||||
#include <ccan/mem/mem.h>
|
||||
#include <ccan/str/hex/hex.h>
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <common/utils.h>
|
||||
|
@ -111,6 +112,21 @@ bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b)
|
||||
{
|
||||
if (tok->type != JSMN_PRIMITIVE)
|
||||
return false;
|
||||
if (memeqstr(buffer + tok->start, tok->end - tok->start, "true")) {
|
||||
*b = true;
|
||||
return true;
|
||||
}
|
||||
if (memeqstr(buffer + tok->start, tok->end - tok->start, "false")) {
|
||||
*b = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
|
||||
uint64_t *satoshi)
|
||||
{
|
||||
|
|
|
@ -40,6 +40,9 @@ bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num);
|
|||
/* Extract signed integer from this (may be a string, or a number literal) */
|
||||
bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num);
|
||||
|
||||
/* Extract boolean from this */
|
||||
bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b);
|
||||
|
||||
/* Extract satoshis from this (may be a string, or a decimal number literal) */
|
||||
bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
|
||||
uint64_t *satoshi);
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
#include <ccan/mem/mem.h>
|
||||
#include <ccan/str/hex/hex.h>
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <common/json_command.h>
|
||||
|
@ -25,16 +24,8 @@ bool json_tok_bool(struct command *cmd, const char *name,
|
|||
bool **b)
|
||||
{
|
||||
*b = tal(cmd, bool);
|
||||
if (tok->type == JSMN_PRIMITIVE) {
|
||||
if (memeqstr(buffer + tok->start, tok->end - tok->start, "true")) {
|
||||
**b = true;
|
||||
return true;
|
||||
}
|
||||
if (memeqstr(buffer + tok->start, tok->end - tok->start, "false")) {
|
||||
**b = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (json_to_bool(buffer, tok, *b))
|
||||
return true;
|
||||
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
||||
"'%s' should be 'true' or 'false', not '%.*s'",
|
||||
name, tok->end - tok->start, buffer + tok->start);
|
||||
|
|
Loading…
Add table
Reference in a new issue