mirror of
https://github.com/json-c/json-c.git
synced 2026-03-22 22:49:06 +08:00
string set and tests
This commit is contained in:
@@ -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 (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 */
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -2,4 +2,5 @@ INT PASSED
|
||||
INT64 PASSED
|
||||
BOOL PASSED
|
||||
DOUBLE PASSED
|
||||
STRING PASSED
|
||||
PASSED
|
||||
|
||||
Reference in New Issue
Block a user