From 54ae25453753f9133c8df14d555222ea20df5bcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Bollo?= Date: Wed, 27 Jul 2016 14:42:25 +0200 Subject: [PATCH 1/3] Fix a compiling error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The function fstat isn't declared without this include on debian strect/sid and Fedora-23 Change-Id: I660a32ff173dcba04674aed51ed855b4fa55ac67 Signed-off-by: José Bollo --- tests/test_util_file.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_util_file.c b/tests/test_util_file.c index 61bf1c5..f62b687 100644 --- a/tests/test_util_file.c +++ b/tests/test_util_file.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "json.h" #include "json_util.h" From 344009bf26f53e0ba218401fa99eaad44cac33f1 Mon Sep 17 00:00:00 2001 From: jobol Date: Tue, 26 Jul 2016 19:22:25 +0200 Subject: [PATCH 2/3] Add method 'json_object_to_json_string_length' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This new method allows to also get the length of the generated string. Fix #165 Change-Id: Iea91404027f143ca3d29a4c58d7c07ae53556110 Signed-off-by: José Bollo --- json_object.c | 33 ++++++++++++++++++++++++--------- json_object.h | 10 ++++++++++ 2 files changed, 34 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 */ diff --git a/json_object.h b/json_object.h index 4bcfcbc..19afa5e 100644 --- a/json_object.h +++ b/json_object.h @@ -223,6 +223,16 @@ extern const char* json_object_to_json_string(struct json_object *obj); extern const char* json_object_to_json_string_ext(struct json_object *obj, int flags); +/** Stringify object to json format + * @see json_object_to_json_string() for details on how to free string. + * @param obj the json_object instance + * @param flags formatting options, see JSON_C_TO_STRING_PRETTY and other constants + * @param length a pointer where, if not NULL, the length (without null) is stored + * @returns a string in JSON format and the length if not NULL + */ +extern const char* json_object_to_json_string_length(struct json_object *obj, int +flags, size_t *length); + /** * Returns the userdata set by json_object_set_userdata() or * json_object_set_serializer() From 1fceb2207aed183c4855b3ad48ec886bbb06ae85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Bollo?= Date: Mon, 1 Aug 2016 15:04:54 +0200 Subject: [PATCH 3/3] test: add test of 'json_object_to_json_string_length' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The test is implied in test1. The idea is to check that the returned lengths and strings are identical to what is expected to return the already tested function 'json_object_to_json_string_ext'. Signed-off-by: José Bollo --- tests/test1.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/test1.c b/tests/test1.c index 3cbd186..05956d1 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -28,7 +28,27 @@ static int sort_fn (const void *j1, const void *j2) } #ifdef TEST_FORMATTED -#define json_object_to_json_string(obj) json_object_to_json_string_ext(obj,sflags) +static const char *to_json_string(json_object *obj, int flags) +{ + size_t length; + char *copy; + const char *result; + + result = json_object_to_json_string_length(obj, flags, &length); + copy = strndup(result, length); + if (copy == NULL) + printf("to_json_string: Allocation failed!\n"); + else { + result = json_object_to_json_string_ext(obj, flags); + if (length != strlen(result)) + printf("to_json_string: Length mismatch!\n"); + if (strcmp(copy, result) != 0) + printf("to_json_string: Comparison Failed!\n"); + free(copy); + } + return result; +} +#define json_object_to_json_string(obj) to_json_string(obj,sflags) #else /* no special define */ #endif