if (MSVC)
check_symbol_exists(strtoll "stdlib.h" HAVE_STRTOLL)
+ check_symbol_exists(strtoull "stdlib.h" HAVE_STRTOULL)
set(json_c_strtoll "strtoll")
if (NOT HAVE_STRTOLL)
# could do the same for strtoull, if needed
endif()
endif()
+
+ set(json_c_strtoull "strtoull")
+ if (NOT HAVE_STRTOULL)
+ # Use _strtoui64 if strtoull is not available.
+ check_symbol_exists(_strtoui64 "stdlib.h" __have_strtoui64)
+ if (__have_strtoui64)
+ set(HAVE_STRTOULL 1)
+ set(json_c_strtoull "_strtoui64")
+ endif()
+ endif()
endif()
#cmakedefine HAVE_STRTOLL
#cmakedefine strtoll @json_c_strtoll@
+#cmakedefine HAVE_STRTOULL
+#cmakedefine strtoull @json_c_strtoull@
/* Have __thread */
#cmakedefine HAVE___THREAD
\r
#cmakedefine HAVE_STRTOLL\r
#cmakedefine strtoll @cmake_strtoll@\r
+#cmakedefine HAVE_STRTOULL\r
+#cmakedefine strtoull @cmake_strtoull@\r
\r
/* Define to 1 if you have the <syslog.h> header file. */\r
#undef HAVE_SYSLOG_H\r
AX_COMPILE_CHECK_SIZEOF(long long)
AX_COMPILE_CHECK_SIZEOF(size_t, [#include <stdint.h>])
AX_COMPILE_CHECK_SIZEOF(int64_t, [#include <stdint.h>])
+AX_COMPILE_CHECK_SIZEOF(uint64_t, [#include <stdint.h>])
AC_CONFIG_FILES([
Makefile
#define PRId64 "I64d"
#define SCNd64 "I64d"
+#define PRIu64 "I64u"
#endif
static json_object_to_json_string_fn json_object_boolean_to_json_string;
static json_object_to_json_string_fn json_object_double_to_json_string_default;
static json_object_to_json_string_fn json_object_int_to_json_string;
+static json_object_to_json_string_fn json_object_uint_to_json_string;
static json_object_to_json_string_fn json_object_string_to_json_string;
static json_object_to_json_string_fn json_object_array_to_json_string;
static json_object_to_json_string_fn _json_object_userdata_to_json_string;
case json_type_int:
jso->_to_json_string = &json_object_int_to_json_string;
break;
+ case json_type_uint:
+ jso->_to_json_string = &json_object_uint_to_json_string;
+ break;
case json_type_object:
jso->_to_json_string = &json_object_object_to_json_string;
break;
return jso->o.c_boolean;
case json_type_int:
return (jso->o.c_int64 != 0);
+ case json_type_uint:
+ return (jso->o.c_uint64 != 0);
case json_type_double:
return (jso->o.c_double != 0);
case json_type_string:
return printbuf_memappend (pb, sbuf, strlen(sbuf));
}
+static int json_object_uint_to_json_string(struct json_object* jso,
+ struct printbuf *pb,
+ int level,
+ int flags)
+{
+ /* room for 20 digits, and a null term */
+ char sbuf[21];
+ snprintf(sbuf, sizeof(sbuf), "%" PRIu64, jso->o.c_uint64);
+ return printbuf_memappend (pb, sbuf, strlen(sbuf));
+}
+
struct json_object* json_object_new_int(int32_t i)
{
struct json_object *jso = json_object_new(json_type_int);
if (cint64 >= INT32_MAX)
return INT32_MAX;
return (int32_t) cint64;
+ case json_type_uint:
+ if (jso->o.c_uint64 >= INT32_MAX)
+ return INT32_MAX;
+ return (int32_t)jso->o.c_uint64;
case json_type_double:
if (jso->o.c_double <= INT32_MIN)
return INT32_MIN;
return jso;
}
+struct json_object* json_object_new_uint64(uint64_t i)
+{
+ struct json_object *jso = json_object_new(json_type_uint);
+ if (!jso)
+ return NULL;
+ jso->_to_json_string = &json_object_uint_to_json_string;
+ jso->o.c_uint64 = i;
+ return jso;
+}
+
int64_t json_object_get_int64(const struct json_object *jso)
{
int64_t cint;
{
case json_type_int:
return jso->o.c_int64;
+ case json_type_uint:
+ if (jso->o.c_uint64 >= INT64_MAX)
+ return INT64_MAX;
+ return (int64_t)jso->o.c_uint64;
case json_type_double:
// INT64_MAX can't be exactly represented as a double
// so cast to tell the compiler it's ok to round up.
}
}
+uint64_t json_object_get_uint64(const struct json_object *jso)
+{
+ uint64_t cuint;
+
+ if (!jso)
+ return 0;
+ switch(jso->o_type)
+ {
+ case json_type_int:
+ if (jso->o.c_int64 < 0)
+ return 0;
+ return (uint64_t)jso->o.c_int64;
+ case json_type_uint:
+ return jso->o.c_uint64;
+ case json_type_double:
+ // UINT64_MAX can't be exactly represented as a double
+ // so cast to tell the compiler it's ok to round up.
+ if (jso->o.c_double >= (double)UINT64_MAX)
+ return UINT64_MAX;
+ if (jso->o.c_double < 0)
+ return 0;
+ return (uint64_t)jso->o.c_double;
+ case json_type_boolean:
+ return jso->o.c_boolean;
+ case json_type_string:
+ if (json_parse_uint64(get_string_component(jso), &cuint) == 0)
+ return cuint;
+ /* FALLTHRU */
+ default:
+ return 0;
+ }
+}
+
int json_object_set_int64(struct json_object *jso,int64_t new_value){
if (!jso || jso->o_type!=json_type_int)
return 0;
return 1;
}
+int json_object_set_uint64(struct json_object *jso,uint64_t new_value){
+ if (!jso || jso->o_type!=json_type_uint)
+ return 0;
+ jso->o.c_uint64=new_value;
+ return 1;
+}
+
int json_object_int_inc(struct json_object *jso, int64_t val) {
if (!jso || jso->o_type != json_type_int)
return 0;
return 1;
}
+int json_object_uint_inc(struct json_object *jso, uint64_t val) {
+ if (!jso || jso->o_type != json_type_uint)
+ return 0;
+ if (jso->o.c_uint64 > UINT64_MAX - val) {
+ jso->o.c_uint64 = UINT64_MAX;
+ } else {
+ jso->o.c_uint64 += val;
+ }
+ return 1;
+}
+
/* json_object_double */
#if defined(HAVE___THREAD)
return jso->o.c_double;
case json_type_int:
return jso->o.c_int64;
+ case json_type_uint:
+ return jso->o.c_uint64;
case json_type_boolean:
return jso->o.c_boolean;
case json_type_string:
case json_type_int:
return (jso1->o.c_int64 == jso2->o.c_int64);
+ case json_type_uint:
+ return (jso1->o.c_uint64 == jso2->o.c_uint64);
+
case json_type_string:
return (jso1->o.c_string.len == jso2->o.c_string.len &&
memcmp(get_string_component(jso1),
*dst = json_object_new_int64(src->o.c_int64);
break;
+ case json_type_uint:
+ *dst = json_object_new_uint64(src->o.c_uint64);
+ break;
+
case json_type_string:
*dst = json_object_new_string(get_string_component(src));
break;
json_type_boolean,
json_type_double,
json_type_int,
+ json_type_uint,
json_type_object,
json_type_array,
json_type_string
json_type_boolean,
json_type_double,
json_type_int,
+ json_type_uint,
json_type_object,
json_type_array,
json_type_string
json_type_boolean,
json_type_double,
json_type_int,
+ json_type_uint,
json_type_object,
json_type_array,
json_type_string
JSON_EXPORT struct json_object* json_object_new_int64(int64_t i);
+/** Create a new empty json_object of type json_type_uint
+ * @param i the integer
+ * @returns a json_object of type json_type_uint
+ */
+JSON_EXPORT struct json_object* json_object_new_uint64(uint64_t i);
+
+
/** Get the int value of a json_object
*
* The type is coerced to a int if the passed object is not a int.
*/
JSON_EXPORT int json_object_int_inc(struct json_object *obj, int64_t val);
+/** Increment a json_type_uint object by the given amount, which may be negative.
+ *
+ * If the type of obj is not json_type_uint then 0 is returned with no further
+ * action taken.
+ * If the addition would result in a overflow, the object value
+ * is set to UINT64_MAX.
+ * Neither overflow nor underflow affect the return value.
+ *
+ * @param obj the json_object instance
+ * @param val the value to add
+ * @returns 1 if the increment succeded, 0 otherwise
+ */
+JSON_EXPORT int json_object_uint_inc(struct json_object *obj, uint64_t val);
/** Get the int value of a json_object
*
*/
JSON_EXPORT int64_t json_object_get_int64(const struct json_object *obj);
+/** Get the uint value of a json_object
+ *
+ * The type is coerced to a uint64 if the passed object is not a uint64.
+ * double objects will return their uint64 conversion. Strings will be
+ * parsed as an uint64. If no conversion exists then 0 is returned.
+ *
+ * NOTE: Set errno to 0 directly before a call to this function to determine
+ * whether or not conversion was successful (it does not clear the value for
+ * you).
+ *
+ * @param obj the json_object instance
+ * @returns an uint64
+ */
+JSON_EXPORT uint64_t json_object_get_uint64(const struct json_object *obj);
/** Set the int64_t value of a json_object
*
*/
JSON_EXPORT int json_object_set_int64(struct json_object *obj,int64_t new_value);
+/** Set the uint64_t value of a json_object
+ *
+ * The type of obj is checked to be a json_type_uint and 0 is returned
+ * if it is not without any further actions. If type of obj is json_type_uint
+ * the object value is changed to new_value
+ *
+ * @param obj the json_object instance
+ * @param new_value the value to be set
+ * @returns 1 if value is set correctly, 0 otherwise
+ */
+JSON_EXPORT int json_object_set_uint64(struct json_object *obj,uint64_t new_value);
+
/* double type methods */
/** Create a new empty json_object of type json_type_double
json_bool c_boolean;
double c_double;
int64_t c_int64;
+ uint64_t c_uint64;
struct lh_table *c_object;
struct array_list *c_array;
struct {
}
{
int64_t num64;
+ uint64_t numuint64;
double numd;
- if (!tok->is_double && json_parse_int64(tok->pb->buf, &num64) == 0) {
- if (num64 && tok->pb->buf[0]=='0' &&
+ if (!tok->is_double && tok->pb->buf[0] == '-'
+ && json_parse_int64(tok->pb->buf, &num64) == 0) {
+ current = json_object_new_int64(num64);
+ if(current == NULL)
+ goto out;
+ } else if ( !tok->is_double && tok->pb->buf[0] != '-'
+ && json_parse_uint64(tok->pb->buf, &numuint64) == 0) {
+ if (numuint64 && tok->pb->buf[0]=='0' &&
(tok->flags & JSON_TOKENER_STRICT)) {
- /* in strict mode, number must not start with 0 */
tok->err = json_tokener_error_parse_number;
goto out;
}
- current = json_object_new_int64(num64);
- if(current == NULL)
- goto out;
- }
- else if(tok->is_double && json_parse_double(tok->pb->buf, &numd) == 0)
+ if (numuint64 <= INT64_MAX){
+ num64 = (uint64_t) numuint64;
+ current = json_object_new_int64(num64);
+ if(current == NULL)
+ goto out;
+ } else {
+ current = json_object_new_uint64(numuint64);
+ if(current == NULL)
+ goto out;
+ }
+ } else if(tok->is_double && json_parse_double(tok->pb->buf, &numd) == 0)
{
- current = json_object_new_double_s(numd, tok->pb->buf);
+ current = json_object_new_double_s(numd, tok->pb->buf);
if(current == NULL)
goto out;
} else {
#ifdef WIN32
# if MSC_VER < 1800
-/* strtoll is available only since Visual Studio 2013 */
+/* strtoll/strtoull is available only since Visual Studio 2013 */
# define strtoll _strtoi64
+# define strtoull _strtoui64
# endif
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
}
+int json_parse_uint64(const char *buf, uint64_t *retval)
+{
+ char *end = NULL;
+ uint64_t val;
+ errno = 1;
+
+ while (*buf == ' ') {
+ buf++;
+ }
+ if (*buf == '-') errno = 0;
+
+ val = strtoull(buf, &end, 10);
+ if (end != buf)
+ *retval = val;
+ return ((errno == 0) || (end == buf)) ? 1 : 0;
+}
+
#ifndef HAVE_REALLOC
void* rpl_realloc(void* p, size_t n)
{
"boolean",
"double",
"int",
+ "uint",
"object",
"array",
"string",
JSON_EXPORT int json_parse_int64(const char *buf, int64_t *retval);
+JSON_EXPORT int json_parse_uint64(const char *buf, uint64_t *retval);
JSON_EXPORT int json_parse_double(const char *buf, double *retval);
/**
case json_type_boolean:
case json_type_double:
case json_type_int:
+ case json_type_uint:
case json_type_string:
// we already called userfunc above, move on to the next object
return JSON_C_VISIT_RETURN_CONTINUE;
\"decimal_number\": 99.55,\n\
\"boolean_true\": true,\n\
\"boolean_false\": false,\n\
- \"big_number\": 2147483649,\n\
+ \"int64_number\": 2147483649,\n\
+ \"negative_number\": -321321321,\n\
\"a_null\": null,\n\
}";
/* Note: 2147483649 = INT_MAX + 2 */
+ /* Note: 9223372036854775809 = INT64_MAX + 2 */
+ /* Note: 18446744073709551617 = UINT64_MAX + 2 */
struct json_object *new_obj;
getit(new_obj, "decimal_number");
getit(new_obj, "boolean_true");
getit(new_obj, "boolean_false");
- getit(new_obj, "big_number");
+ getit(new_obj, "int64_number");
+ getit(new_obj, "negative_number");
getit(new_obj, "a_null");
// Now check the behaviour of the json_object_is_type() function.
checktype(new_obj, "decimal_number");
checktype(new_obj, "boolean_true");
checktype(new_obj, "boolean_false");
- checktype(new_obj, "big_number");
+ checktype(new_obj, "int64_number");
+ checktype(new_obj, "negative_number");
checktype(new_obj, "a_null");
json_object_put(new_obj);
json_object_get_int(o));
printf("new_obj.%s json_object_get_int64()=%" PRId64 "\n", field,
json_object_get_int64(o));
+ printf("new_obj.%s json_object_get_uint64()=%" PRIu64 "\n", field,
+ json_object_get_uint64(o));
printf("new_obj.%s json_object_get_boolean()=%d\n", field,
json_object_get_boolean(o));
printf("new_obj.%s json_object_get_double()=%f\n", field,
static void checktype_header()
{
- printf("json_object_is_type: %s,%s,%s,%s,%s,%s,%s\n",
+ printf("json_object_is_type: %s,%s,%s,%s,%s,%s,%s,%s\n",
json_type_to_name(json_type_null),
json_type_to_name(json_type_boolean),
json_type_to_name(json_type_double),
json_type_to_name(json_type_int),
+ json_type_to_name(json_type_uint),
json_type_to_name(json_type_object),
json_type_to_name(json_type_array),
json_type_to_name(json_type_string));
if (field && !json_object_object_get_ex(new_obj, field, &o))
printf("Field %s does not exist\n", field);
- printf("new_obj%s%-18s: %d,%d,%d,%d,%d,%d,%d\n",
+ printf("new_obj%s%-18s: %d,%d,%d,%d,%d,%d,%d,%d\n",
field ? "." : " ", field ? field : "",
json_object_is_type(o, json_type_null),
json_object_is_type(o, json_type_boolean),
json_object_is_type(o, json_type_double),
json_object_is_type(o, json_type_int),
+ json_object_is_type(o, json_type_uint),
json_object_is_type(o, json_type_object),
json_object_is_type(o, json_type_array),
json_object_is_type(o, json_type_string));
"decimal_number": 99.55,
"boolean_true": true,
"boolean_false": false,
- "big_number": 2147483649,
+ "int64_number": 2147483649,
+ "negative_number": -321321321,
"a_null": null,
}
Result is not NULL
new_obj.string_of_digits json_object_get_type()=string
new_obj.string_of_digits json_object_get_int()=123
new_obj.string_of_digits json_object_get_int64()=123
+new_obj.string_of_digits json_object_get_uint64()=123
new_obj.string_of_digits json_object_get_boolean()=1
new_obj.string_of_digits json_object_get_double()=123.000000
new_obj.regular_number json_object_get_type()=int
new_obj.regular_number json_object_get_int()=222
new_obj.regular_number json_object_get_int64()=222
+new_obj.regular_number json_object_get_uint64()=222
new_obj.regular_number json_object_get_boolean()=1
new_obj.regular_number json_object_get_double()=222.000000
new_obj.decimal_number json_object_get_type()=double
new_obj.decimal_number json_object_get_int()=99
new_obj.decimal_number json_object_get_int64()=99
+new_obj.decimal_number json_object_get_uint64()=99
new_obj.decimal_number json_object_get_boolean()=1
new_obj.decimal_number json_object_get_double()=99.550000
new_obj.boolean_true json_object_get_type()=boolean
new_obj.boolean_true json_object_get_int()=1
new_obj.boolean_true json_object_get_int64()=1
+new_obj.boolean_true json_object_get_uint64()=1
new_obj.boolean_true json_object_get_boolean()=1
new_obj.boolean_true json_object_get_double()=1.000000
new_obj.boolean_false json_object_get_type()=boolean
new_obj.boolean_false json_object_get_int()=0
new_obj.boolean_false json_object_get_int64()=0
+new_obj.boolean_false json_object_get_uint64()=0
new_obj.boolean_false json_object_get_boolean()=0
new_obj.boolean_false json_object_get_double()=0.000000
-new_obj.big_number json_object_get_type()=int
-new_obj.big_number json_object_get_int()=2147483647
-new_obj.big_number json_object_get_int64()=2147483649
-new_obj.big_number json_object_get_boolean()=1
-new_obj.big_number json_object_get_double()=2147483649.000000
+new_obj.int64_number json_object_get_type()=int
+new_obj.int64_number json_object_get_int()=2147483647
+new_obj.int64_number json_object_get_int64()=2147483649
+new_obj.int64_number json_object_get_uint64()=2147483649
+new_obj.int64_number json_object_get_boolean()=1
+new_obj.int64_number json_object_get_double()=2147483649.000000
+new_obj.negative_number json_object_get_type()=int
+new_obj.negative_number json_object_get_int()=-321321321
+new_obj.negative_number json_object_get_int64()=-321321321
+new_obj.negative_number json_object_get_uint64()=0
+new_obj.negative_number json_object_get_boolean()=1
+new_obj.negative_number json_object_get_double()=-321321321.000000
new_obj.a_null json_object_get_type()=null
new_obj.a_null json_object_get_int()=0
new_obj.a_null json_object_get_int64()=0
+new_obj.a_null json_object_get_uint64()=0
new_obj.a_null json_object_get_boolean()=0
new_obj.a_null json_object_get_double()=0.000000
================================
-json_object_is_type: null,boolean,double,int,object,array,string
-new_obj : 0,0,0,0,1,0,0
-new_obj.string_of_digits : 0,0,0,0,0,0,1
-new_obj.regular_number : 0,0,0,1,0,0,0
-new_obj.decimal_number : 0,0,1,0,0,0,0
-new_obj.boolean_true : 0,1,0,0,0,0,0
-new_obj.boolean_false : 0,1,0,0,0,0,0
-new_obj.big_number : 0,0,0,1,0,0,0
-new_obj.a_null : 1,0,0,0,0,0,0
+json_object_is_type: null,boolean,double,int,uint,object,array,string
+new_obj : 0,0,0,0,0,1,0,0
+new_obj.string_of_digits : 0,0,0,0,0,0,0,1
+new_obj.regular_number : 0,0,0,1,0,0,0,0
+new_obj.decimal_number : 0,0,1,0,0,0,0,0
+new_obj.boolean_true : 0,1,0,0,0,0,0,0
+new_obj.boolean_false : 0,1,0,0,0,0,0,0
+new_obj.int64_number : 0,0,0,1,0,0,0,0
+new_obj.negative_number : 0,0,0,1,0,0,0,0
+new_obj.a_null : 1,0,0,0,0,0,0,0
struct json_object *int1 = json_object_new_int(0);
struct json_object *int2 = json_object_new_int(1);
struct json_object *int3 = json_object_new_int(1);
+ struct json_object *uint1 = json_object_new_uint64(0);
+ struct json_object *uint2 = json_object_new_uint64(1);
+ struct json_object *uint3 = json_object_new_uint64(1);
if (!json_object_equal(int1, int2))
printf("JSON integer comparison is correct\n");
else
printf("JSON same integer comparison failed\n");
+ if (!json_object_equal(uint1, uint2))
+ printf("JSON usigned integer comparison is correct\n");
+ else
+ printf("JSON usigned integer comparison failed\n");
+
+ if (json_object_equal(uint1, uint1))
+ printf("JSON same usigned object comparison is correct\n");
+ else
+ printf("JSON same usigned object comparison failed\n");
+
+ if (json_object_equal(uint2, uint3))
+ printf("JSON same usigned integer comparison is correct\n");
+ else
+ printf("JSON same usigned integer comparison failed\n");
+
json_object_put(int1);
json_object_put(int2);
json_object_put(int3);
+ json_object_put(uint1);
+ json_object_put(uint2);
+ json_object_put(uint3);
/* string tests */
struct json_object *str1 = json_object_new_string("TESTSTRING");
JSON integer comparison is correct
JSON same object comparison is correct
JSON same integer comparison is correct
+JSON usigned integer comparison is correct
+JSON same usigned object comparison is correct
+JSON same usigned integer comparison is correct
Comparing equal strings is correct
Comparing different strings is correct
Comparing equal doubles is correct
assert(json_object_get_int64(tmp) != INT64_MIN);
json_object_put(tmp);
printf("INT64 ADD UNDERFLOW PASSED\n");
+ tmp = json_object_new_uint64(321321321);
+ json_object_uint_inc(tmp, 321321321);
+ assert(json_object_get_uint64(tmp) == 642642642);
+ json_object_put(tmp);
+ printf("UINT64 ADD PASSED\n");
+ tmp = json_object_new_uint64(UINT64_MAX);
+ json_object_uint_inc(tmp, 100);
+ assert(json_object_get_uint64(tmp) == UINT64_MAX);
+ json_object_uint_inc(tmp, -100);
+ assert(json_object_get_uint64(tmp) != INT64_MAX);
+ json_object_put(tmp);
+ printf("UINT64 ADD OVERFLOW PASSED\n");
printf("PASSED\n");
return 0;
INT64 ADD PASSED
INT64 ADD OVERFLOW PASSED
INT64 ADD UNDERFLOW PASSED
+UINT64 ADD PASSED
+UINT64 ADD OVERFLOW PASSED
PASSED
single_basic_parse("[0e]", 1);
single_basic_parse("[0e+]", 1);
single_basic_parse("[0e+-1]", 1);
- single_basic_parse("[18446744073709551616]", 1);
single_basic_parse("\"hello world!\"", 1);
+
+ // uint64/int64 range test
+ single_basic_parse("[9223372036854775806]", 1);
+ single_basic_parse("[9223372036854775807]", 1);
+ single_basic_parse("[9223372036854775808]", 1);
+ single_basic_parse("[-9223372036854775807]", 1);
+ single_basic_parse("[-9223372036854775808]", 1);
+ single_basic_parse("[-9223372036854775809]", 1);
+ single_basic_parse("[18446744073709551614]", 1);
+ single_basic_parse("[18446744073709551615]", 1);
+ single_basic_parse("[18446744073709551616]", 1);
}
static void test_utf8_parse()
new_obj.to_string([0e])=[ 0.0 ]
new_obj.to_string([0e+])=[ 0.0 ]
new_obj.to_string([0e+-1])=null
-new_obj.to_string([18446744073709551616])=[ 9223372036854775807 ]
new_obj.to_string("hello world!")="hello world!"
+new_obj.to_string([9223372036854775806])=[ 9223372036854775806 ]
+new_obj.to_string([9223372036854775807])=[ 9223372036854775807 ]
+new_obj.to_string([9223372036854775808])=[ 9223372036854775808 ]
+new_obj.to_string([-9223372036854775807])=[ -9223372036854775807 ]
+new_obj.to_string([-9223372036854775808])=[ -9223372036854775808 ]
+new_obj.to_string([-9223372036854775809])=[ -9223372036854775808 ]
+new_obj.to_string([18446744073709551614])=[ 18446744073709551614 ]
+new_obj.to_string([18446744073709551615])=[ 18446744073709551615 ]
+new_obj.to_string([18446744073709551616])=[ 18446744073709551615 ]
==================================
new_obj.to_string()=null
new_obj.to_string({})=null
printf("buf=%s parseit=%d, value=%" PRId64 " \n", buf, retval, cint64);
}
+void checkit_uint(const char *buf)
+{
+ uint64_t cuint64 = 666;
+
+ int retval = json_parse_uint64(buf, &cuint64);
+ printf("buf=%s parseit=%d, value=%" PRIu64 " \n", buf, retval, cuint64);
+}
+
/**
- * This test calls json_parse_int64 with a variety of different strings.
- * It's purpose is to ensure that the results are consistent across all
- * different environments that it might be executed in.
+ * This test calls json_parse_int64 and json_parse_int64 with a variety
+ * of different strings. It's purpose is to ensure that the results are
+ * consistent across all different environments that it might be executed in.
*
* This always exits with a 0 exit value. The output should be compared
* against previously saved expected output.
{
char buf[100];
+ printf("==========json_parse_int64() test===========\n");
checkit("x");
checkit("0");
checkit(buf);
strcpy(buf, "4294967295"); // aka UINT32_MAX
+ checkit(buf);
- sprintf(buf, "4294967296"); // aka UINT32_MAX + 1
+ strcpy(buf, "4294967296"); // aka UINT32_MAX + 1
+ checkit(buf);
strcpy(buf, "21474836470"); // INT32_MAX * 10
checkit(buf);
strcpy(buf, "123");
checkit(buf);
+ printf("\n==========json_parse_uint64() test===========\n");
+ checkit_uint("x");
+
+ checkit_uint("0");
+ checkit_uint("-0");
+
+ checkit_uint("00000000");
+ checkit_uint("-00000000");
+
+ checkit_uint("1");
+
+ strcpy(buf, "2147483647"); // aka INT32_MAX
+ checkit_uint(buf);
+
+ strcpy(buf, "-1");
+ checkit_uint(buf);
+
+ strcpy(buf, "-9223372036854775808");
+ checkit_uint(buf);
+
+ strcpy(buf, " 1");
+ checkit_uint(buf);
+
+ strcpy(buf, "00001234");
+ checkit_uint(buf);
+
+ strcpy(buf, "0001234x");
+ checkit_uint(buf);
+
+ strcpy(buf, "4294967295"); // aka UINT32_MAX
+ checkit_uint(buf);
+
+ strcpy(buf, "4294967296"); // aka UINT32_MAX + 1
+ checkit_uint(buf);
+
+ strcpy(buf, "21474836470"); // INT32_MAX * 10
+ checkit_uint(buf);
+
+ strcpy(buf, "31474836470"); // INT32_MAX * 10 + a bunch
+ checkit_uint(buf);
+
+ strcpy(buf, "9223372036854775806"); // INT64_MAX - 1
+ checkit_uint(buf);
+
+ strcpy(buf, "9223372036854775807"); // INT64_MAX
+ checkit_uint(buf);
+
+ strcpy(buf, "9223372036854775808"); // INT64_MAX + 1
+ checkit_uint(buf);
+
+ strcpy(buf, "18446744073709551614"); // UINT64_MAX - 1
+ checkit_uint(buf);
+
+ strcpy(buf, "18446744073709551615"); // UINT64_MAX
+ checkit_uint(buf);
+
+ strcpy(buf, "18446744073709551616"); // UINT64_MAX + 1
+ checkit_uint(buf);
+
+ // Ensure we can still parse valid numbers after parsing out of range ones.
+ strcpy(buf, "123");
+ checkit_uint(buf);
+
return 0;
}
+==========json_parse_int64() test===========
buf=x parseit=1, value=-666
buf=0 parseit=0, value=0
buf=-0 parseit=0, value=0
buf=0001234x parseit=0, value=1234
buf=-00001234 parseit=0, value=-1234
buf=-00001234x parseit=0, value=-1234
+buf=4294967295 parseit=0, value=4294967295
+buf=4294967296 parseit=0, value=4294967296
buf=21474836470 parseit=0, value=21474836470
buf=31474836470 parseit=0, value=31474836470
buf=-2147483647 parseit=0, value=-2147483647
buf=18446744073709551616 parseit=0, value=9223372036854775807
buf=-18446744073709551616 parseit=0, value=-9223372036854775808
buf=123 parseit=0, value=123
+
+==========json_parse_uint64() test===========
+buf=x parseit=1, value=666
+buf=0 parseit=0, value=0
+buf=-0 parseit=1, value=0
+buf=00000000 parseit=0, value=0
+buf=-00000000 parseit=1, value=0
+buf=1 parseit=0, value=1
+buf=2147483647 parseit=0, value=2147483647
+buf=-1 parseit=1, value=18446744073709551615
+buf=-9223372036854775808 parseit=1, value=9223372036854775808
+buf= 1 parseit=0, value=1
+buf=00001234 parseit=0, value=1234
+buf=0001234x parseit=0, value=1234
+buf=4294967295 parseit=0, value=4294967295
+buf=4294967296 parseit=0, value=4294967296
+buf=21474836470 parseit=0, value=21474836470
+buf=31474836470 parseit=0, value=31474836470
+buf=9223372036854775806 parseit=0, value=9223372036854775806
+buf=9223372036854775807 parseit=0, value=9223372036854775807
+buf=9223372036854775808 parseit=0, value=9223372036854775808
+buf=18446744073709551614 parseit=0, value=18446744073709551614
+buf=18446744073709551615 parseit=0, value=18446744073709551615
+buf=18446744073709551616 parseit=0, value=18446744073709551615
+buf=123 parseit=0, value=123
assert (json_object_get_int64(tmp)==321321321);
json_object_put(tmp);
printf("INT64 PASSED\n");
+ tmp=json_object_new_uint64(123);
+ assert (json_object_get_int(tmp)==123);
+ assert (json_object_get_int64(tmp)==123);
+ assert (json_object_get_uint64(tmp)==123);
+ json_object_set_uint64(tmp,(uint64_t)321321321);
+ assert (json_object_get_uint64(tmp)==321321321);
+ json_object_set_uint64(tmp,9223372036854775808U);
+ assert (json_object_get_uint64(tmp)==9223372036854775808U);
+ json_object_put(tmp);
+ printf("UINT64 PASSED\n");
tmp=json_object_new_boolean(1);
assert (json_object_get_boolean(tmp)==1);
json_object_set_boolean(tmp,0);
assert (json_object_get_double(tmp)==34.56);
json_object_set_double(tmp,6435.34);
assert (json_object_get_double(tmp)==6435.34);
+ json_object_set_double(tmp,2e21);
+ assert (json_object_get_int64(tmp)==INT64_MAX);
+ assert (json_object_get_uint64(tmp)==UINT64_MAX);
+ json_object_set_double(tmp,-2e21);
+ assert (json_object_get_int64(tmp)==INT64_MIN);
+ assert (json_object_get_uint64(tmp)==0);
json_object_put(tmp);
printf("DOUBLE PASSED\n");
#define SHORT "SHORT"
INT PASSED
INT64 PASSED
+UINT64 PASSED
BOOL PASSED
DOUBLE PASSED
STRING PASSED