Renamed flag to JSON_DECODE_INT_AS_REAL and added documentation

This commit is contained in:
Jason Choy 2013-06-11 11:53:35 +01:00
parent e6bd0aba9d
commit 02a3829363
5 changed files with 18 additions and 7 deletions

View file

@ -950,6 +950,14 @@ macros can be ORed together to obtain *flags*.
.. versionadded:: 2.1
``JSON_DECODE_INT_AS_REAL``
JSON defines only one number type. Jansson distinguishes between
ints and reals. For more information see :ref:`real-vs-integer`.
With this flag enabled the decoder interprets all numbers as real
values.
.. versionadded:: 2.4.1
Each function also takes an optional :type:`json_error_t` parameter
that is filled with error information if decoding fails. It's also
updated on success; the number of bytes of input read is written to

View file

@ -38,6 +38,8 @@ strings.
Numbers
=======
.. _real-vs-integer:
Real vs. Integer
----------------
@ -51,7 +53,8 @@ A JSON number is considered to be a real number if its lexical
representation includes one of ``e``, ``E``, or ``.``; regardless if
its actual numeric value is a true integer (e.g., all of ``1E6``,
``3.0``, ``400E-2``, and ``3.14E3`` are mathematical integers, but
will be treated as real values).
will be treated as real values). With the ``JSON_DECODE_INT_AS_REAL``
decoder flag set all numbers are interpreted as real.
All other JSON numbers are considered integers.

View file

@ -236,10 +236,10 @@ json_t *json_deep_copy(json_t *value);
/* decoding */
#define JSON_REJECT_DUPLICATES 0x1
#define JSON_DISABLE_EOF_CHECK 0x2
#define JSON_DECODE_ANY 0x4
#define JSON_DECODE_NO_INT 0x8
#define JSON_REJECT_DUPLICATES 0x1
#define JSON_DISABLE_EOF_CHECK 0x2
#define JSON_DECODE_ANY 0x4
#define JSON_DECODE_INT_AS_REAL 0x8
typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data);

View file

@ -783,7 +783,7 @@ static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error)
}
case TOKEN_INTEGER: {
if (flags & JSON_DECODE_NO_INT) {
if (flags & JSON_DECODE_INT_AS_REAL) {
json = json_real(lex->value.integer);
} else {
json = json_integer(lex->value.integer);

View file

@ -92,7 +92,7 @@ static void decode_no_int()
json_t *json;
json_error_t error;
json = json_loads("42", JSON_DECODE_NO_INT | JSON_DECODE_ANY, &error);
json = json_loads("42", JSON_DECODE_INT_AS_REAL | JSON_DECODE_ANY, &error);
if (!json || !json_is_real(json) || json_real_value(json) != 42.0)
fail("json_load decode no int failed - int");
json_decref(json);