summaryrefslogtreecommitdiff
path: root/json_object.c
diff options
context:
space:
mode:
authorjobol <jose.bollo@iot.bzh>2016-07-26 19:22:25 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2016-07-27 14:45:25 +0200
commit344009bf26f53e0ba218401fa99eaad44cac33f1 (patch)
treee51c30c38ea82b0d60f6b09e51e84cd89d3db5d7 /json_object.c
parent54ae25453753f9133c8df14d555222ea20df5bcb (diff)
downloadjson-c-344009bf26f53e0ba218401fa99eaad44cac33f1.tar.gz
Add method 'json_object_to_json_string_length'
This new method allows to also get the length of the generated string. Fix #165 Change-Id: Iea91404027f143ca3d29a4c58d7c07ae53556110 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'json_object.c')
-rw-r--r--json_object.c33
1 files changed, 24 insertions, 9 deletions
diff --git a/json_object.c b/json_object.c
index 24cb7ab..29eb29d 100644
--- a/json_object.c
+++ b/json_object.c
@@ -295,20 +295,35 @@ void json_object_set_serializer(json_object *jso,
/* extended conversion to string */
-const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
+const char* json_object_to_json_string_length(struct json_object *jso, int flags, size_t *length)
{
- if (!jso)
- return "null";
+ const char *r = NULL;
+ size_t s = 0;
- if ((!jso->_pb) && !(jso->_pb = printbuf_new()))
- return NULL;
+ if (!jso)
+ {
+ s = 4;
+ r = "null";
+ }
+ else if ((jso->_pb) || (jso->_pb = printbuf_new()))
+ {
+ printbuf_reset(jso->_pb);
- printbuf_reset(jso->_pb);
+ if(jso->_to_json_string(jso, jso->_pb, 0, flags) >= 0)
+ {
+ s = (size_t)jso->_pb->bpos;
+ r = jso->_pb->buf;
+ }
+ }
- if(jso->_to_json_string(jso, jso->_pb, 0, flags) < 0)
- return NULL;
+ if (length)
+ *length = s;
+ return r;
+}
- return jso->_pb->buf;
+const char* json_object_to_json_string_ext(struct json_object *jso, int flags)
+{
+ return json_object_to_json_string_length(jso, flags, NULL);
}
/* backwards-compatible conversion to string */