@@ -51,6 +51,7 @@ static json_object_to_json_string_fn json_object_object_to_json_string;
51
51
static json_object_to_json_string_fn json_object_boolean_to_json_string ;
52
52
static json_object_to_json_string_fn json_object_double_to_json_string_default ;
53
53
static json_object_to_json_string_fn json_object_int_to_json_string ;
54
+ static json_object_to_json_string_fn json_object_uint_to_json_string ;
54
55
static json_object_to_json_string_fn json_object_string_to_json_string ;
55
56
static json_object_to_json_string_fn json_object_array_to_json_string ;
56
57
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,
302
303
case json_type_int :
303
304
jso -> _to_json_string = & json_object_int_to_json_string ;
304
305
break ;
306
+ case json_type_uint :
307
+ jso -> _to_json_string = & json_object_uint_to_json_string ;
308
+ break ;
305
309
case json_type_object :
306
310
jso -> _to_json_string = & json_object_object_to_json_string ;
307
311
break ;
@@ -592,6 +596,8 @@ json_bool json_object_get_boolean(const struct json_object *jso)
592
596
return jso -> o .c_boolean ;
593
597
case json_type_int :
594
598
return (jso -> o .c_int64 != 0 );
599
+ case json_type_uint :
600
+ return (jso -> o .c_uint64 != 0 );
595
601
case json_type_double :
596
602
return (jso -> o .c_double != 0 );
597
603
case json_type_string :
@@ -622,6 +628,17 @@ static int json_object_int_to_json_string(struct json_object* jso,
622
628
return printbuf_memappend (pb , sbuf , strlen (sbuf ));
623
629
}
624
630
631
+ static int json_object_uint_to_json_string (struct json_object * jso ,
632
+ struct printbuf * pb ,
633
+ int level ,
634
+ int flags )
635
+ {
636
+ /* room for 20 digits, and a null term */
637
+ char sbuf [21 ];
638
+ snprintf (sbuf , sizeof (sbuf ), "%" PRIu64 , jso -> o .c_uint64 );
639
+ return printbuf_memappend (pb , sbuf , strlen (sbuf ));
640
+ }
641
+
625
642
struct json_object * json_object_new_int (int32_t i )
626
643
{
627
644
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)
661
678
if (cint64 >= INT32_MAX )
662
679
return INT32_MAX ;
663
680
return (int32_t ) cint64 ;
681
+ case json_type_uint :
682
+ if (jso -> o .c_uint64 >= INT32_MAX )
683
+ return INT32_MAX ;
684
+ return (int32_t )jso -> o .c_uint64 ;
664
685
case json_type_double :
665
686
if (jso -> o .c_double <= INT32_MIN )
666
687
return INT32_MIN ;
@@ -691,6 +712,16 @@ struct json_object* json_object_new_int64(int64_t i)
691
712
return jso ;
692
713
}
693
714
715
+ struct json_object * json_object_new_uint64 (uint64_t i )
716
+ {
717
+ struct json_object * jso = json_object_new (json_type_uint );
718
+ if (!jso )
719
+ return NULL ;
720
+ jso -> _to_json_string = & json_object_uint_to_json_string ;
721
+ jso -> o .c_uint64 = i ;
722
+ return jso ;
723
+ }
724
+
694
725
int64_t json_object_get_int64 (const struct json_object * jso )
695
726
{
696
727
int64_t cint ;
@@ -701,6 +732,10 @@ int64_t json_object_get_int64(const struct json_object *jso)
701
732
{
702
733
case json_type_int :
703
734
return jso -> o .c_int64 ;
735
+ case json_type_uint :
736
+ if (jso -> o .c_uint64 >= INT64_MAX )
737
+ return INT64_MAX ;
738
+ return (int64_t )jso -> o .c_uint64 ;
704
739
case json_type_double :
705
740
// INT64_MAX can't be exactly represented as a double
706
741
// so cast to tell the compiler it's ok to round up.
@@ -720,13 +755,53 @@ int64_t json_object_get_int64(const struct json_object *jso)
720
755
}
721
756
}
722
757
758
+ uint64_t json_object_get_uint64 (const struct json_object * jso )
759
+ {
760
+ uint64_t cuint ;
761
+
762
+ if (!jso )
763
+ return 0 ;
764
+ switch (jso -> o_type )
765
+ {
766
+ case json_type_int :
767
+ if (jso -> o .c_int64 < 0 )
768
+ return 0 ;
769
+ return (uint64_t )jso -> o .c_int64 ;
770
+ case json_type_uint :
771
+ return jso -> o .c_uint64 ;
772
+ case json_type_double :
773
+ // UINT64_MAX can't be exactly represented as a double
774
+ // so cast to tell the compiler it's ok to round up.
775
+ if (jso -> o .c_double >= (double )UINT64_MAX )
776
+ return UINT64_MAX ;
777
+ if (jso -> o .c_double < 0 )
778
+ return 0 ;
779
+ return (uint64_t )jso -> o .c_double ;
780
+ case json_type_boolean :
781
+ return jso -> o .c_boolean ;
782
+ case json_type_string :
783
+ if (json_parse_uint64 (get_string_component (jso ), & cuint ) == 0 )
784
+ return cuint ;
785
+ /* FALLTHRU */
786
+ default :
787
+ return 0 ;
788
+ }
789
+ }
790
+
723
791
int json_object_set_int64 (struct json_object * jso ,int64_t new_value ){
724
792
if (!jso || jso -> o_type != json_type_int )
725
793
return 0 ;
726
794
jso -> o .c_int64 = new_value ;
727
795
return 1 ;
728
796
}
729
797
798
+ int json_object_set_uint64 (struct json_object * jso ,uint64_t new_value ){
799
+ if (!jso || jso -> o_type != json_type_uint )
800
+ return 0 ;
801
+ jso -> o .c_uint64 = new_value ;
802
+ return 1 ;
803
+ }
804
+
730
805
int json_object_int_inc (struct json_object * jso , int64_t val ) {
731
806
if (!jso || jso -> o_type != json_type_int )
732
807
return 0 ;
@@ -740,6 +815,17 @@ int json_object_int_inc(struct json_object *jso, int64_t val) {
740
815
return 1 ;
741
816
}
742
817
818
+ int json_object_uint_inc (struct json_object * jso , uint64_t val ) {
819
+ if (!jso || jso -> o_type != json_type_uint )
820
+ return 0 ;
821
+ if (jso -> o .c_uint64 > UINT64_MAX - val ) {
822
+ jso -> o .c_uint64 = UINT64_MAX ;
823
+ } else {
824
+ jso -> o .c_uint64 += val ;
825
+ }
826
+ return 1 ;
827
+ }
828
+
743
829
/* json_object_double */
744
830
745
831
#if defined(HAVE___THREAD )
@@ -962,6 +1048,8 @@ double json_object_get_double(const struct json_object *jso)
962
1048
return jso -> o .c_double ;
963
1049
case json_type_int :
964
1050
return jso -> o .c_int64 ;
1051
+ case json_type_uint :
1052
+ return jso -> o .c_uint64 ;
965
1053
case json_type_boolean :
966
1054
return jso -> o .c_boolean ;
967
1055
case json_type_string :
@@ -1342,6 +1430,9 @@ int json_object_equal(struct json_object* jso1, struct json_object* jso2)
1342
1430
case json_type_int :
1343
1431
return (jso1 -> o .c_int64 == jso2 -> o .c_int64 );
1344
1432
1433
+ case json_type_uint :
1434
+ return (jso1 -> o .c_uint64 == jso2 -> o .c_uint64 );
1435
+
1345
1436
case json_type_string :
1346
1437
return (jso1 -> o .c_string .len == jso2 -> o .c_string .len &&
1347
1438
memcmp (get_string_component (jso1 ),
@@ -1405,6 +1496,10 @@ int json_c_shallow_copy_default(json_object *src, json_object *parent, const cha
1405
1496
* dst = json_object_new_int64 (src -> o .c_int64 );
1406
1497
break ;
1407
1498
1499
+ case json_type_uint :
1500
+ * dst = json_object_new_uint64 (src -> o .c_uint64 );
1501
+ break ;
1502
+
1408
1503
case json_type_string :
1409
1504
* dst = json_object_new_string (get_string_component (src ));
1410
1505
break ;
0 commit comments