From fae09456ae516100bf9e799c8a744903c88faeba Mon Sep 17 00:00:00 2001 From: Stoian Ivanov Date: Wed, 21 Sep 2016 01:31:00 +0300 Subject: [PATCH 1/7] json_object_set_boolean for upstream style check --- json_object.c | 7 +++++++ json_object.h | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/json_object.c b/json_object.c index f657afe..4f74a20 100644 --- a/json_object.c +++ b/json_object.c @@ -568,6 +568,13 @@ json_bool json_object_get_boolean(const struct json_object *jso) } } +json_bool json_object_set_boolean(struct json_object *jso,json_bool new_value){ + if (!jso || jso->o_type!=json_type_boolean) + return FALSE; + jso->o.c_boolean=new_value; + return TRUE; +} + /* json_object_int */ diff --git a/json_object.h b/json_object.h index b6fd917..63c3dc7 100644 --- a/json_object.h +++ b/json_object.h @@ -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 FALSE 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 TRUE if value is set correctly + */ +extern json_bool 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 From 05f025c07537f643b9cc463cb8fed6fbb72a73d8 Mon Sep 17 00:00:00 2001 From: Stoian Ivanov Date: Wed, 5 Oct 2016 23:55:46 +0300 Subject: [PATCH 2/7] some basic set --- json_object.c | 26 +++++++++++++++++++++++--- json_object.h | 46 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/json_object.c b/json_object.c index 4f74a20..ea2ea64 100644 --- a/json_object.c +++ b/json_object.c @@ -568,11 +568,11 @@ json_bool json_object_get_boolean(const struct json_object *jso) } } -json_bool json_object_set_boolean(struct json_object *jso,json_bool new_value){ +int json_object_set_boolean(struct json_object *jso,json_bool new_value){ if (!jso || jso->o_type!=json_type_boolean) - return FALSE; + return 0; jso->o.c_boolean=new_value; - return TRUE; + return 1; } @@ -634,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); @@ -666,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 */ @@ -820,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 */ diff --git a/json_object.h b/json_object.h index 63c3dc7..ff895b6 100644 --- a/json_object.h +++ b/json_object.h @@ -622,15 +622,15 @@ 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 FALSE is returned + * 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 TRUE if value is set correctly + * @returns 1 if value is set correctly, 0 otherwise */ -extern json_bool json_object_set_boolean(struct json_object *obj,json_bool new_value); +extern int json_object_set_boolean(struct json_object *obj,json_bool new_value); /* int type methods */ @@ -667,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. @@ -683,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 @@ -773,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 From 6a0667567d2daef620ce84ac5e282714adea3fa2 Mon Sep 17 00:00:00 2001 From: Stoian Ivanov Date: Thu, 6 Oct 2016 23:16:29 +0300 Subject: [PATCH 3/7] some simple tests --- tests/Makefile.am | 1 + tests/test_set_value.c | 30 ++++++++++++++++++++++++++++++ tests/test_set_value.expected | 4 ++++ tests/test_set_value.test | 12 ++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 tests/test_set_value.c create mode 100644 tests/test_set_value.expected create mode 100755 tests/test_set_value.test diff --git a/tests/Makefile.am b/tests/Makefile.am index d33e22c..f6e4b2d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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=) diff --git a/tests/test_set_value.c b/tests/test_set_value.c new file mode 100644 index 0000000..a321260 --- /dev/null +++ b/tests/test_set_value.c @@ -0,0 +1,30 @@ +#include +#include +#include + +#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"); + + + printf("PASSED\n"); + return 0; +} diff --git a/tests/test_set_value.expected b/tests/test_set_value.expected new file mode 100644 index 0000000..0ac57fe --- /dev/null +++ b/tests/test_set_value.expected @@ -0,0 +1,4 @@ +INT PASSED +INT64 PASSED +BOOL PASSED +PASSED diff --git a/tests/test_set_value.test b/tests/test_set_value.test new file mode 100755 index 0000000..4e8fdac --- /dev/null +++ b/tests/test_set_value.test @@ -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 $? From 9a313f767f42dfdaa957ae836a1a7f20438baba4 Mon Sep 17 00:00:00 2001 From: Stoian Ivanov Date: Thu, 6 Oct 2016 23:32:19 +0300 Subject: [PATCH 4/7] gitignore test; add double value set checks in test --- .gitignore | 1 + tests/test_set_value.c | 10 ++++++++-- tests/test_set_value.expected | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index ac8d17c..68b6fcf 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/tests/test_set_value.c b/tests/test_set_value.c index a321260..f64924c 100644 --- a/tests/test_set_value.c +++ b/tests/test_set_value.c @@ -23,8 +23,14 @@ int main(int argc, char **argv) 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"); printf("PASSED\n"); return 0; } diff --git a/tests/test_set_value.expected b/tests/test_set_value.expected index 0ac57fe..ccc3f5c 100644 --- a/tests/test_set_value.expected +++ b/tests/test_set_value.expected @@ -1,4 +1,5 @@ INT PASSED INT64 PASSED BOOL PASSED +DOUBLE PASSED PASSED From e518b22b72ea8ce370c9ec2dd37c16129a2011da Mon Sep 17 00:00:00 2001 From: Stoian Ivanov Date: Fri, 7 Oct 2016 00:51:24 +0300 Subject: [PATCH 5/7] string set and tests --- json_object.c | 21 +++++++++++++++++++++ json_object.h | 20 ++++++++++++++++++++ tests/test_set_value.c | 16 ++++++++++++++++ tests/test_set_value.expected | 1 + 4 files changed, 58 insertions(+) diff --git a/json_object.c b/json_object.c index ea2ea64..9daa999 100644 --- a/json_object.c +++ b/json_object.c @@ -935,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, 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 (leno.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 */ diff --git a/json_object.h b/json_object.h index ff895b6..f86f678 100644 --- a/json_object.h +++ b/json_object.h @@ -854,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. diff --git a/tests/test_set_value.c b/tests/test_set_value.c index f64924c..4c0a54a 100644 --- a/tests/test_set_value.c +++ b/tests/test_set_value.c @@ -31,6 +31,22 @@ int main(int argc, char **argv) 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; } diff --git a/tests/test_set_value.expected b/tests/test_set_value.expected index ccc3f5c..6a900f9 100644 --- a/tests/test_set_value.expected +++ b/tests/test_set_value.expected @@ -2,4 +2,5 @@ INT PASSED INT64 PASSED BOOL PASSED DOUBLE PASSED +STRING PASSED PASSED From a26305d428ff462ed66e4bf5d2751277e283e44a Mon Sep 17 00:00:00 2001 From: Stoian Ivanov Date: Fri, 7 Oct 2016 01:07:34 +0300 Subject: [PATCH 6/7] fix compiler warning for int sizes --- json_object.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json_object.c b/json_object.c index 9daa999..e7f63bb 100644 --- a/json_object.c +++ b/json_object.c @@ -936,7 +936,7 @@ 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, strlen(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){ From be63ba99ca2f42177bff81f79ee9d65b106c38f4 Mon Sep 17 00:00:00 2001 From: Stoian Ivanov Date: Fri, 7 Oct 2016 22:54:06 +0300 Subject: [PATCH 7/7] try restore windows automated builds --- json_object.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json_object.c b/json_object.c index e7f63bb..bdc6f6b 100644 --- a/json_object.c +++ b/json_object.c @@ -741,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)