Change JSON integer's underlying type from int to long
This is a backwards incompatible change.
This commit is contained in:
parent
68f2861e92
commit
014c49c285
@ -287,18 +287,18 @@ String
|
|||||||
Number
|
Number
|
||||||
======
|
======
|
||||||
|
|
||||||
.. cfunction:: json_t *json_integer(int value)
|
.. cfunction:: json_t *json_integer(long value)
|
||||||
|
|
||||||
.. refcounting:: new
|
.. refcounting:: new
|
||||||
|
|
||||||
Returns a new JSON integer, or *NULL* on error.
|
Returns a new JSON integer, or *NULL* on error.
|
||||||
|
|
||||||
.. cfunction:: int json_integer_value(const json_t *integer)
|
.. cfunction:: long json_integer_value(const json_t *integer)
|
||||||
|
|
||||||
Returns the associated value of *integer*, or 0 if *json* is not a
|
Returns the associated value of *integer*, or 0 if *json* is not a
|
||||||
JSON integer.
|
JSON integer.
|
||||||
|
|
||||||
.. cfunction:: int json_integer_set(const json_t *integer, int value)
|
.. cfunction:: int json_integer_set(const json_t *integer, long value)
|
||||||
|
|
||||||
Sets the associated value of *integer* to *value*. Returns 0 on
|
Sets the associated value of *integer* to *value*. Returns 0 on
|
||||||
success and -1 if *integer* is not a JSON integer.
|
success and -1 if *integer* is not a JSON integer.
|
||||||
|
@ -36,7 +36,7 @@ Real vs. Integer
|
|||||||
|
|
||||||
JSON makes no distinction between real and integer numbers; Jansson
|
JSON makes no distinction between real and integer numbers; Jansson
|
||||||
does. Real numbers are mapped to the ``double`` type and integers to
|
does. Real numbers are mapped to the ``double`` type and integers to
|
||||||
the ``int`` type.
|
the ``long`` type.
|
||||||
|
|
||||||
A JSON number is considered to be a real number if its lexical
|
A JSON number is considered to be a real number if its lexical
|
||||||
representation includes one of ``e``, ``E``, or ``.``; regardless if
|
representation includes one of ``e``, ``E``, or ``.``; regardless if
|
||||||
@ -64,7 +64,7 @@ error). Thus, depending on platform, JSON numbers like 1E+999 or
|
|||||||
-1E+999 may result in a parsing error.
|
-1E+999 may result in a parsing error.
|
||||||
|
|
||||||
Likewise, integer numbers whose absolute values are too large to be
|
Likewise, integer numbers whose absolute values are too large to be
|
||||||
represented in the ``int`` type will result in an overflow error (a
|
represented in the ``long`` type will result in an overflow error (a
|
||||||
JSON decoding error). Thus, depending on platform, JSON numbers like
|
JSON decoding error). Thus, depending on platform, JSON numbers like
|
||||||
1000000000000000 may result in parsing error.
|
1000000000000000 may result in parsing error.
|
||||||
|
|
||||||
@ -94,9 +94,9 @@ Types
|
|||||||
-----
|
-----
|
||||||
|
|
||||||
No support is provided in Jansson for any C numeric types other than
|
No support is provided in Jansson for any C numeric types other than
|
||||||
``int`` and ``double``. This excludes things such as unsigned types,
|
``long`` and ``double``. This excludes things such as unsigned types,
|
||||||
``long``, ``long long``, ``long double``, etc. Obviously, shorter
|
``long long``, ``long double``, etc. Obviously, shorter types like
|
||||||
types like ``short`` and ``float`` are implicitly handled via the
|
``short``, ``int`` and ``float`` are implicitly handled via the
|
||||||
ordinary C type coercion rules (subject to overflow semantics). Also,
|
ordinary C type coercion rules (subject to overflow semantics). Also,
|
||||||
no support or hooks are provided for any supplemental "bignum" type
|
no support or hooks are provided for any supplemental "bignum" type
|
||||||
add-on packages.
|
add-on packages.
|
||||||
|
@ -185,7 +185,7 @@ static int do_dump(const json_t *json, size_t flags, int depth,
|
|||||||
char buffer[MAX_INTEGER_STR_LENGTH];
|
char buffer[MAX_INTEGER_STR_LENGTH];
|
||||||
int size;
|
int size;
|
||||||
|
|
||||||
size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%d", json_integer_value(json));
|
size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%li", json_integer_value(json));
|
||||||
if(size >= MAX_INTEGER_STR_LENGTH)
|
if(size >= MAX_INTEGER_STR_LENGTH)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ json_t *json_object(void);
|
|||||||
json_t *json_array(void);
|
json_t *json_array(void);
|
||||||
json_t *json_string(const char *value);
|
json_t *json_string(const char *value);
|
||||||
json_t *json_string_nocheck(const char *value);
|
json_t *json_string_nocheck(const char *value);
|
||||||
json_t *json_integer(int value);
|
json_t *json_integer(long value);
|
||||||
json_t *json_real(double value);
|
json_t *json_real(double value);
|
||||||
json_t *json_true(void);
|
json_t *json_true(void);
|
||||||
json_t *json_false(void);
|
json_t *json_false(void);
|
||||||
@ -141,13 +141,13 @@ int json_array_insert(json_t *array, size_t index, json_t *value)
|
|||||||
}
|
}
|
||||||
|
|
||||||
const char *json_string_value(const json_t *string);
|
const char *json_string_value(const json_t *string);
|
||||||
int json_integer_value(const json_t *integer);
|
long json_integer_value(const json_t *integer);
|
||||||
double json_real_value(const json_t *real);
|
double json_real_value(const json_t *real);
|
||||||
double json_number_value(const json_t *json);
|
double json_number_value(const json_t *json);
|
||||||
|
|
||||||
int json_string_set(json_t *string, const char *value);
|
int json_string_set(json_t *string, const char *value);
|
||||||
int json_string_set_nocheck(json_t *string, const char *value);
|
int json_string_set_nocheck(json_t *string, const char *value);
|
||||||
int json_integer_set(json_t *integer, int value);
|
int json_integer_set(json_t *integer, long value);
|
||||||
int json_real_set(json_t *real, double value);
|
int json_real_set(json_t *real, double value);
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ typedef struct {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
json_t json;
|
json_t json;
|
||||||
int value;
|
long value;
|
||||||
} json_integer_t;
|
} json_integer_t;
|
||||||
|
|
||||||
#define json_to_object(json_) container_of(json_, json_object_t, json)
|
#define json_to_object(json_) container_of(json_, json_object_t, json)
|
||||||
|
@ -52,7 +52,7 @@ typedef struct {
|
|||||||
int line, column;
|
int line, column;
|
||||||
union {
|
union {
|
||||||
char *string;
|
char *string;
|
||||||
int integer;
|
long integer;
|
||||||
double real;
|
double real;
|
||||||
} value;
|
} value;
|
||||||
} lex_t;
|
} lex_t;
|
||||||
@ -438,17 +438,17 @@ static int lex_scan_number(lex_t *lex, char c, json_error_t *error)
|
|||||||
value = strtol(saved_text, &end, 10);
|
value = strtol(saved_text, &end, 10);
|
||||||
assert(end == saved_text + lex->saved_text.length);
|
assert(end == saved_text + lex->saved_text.length);
|
||||||
|
|
||||||
if((value == LONG_MAX && errno == ERANGE) || value > INT_MAX) {
|
if(value == LONG_MAX && errno == ERANGE) {
|
||||||
error_set(error, lex, "too big integer");
|
error_set(error, lex, "too big integer");
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
else if((value == LONG_MIN && errno == ERANGE) || value < INT_MIN) {
|
else if(value == LONG_MIN && errno == ERANGE) {
|
||||||
error_set(error, lex, "too big negative integer");
|
error_set(error, lex, "too big negative integer");
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
lex->token = TOKEN_INTEGER;
|
lex->token = TOKEN_INTEGER;
|
||||||
lex->value.integer = (int)value;
|
lex->value.integer = value;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -725,7 +725,7 @@ static json_t *json_string_copy(json_t *string)
|
|||||||
|
|
||||||
/*** integer ***/
|
/*** integer ***/
|
||||||
|
|
||||||
json_t *json_integer(int value)
|
json_t *json_integer(long value)
|
||||||
{
|
{
|
||||||
json_integer_t *integer = malloc(sizeof(json_integer_t));
|
json_integer_t *integer = malloc(sizeof(json_integer_t));
|
||||||
if(!integer)
|
if(!integer)
|
||||||
@ -736,7 +736,7 @@ json_t *json_integer(int value)
|
|||||||
return &integer->json;
|
return &integer->json;
|
||||||
}
|
}
|
||||||
|
|
||||||
int json_integer_value(const json_t *json)
|
long json_integer_value(const json_t *json)
|
||||||
{
|
{
|
||||||
if(!json_is_integer(json))
|
if(!json_is_integer(json))
|
||||||
return 0;
|
return 0;
|
||||||
@ -744,7 +744,7 @@ int json_integer_value(const json_t *json)
|
|||||||
return json_to_integer(json)->value;
|
return json_to_integer(json)->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
int json_integer_set(json_t *json, int value)
|
int json_integer_set(json_t *json, long value)
|
||||||
{
|
{
|
||||||
if(!json_is_integer(json))
|
if(!json_is_integer(json))
|
||||||
return -1;
|
return -1;
|
||||||
|
Loading…
Reference in New Issue
Block a user