Json object set xxxtags/json-c-0.13-20171207
| @@ -33,6 +33,7 @@ | |||
| /tests/test_set_serializer | |||
| /tests/test_compare | |||
| /tests/test_util_file | |||
| /tests/test_set_value | |||
| /tests/*.vg.out | |||
| /tests/*.log | |||
| /tests/*.trs | |||
| @@ -568,6 +568,13 @@ json_bool json_object_get_boolean(const struct json_object *jso) | |||
| } | |||
| } | |||
| int json_object_set_boolean(struct json_object *jso,json_bool new_value){ | |||
| if (!jso || jso->o_type!=json_type_boolean) | |||
| return 0; | |||
| jso->o.c_boolean=new_value; | |||
| return 1; | |||
| } | |||
| /* json_object_int */ | |||
| @@ -627,6 +634,14 @@ int32_t json_object_get_int(const struct json_object *jso) | |||
| } | |||
| } | |||
| int json_object_set_int(struct json_object *jso,int new_value){ | |||
| if (!jso || jso->o_type!=json_type_int) | |||
| return 0; | |||
| jso->o.c_int64=new_value; | |||
| return 1; | |||
| } | |||
| struct json_object* json_object_new_int64(int64_t i) | |||
| { | |||
| struct json_object *jso = json_object_new(json_type_int); | |||
| @@ -659,6 +674,12 @@ int64_t json_object_get_int64(const struct json_object *jso) | |||
| } | |||
| } | |||
| int json_object_set_int64(struct json_object *jso,int64_t new_value){ | |||
| if (!jso || jso->o_type!=json_type_int) | |||
| return 0; | |||
| jso->o.c_int64=new_value; | |||
| return 1; | |||
| } | |||
| /* json_object_double */ | |||
| @@ -720,7 +741,7 @@ int json_object_double_to_json_string(struct json_object* jso, | |||
| int flags) | |||
| { | |||
| return json_object_double_to_json_string_format(jso, pb, level, flags, | |||
| jso->_userdata); | |||
| (const char *)jso->_userdata); | |||
| } | |||
| struct json_object* json_object_new_double(double d) | |||
| @@ -813,6 +834,12 @@ double json_object_get_double(const struct json_object *jso) | |||
| } | |||
| } | |||
| int json_object_set_double(struct json_object *jso,double new_value){ | |||
| if (!jso || jso->o_type!=json_type_double) | |||
| return 0; | |||
| jso->o.c_double=new_value; | |||
| return 1; | |||
| } | |||
| /* json_object_string */ | |||
| @@ -908,6 +935,27 @@ int json_object_get_string_len(const struct json_object *jso) | |||
| } | |||
| } | |||
| int json_object_set_string(json_object* jso, const char* s) { | |||
| return json_object_set_string_len(jso, s, (int)(strlen(s))); | |||
| } | |||
| int json_object_set_string_len(json_object* jso, const char* s, int len){ | |||
| if (jso==NULL || jso->o_type!=json_type_string) return 0; | |||
| char *dstbuf; | |||
| if (len<LEN_DIRECT_STRING_DATA) { | |||
| dstbuf=jso->o.c_string.str.data; | |||
| if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr); | |||
| } else { | |||
| dstbuf=(char *)malloc(len+1); | |||
| if (dstbuf==NULL) return 0; | |||
| if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr); | |||
| jso->o.c_string.str.ptr=dstbuf; | |||
| } | |||
| jso->o.c_string.len=len; | |||
| memcpy(dstbuf, (const void *)s, len); | |||
| dstbuf[len] = '\0'; | |||
| return 1; | |||
| } | |||
| /* json_object_array */ | |||
| @@ -620,6 +620,19 @@ extern struct json_object* json_object_new_boolean(json_bool b); | |||
| extern json_bool json_object_get_boolean(const struct json_object *obj); | |||
| /** Set the json_bool value of a json_object | |||
| * | |||
| * The type of obj is checked to be a json_type_boolean and 0 is returned | |||
| * if it is not without any further actions. If type of obj is json_type_boolean | |||
| * the obect value is chaned 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 | |||
| */ | |||
| extern int json_object_set_boolean(struct json_object *obj,json_bool new_value); | |||
| /* int type methods */ | |||
| /** Create a new empty json_object of type json_type_int | |||
| @@ -654,6 +667,19 @@ extern struct json_object* json_object_new_int64(int64_t i); | |||
| */ | |||
| extern int32_t json_object_get_int(const struct json_object *obj); | |||
| /** Set the int value of a json_object | |||
| * | |||
| * The type of obj is checked to be a json_type_int and 0 is returned | |||
| * if it is not without any further actions. If type of obj is json_type_int | |||
| * the obect value is chaned 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 | |||
| */ | |||
| extern int json_object_set_int(struct json_object *obj,int new_value); | |||
| /** Get the int value of a json_object | |||
| * | |||
| * The type is coerced to a int64 if the passed object is not a int64. | |||
| @@ -670,6 +696,19 @@ extern int32_t json_object_get_int(const struct json_object *obj); | |||
| extern int64_t json_object_get_int64(const struct json_object *obj); | |||
| /** Set the int64_t value of a json_object | |||
| * | |||
| * The type of obj is checked to be a json_type_int and 0 is returned | |||
| * if it is not without any further actions. If type of obj is json_type_int | |||
| * the obect value is chaned 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 | |||
| */ | |||
| extern int json_object_set_int64(struct json_object *obj,int64_t new_value); | |||
| /* double type methods */ | |||
| /** Create a new empty json_object of type json_type_double | |||
| @@ -760,6 +799,20 @@ extern int json_object_double_to_json_string(struct json_object* jso, | |||
| extern double json_object_get_double(const struct json_object *obj); | |||
| /** Set the double value of a json_object | |||
| * | |||
| * The type of obj is checked to be a json_type_double and 0 is returned | |||
| * if it is not without any further actions. If type of obj is json_type_double | |||
| * the obect value is chaned 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 | |||
| */ | |||
| extern int json_object_set_double(struct json_object *obj,double new_value); | |||
| /* string type methods */ | |||
| /** Create a new empty json_object of type json_type_string | |||
| @@ -801,6 +854,26 @@ extern const char* json_object_get_string(struct json_object *obj); | |||
| */ | |||
| extern int json_object_get_string_len(const struct json_object *obj); | |||
| /** Set the string value of a json_object with zero terminated strings | |||
| * equivalent to json_object_set_string_len (obj, new_value, strlen(new_value)) | |||
| * @returns 1 if value is set correctly, 0 otherwise | |||
| */ | |||
| extern int json_object_set_string(json_object* obj, const char* new_value); | |||
| /** Set the string value of a json_object str | |||
| * | |||
| * The type of obj is checked to be a json_type_string and 0 is returned | |||
| * if it is not without any further actions. If type of obj is json_type_string | |||
| * the obect value is chaned to new_value | |||
| * | |||
| * @param obj the json_object instance | |||
| * @param new_value the value to be set; Since string legth is given in len this need not be zero terminated | |||
| * @param len the length of new_value | |||
| * @returns 1 if value is set correctly, 0 otherwise | |||
| */ | |||
| extern int json_object_set_string_len(json_object* obj, const char* new_value, int len); | |||
| /** Check if two json_object's are equal | |||
| * | |||
| * If the passed objects are equal 1 will be returned. | |||
| @@ -21,6 +21,7 @@ TESTS+= test_charcase.test | |||
| TESTS+= test_printbuf.test | |||
| TESTS+= test_set_serializer.test | |||
| TESTS+= test_compare.test | |||
| TESTS+= test_set_value.test | |||
| check_PROGRAMS= | |||
| check_PROGRAMS += $(TESTS:.test=) | |||
| @@ -0,0 +1,52 @@ | |||
| #include <assert.h> | |||
| #include <stdio.h> | |||
| #include <string.h> | |||
| #include "json.h" | |||
| int main(int argc, char **argv) | |||
| { | |||
| json_object *tmp=json_object_new_int(123); | |||
| assert (json_object_get_int(tmp)==123); | |||
| json_object_set_int(tmp,321); | |||
| assert (json_object_get_int(tmp)==321); | |||
| printf("INT PASSED\n"); | |||
| json_object_set_int64(tmp,(int64_t)321321321); | |||
| assert (json_object_get_int64(tmp)==321321321); | |||
| json_object_put(tmp); | |||
| printf("INT64 PASSED\n"); | |||
| tmp=json_object_new_boolean(TRUE); | |||
| assert (json_object_get_boolean(tmp)==TRUE); | |||
| json_object_set_boolean(tmp,FALSE); | |||
| assert (json_object_get_boolean(tmp)==FALSE); | |||
| json_object_set_boolean(tmp,TRUE); | |||
| assert (json_object_get_boolean(tmp)==TRUE); | |||
| json_object_put(tmp); | |||
| printf("BOOL PASSED\n"); | |||
| tmp=json_object_new_double(12.34); | |||
| assert (json_object_get_double(tmp)==12.34); | |||
| json_object_set_double(tmp,34.56); | |||
| 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_put(tmp); | |||
| printf("DOUBLE PASSED\n"); | |||
| #define SHORT "SHORT" | |||
| #define MID "A MID STRING" | |||
| // 12345678901234567890123456789012.... | |||
| #define HUGE "A string longer than 32 chars as to check non local buf codepath" | |||
| tmp=json_object_new_string(SHORT); | |||
| assert (strcmp(json_object_get_string(tmp),SHORT)==0); | |||
| json_object_set_string(tmp,MID); | |||
| assert (strcmp(json_object_get_string(tmp),MID)==0); | |||
| json_object_set_string(tmp,HUGE); | |||
| assert (strcmp(json_object_get_string(tmp),HUGE)==0); | |||
| json_object_set_string(tmp,SHORT); | |||
| assert (strcmp(json_object_get_string(tmp),SHORT)==0); | |||
| json_object_put(tmp); | |||
| printf("STRING PASSED\n"); | |||
| printf("PASSED\n"); | |||
| return 0; | |||
| } | |||
| @@ -0,0 +1,6 @@ | |||
| INT PASSED | |||
| INT64 PASSED | |||
| BOOL PASSED | |||
| DOUBLE PASSED | |||
| STRING PASSED | |||
| PASSED | |||
| @@ -0,0 +1,12 @@ | |||
| #!/bin/sh | |||
| # Common definitions | |||
| if test -z "$srcdir"; then | |||
| srcdir="${0%/*}" | |||
| test "$srcdir" = "$0" && srcdir=. | |||
| test -z "$srcdir" && srcdir=. | |||
| fi | |||
| . "$srcdir/test-defs.sh" | |||
| run_output_test test_set_value | |||
| exit $? | |||