* Add int64 support. Two new functions json_object_net_int64 and
json_object_get_int64. Binary compatibility preserved.
Eric Haszlakiewicz, EHASZLA at transunion com
Rui Miguel Silva Seabra, rms at 1407 dot org
git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@56 327403b1-1117-474d-bef2-5cb71233fd97
diff --git a/json_tokener.c b/json_tokener.c
index 8d0b5dc..dbaacaa 100644
--- a/json_tokener.c
+++ b/json_tokener.c
@@ -20,14 +20,16 @@
#include <stddef.h>
#include <ctype.h>
#include <string.h>
+#include <limits.h>
#include "bits.h"
#include "debug.h"
#include "printbuf.h"
#include "arraylist.h"
+#include "json_inttypes.h"
#include "json_object.h"
#include "json_tokener.h"
-
+#include "json_util.h"
#if !HAVE_STRNCASECMP && defined(_MSC_VER)
/* MSC has the version as _strnicmp */
@@ -542,11 +544,21 @@
printbuf_memappend_fast(tok->pb, case_start, case_len);
}
{
- int numi;
- double numd;
- if(!tok->is_double && sscanf(tok->pb->buf, "%d", &numi) == 1) {
- current = json_object_new_int(numi);
- } else if(tok->is_double && sscanf(tok->pb->buf, "%lf", &numd) == 1) {
+ int64_t num64;
+ double numd;
+ if (!tok->is_double && json_parse_int64(tok->pb->buf, &num64) == 0) {
+ // Decode the type based on the value range to keep compatibilty
+ // with code that checks the type of objects. i.e. this:
+ // json_object_get_type(o) == json_type_int
+ // will continue to work.
+ // The other option would be to eliminate any distinction between
+ // int and int64 types, but that would change the ABI of
+ // json_object_get_int().
+ if (num64 < INT32_MAX && num64 > INT32_MIN)
+ current = json_object_new_int(num64);
+ else
+ current = json_object_new_int64(num64);
+ } else if(tok->is_double && sscanf(tok->pb->buf, "%lf", &numd) == 1) {
current = json_object_new_double(numd);
} else {
tok->err = json_tokener_error_parse_number;