Browse Source

json: add json_to_bool() helper.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
plugin-7
Rusty Russell 6 years ago
parent
commit
465d5d5649
  1. 16
      common/json.c
  2. 3
      common/json.h
  3. 13
      common/json_tok.c

16
common/json.c

@ -2,6 +2,7 @@
#include "json.h" #include "json.h"
#include <assert.h> #include <assert.h>
#include <ccan/build_assert/build_assert.h> #include <ccan/build_assert/build_assert.h>
#include <ccan/mem/mem.h>
#include <ccan/str/hex/hex.h> #include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h> #include <ccan/tal/str/str.h>
#include <common/utils.h> #include <common/utils.h>
@ -111,6 +112,21 @@ bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num)
return true; 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, bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
uint64_t *satoshi) uint64_t *satoshi)
{ {

3
common/json.h

@ -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) */ /* 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); 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) */ /* 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, bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
uint64_t *satoshi); uint64_t *satoshi);

13
common/json_tok.c

@ -1,4 +1,3 @@
#include <ccan/mem/mem.h>
#include <ccan/str/hex/hex.h> #include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h> #include <ccan/tal/str/str.h>
#include <common/json_command.h> #include <common/json_command.h>
@ -25,16 +24,8 @@ bool json_tok_bool(struct command *cmd, const char *name,
bool **b) bool **b)
{ {
*b = tal(cmd, bool); *b = tal(cmd, bool);
if (tok->type == JSMN_PRIMITIVE) { if (json_to_bool(buffer, tok, *b))
if (memeqstr(buffer + tok->start, tok->end - tok->start, "true")) { return true;
**b = true;
return true;
}
if (memeqstr(buffer + tok->start, tok->end - tok->start, "false")) {
**b = false;
return true;
}
}
command_fail(cmd, JSONRPC2_INVALID_PARAMS, command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be 'true' or 'false', not '%.*s'", "'%s' should be 'true' or 'false', not '%.*s'",
name, tok->end - tok->start, buffer + tok->start); name, tok->end - tok->start, buffer + tok->start);

Loading…
Cancel
Save