From: Stoian Ivanov Date: Thu, 6 Oct 2016 21:51:24 +0000 (+0300) Subject: string set and tests X-Git-Tag: json-c-0.13-20171207~121^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e518b22b72ea8ce370c9ec2dd37c16129a2011da;p=json-c string set and tests --- 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