Browse Source

json: routine to parse a uint64_t.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
06a25887da
  1. 21
      daemon/json.c
  2. 9
      daemon/json.h

21
daemon/json.c

@ -1,6 +1,7 @@
/* JSON core and helpers */
#include "json.h"
#include <assert.h>
#include <ccan/build_assert/build_assert.h>
#include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h>
#include <ccan/tal/tal.h>
@ -39,8 +40,8 @@ bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str)
return strncmp(buffer + tok->start, str, tok->end - tok->start) == 0;
}
bool json_tok_number(const char *buffer, const jsmntok_t *tok,
unsigned int *num)
bool json_tok_u64(const char *buffer, const jsmntok_t *tok,
uint64_t *num)
{
char *end;
unsigned long l;
@ -49,6 +50,7 @@ bool json_tok_number(const char *buffer, const jsmntok_t *tok,
if (end != buffer + tok->end)
return false;
BUILD_ASSERT(sizeof(l) >= sizeof(*num));
*num = l;
/* Check for overflow */
@ -61,6 +63,21 @@ bool json_tok_number(const char *buffer, const jsmntok_t *tok,
return true;
}
bool json_tok_number(const char *buffer, const jsmntok_t *tok,
unsigned int *num)
{
uint64_t u64;
if (!json_tok_u64(buffer, tok, &u64))
return false;
*num = u64;
/* Just in case it doesn't fit. */
if (*num != u64)
return false;
return true;
}
bool json_tok_is_null(const char *buffer, const jsmntok_t *tok)
{
if (tok->type != JSMN_PRIMITIVE)

9
daemon/json.h

@ -1,9 +1,10 @@
#ifndef LIGHTNING_DAEMON_JSON_H
#define LIGHTNING_DAEMON_JSON_H
#include "config.h"
#include "stdbool.h"
#include "stdlib.h"
#include <ccan/tal/tal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define JSMN_STRICT 1
# include "jsmn/jsmn.h"
@ -23,6 +24,10 @@ bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str);
bool json_tok_number(const char *buffer, const jsmntok_t *tok,
unsigned int *num);
/* Extract number from this (may be a string, or a number literal) */
bool json_tok_u64(const char *buffer, const jsmntok_t *tok,
uint64_t *num);
/* Is this the null primitive? */
bool json_tok_is_null(const char *buffer, const jsmntok_t *tok);

Loading…
Cancel
Save