mirror of
https://github.com/json-c/json-c.git
synced 2026-03-29 01:49:06 +08:00
add uint64 data to json-c
This commit is contained in:
@@ -51,6 +51,7 @@ static json_object_to_json_string_fn json_object_object_to_json_string;
|
||||
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;
|
||||
@@ -302,6 +303,9 @@ void json_object_set_serializer(json_object *jso,
|
||||
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;
|
||||
@@ -592,6 +596,8 @@ json_bool json_object_get_boolean(const struct json_object *jso)
|
||||
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:
|
||||
@@ -622,6 +628,17 @@ static int json_object_int_to_json_string(struct json_object* jso,
|
||||
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);
|
||||
@@ -661,6 +678,10 @@ int32_t json_object_get_int(const struct json_object *jso)
|
||||
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;
|
||||
@@ -691,6 +712,16 @@ struct json_object* json_object_new_int64(int64_t i)
|
||||
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;
|
||||
@@ -701,6 +732,10 @@ int64_t json_object_get_int64(const struct json_object *jso)
|
||||
{
|
||||
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.
|
||||
@@ -720,6 +755,39 @@ int64_t json_object_get_int64(const struct json_object *jso)
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -727,6 +795,13 @@ int json_object_set_int64(struct json_object *jso,int64_t new_value){
|
||||
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;
|
||||
@@ -740,6 +815,17 @@ int json_object_int_inc(struct json_object *jso, int64_t val) {
|
||||
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)
|
||||
@@ -962,6 +1048,8 @@ double json_object_get_double(const struct json_object *jso)
|
||||
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:
|
||||
@@ -1342,6 +1430,9 @@ int json_object_equal(struct json_object* jso1, struct json_object* jso2)
|
||||
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),
|
||||
@@ -1405,6 +1496,10 @@ int json_c_shallow_copy_default(json_object *src, json_object *parent, const cha
|
||||
*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;
|
||||
|
||||
Reference in New Issue
Block a user