diff options
author | Stoian Ivanov <sdr@mail.bg> | 2016-10-07 00:51:24 +0300 |
---|---|---|
committer | Stoian Ivanov <sdr@mail.bg> | 2016-10-07 00:51:24 +0300 |
commit | e518b22b72ea8ce370c9ec2dd37c16129a2011da (patch) | |
tree | 24d1fc9dd9b6fc4068b9acbd039f39f7b893f18d /json_object.c | |
parent | 9a313f767f42dfdaa957ae836a1a7f20438baba4 (diff) | |
download | json-c-e518b22b72ea8ce370c9ec2dd37c16129a2011da.tar.gz |
string set and tests
Diffstat (limited to 'json_object.c')
-rw-r--r-- | json_object.c | 21 |
1 files changed, 21 insertions, 0 deletions
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 (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 */ |