json: routine to parse a uint64_t.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-01-22 06:41:48 +10:30
parent ac4491909c
commit 06a25887da
2 changed files with 27 additions and 5 deletions

View File

@ -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 */
@ -59,7 +61,22 @@ bool json_tok_number(const char *buffer, const jsmntok_t *tok,
return false;
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)
{

View File

@ -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);