Change the underlying type of JSON integer from long to json_int_t

json_int_t is typedef'd to long long if it's supported, or long
otherwise. There's also some supporting things, like the
JSON_INTEGER_FORMAT macro that expands to the printf() conversion
specifier that corresponds to json_int_t's actual type.

This is a backwards incompatible change.
This commit is contained in:
Petri Lehtinen
2010-08-13 22:06:01 +03:00
parent 7ce70533c9
commit ffbab6fedd
17 changed files with 120 additions and 45 deletions

View File

@@ -185,7 +185,9 @@ static int do_dump(const json_t *json, size_t flags, int depth,
char buffer[MAX_INTEGER_STR_LENGTH];
int size;
size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%li", json_integer_value(json));
size = snprintf(buffer, MAX_INTEGER_STR_LENGTH,
"%" JSON_INTEGER_FORMAT,
json_integer_value(json));
if(size >= MAX_INTEGER_STR_LENGTH)
return -1;

View File

@@ -34,6 +34,14 @@ typedef struct {
size_t refcount;
} json_t;
#if JSON_INTEGER_IS_LONG_LONG
#define JSON_INTEGER_FORMAT "lld"
typedef long long json_int_t;
#else
#define JSON_INTEGER_FORMAT "ld"
typedef long json_int_t;
#endif /* JSON_INTEGER_IS_LONG_LONG */
#define json_typeof(json) ((json)->type)
#define json_is_object(json) (json && json_typeof(json) == JSON_OBJECT)
#define json_is_array(json) (json && json_typeof(json) == JSON_ARRAY)
@@ -52,7 +60,7 @@ json_t *json_object(void);
json_t *json_array(void);
json_t *json_string(const char *value);
json_t *json_string_nocheck(const char *value);
json_t *json_integer(long value);
json_t *json_integer(json_int_t value);
json_t *json_real(double value);
json_t *json_true(void);
json_t *json_false(void);
@@ -139,13 +147,13 @@ int json_array_insert(json_t *array, size_t index, json_t *value)
}
const char *json_string_value(const json_t *string);
long json_integer_value(const json_t *integer);
json_int_t json_integer_value(const json_t *integer);
double json_real_value(const json_t *real);
double json_number_value(const json_t *json);
int json_string_set(json_t *string, const char *value);
int json_string_set_nocheck(json_t *string, const char *value);
int json_integer_set(json_t *integer, long value);
int json_integer_set(json_t *integer, json_int_t value);
int json_real_set(json_t *real, double value);

View File

@@ -3,6 +3,18 @@
*
* Jansson is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*
*
* This file specifies a part of the site-specific configuration for
* Jansson, namely those things that affect the public API in
* jansson.h.
*
* The configure script copies this file to jansson_config.h and
* replaces @var@ substitutions by values that fit your system. If you
* cannot run the configure script, you can copy the file and do the
* value substitution by hand.
*
* See below for explanations of each substitution variable.
*/
#ifndef JANSSON_CONFIG_H
@@ -11,7 +23,17 @@
#ifdef __cplusplus
#define JSON_INLINE inline
#else
/* If your compiler supports the inline keyword, @json_inline@ is
replaced with `inline', otherwise empty. */
#define JSON_INLINE @json_inline@
#endif
/* If your compiler supports the `long long` type,
@json_have_long_long@ is replaced with 1, otherwise with 0. */
#if @json_have_long_long@
#define JSON_INTEGER_IS_LONG_LONG 1
#else
#define JSON_INTEGER_IS_LONG 1
#endif
#endif

View File

@@ -41,7 +41,7 @@ typedef struct {
typedef struct {
json_t json;
long value;
json_int_t value;
} json_integer_t;
#define json_to_object(json_) container_of(json_, json_object_t, json)

View File

@@ -52,7 +52,7 @@ typedef struct {
int line, column;
union {
char *string;
long integer;
json_int_t integer;
double real;
} value;
} lex_t;
@@ -401,6 +401,12 @@ out:
free(lex->value.string);
}
#ifdef JSON_INTEGER_IS_LONG_LONG
#define json_strtoint strtoll
#else
#define json_strtoint strtol
#endif
static int lex_scan_number(lex_t *lex, char c, json_error_t *error)
{
const char *saved_text;
@@ -430,23 +436,24 @@ static int lex_scan_number(lex_t *lex, char c, json_error_t *error)
}
if(c != '.' && c != 'E' && c != 'e') {
long value;
json_int_t value;
lex_unget_unsave(lex, c);
saved_text = strbuffer_value(&lex->saved_text);
value = strtol(saved_text, &end, 10);
assert(end == saved_text + lex->saved_text.length);
if(value == LONG_MAX && errno == ERANGE) {
error_set(error, lex, "too big integer");
goto out;
}
else if(value == LONG_MIN && errno == ERANGE) {
error_set(error, lex, "too big negative integer");
errno = 0;
value = json_strtoint(saved_text, &end, 10);
if(errno == ERANGE) {
if(value < 0)
error_set(error, lex, "too big negative integer");
else
error_set(error, lex, "too big integer");
goto out;
}
assert(end == saved_text + lex->saved_text.length);
lex->token = TOKEN_INTEGER;
lex->value.integer = value;
return 0;

View File

@@ -725,7 +725,7 @@ static json_t *json_string_copy(json_t *string)
/*** integer ***/
json_t *json_integer(long value)
json_t *json_integer(json_int_t value)
{
json_integer_t *integer = malloc(sizeof(json_integer_t));
if(!integer)
@@ -736,7 +736,7 @@ json_t *json_integer(long value)
return &integer->json;
}
long json_integer_value(const json_t *json)
json_int_t json_integer_value(const json_t *json)
{
if(!json_is_integer(json))
return 0;
@@ -744,7 +744,7 @@ long json_integer_value(const json_t *json)
return json_to_integer(json)->value;
}
int json_integer_set(json_t *json, long value)
int json_integer_set(json_t *json, json_int_t value)
{
if(!json_is_integer(json))
return -1;