[asterisk-commits] dlee: branch dlee/jansson r378905 - in /team/dlee/jansson: include/asterisk/ ...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Jan 9 23:01:04 CST 2013


Author: dlee
Date: Wed Jan  9 23:01:01 2013
New Revision: 378905

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=378905
Log:
Added ast_json_dump_str function

Modified:
    team/dlee/jansson/include/asterisk/json.h
    team/dlee/jansson/res/res_json.c
    team/dlee/jansson/tests/test_json.c

Modified: team/dlee/jansson/include/asterisk/json.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/jansson/include/asterisk/json.h?view=diff&rev=378905&r1=378904&r2=378905
==============================================================================
--- team/dlee/jansson/include/asterisk/json.h (original)
+++ team/dlee/jansson/include/asterisk/json.h Wed Jan  9 23:01:01 2013
@@ -399,6 +399,16 @@
  * Returned string must be freed by calling ast_free().
  */
 char *ast_json_dump_string(struct ast_json *root);
+
+/*!
+ * \brief Writes JSON representation of \a root to \a dst.
+ *
+ * If \a dst is too small, it will be grown as needed.
+ *
+ * \return 0 on success, or -1 on error. On error, the contents of \a dst are
+ * undefined.
+ */
+int ast_json_dump_str(struct ast_json *root, struct ast_str **dst);
 
 /*!
  * \brief Writes JSON representation of \a root to \a output.

Modified: team/dlee/jansson/res/res_json.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/jansson/res/res_json.c?view=diff&rev=378905&r1=378904&r2=378905
==============================================================================
--- team/dlee/jansson/res/res_json.c (original)
+++ team/dlee/jansson/res/res_json.c Wed Jan  9 23:01:01 2013
@@ -344,6 +344,36 @@
 {
 	return json_dumps((json_t *)root, dump_flags());
 }
+
+static int write_to_ast_str(const char *buffer, size_t size, void *data)
+{
+	struct ast_str **dst = data;
+	/* While ast_str_append will grow the ast_str, it won't report
+	 * allocation errors. Fortunately, it's not that hard.
+	 */
+	size_t remaining = ast_str_size(*dst) - ast_str_strlen(*dst);
+	while (remaining < size) {
+		/* doubling the size of the buffer gives us 'amortized
+		 * constant' time.
+		 * See http://stackoverflow.com/a/249695/115478 for info.
+		 */
+		int ret = ast_str_make_space(dst, 2 * ast_str_size(*dst));
+		if (ret == -1) {
+			/* Could not alloc; fail */
+			return -1;
+		}
+		remaining = ast_str_size(*dst) - ast_str_strlen(*dst);
+	}
+	ast_str_append_substr(dst, -1, buffer, size);
+	return 0;
+}
+
+int ast_json_dump_str(struct ast_json *root, struct ast_str **dst)
+{
+	return json_dump_callback((json_t *)root, write_to_ast_str, dst, dump_flags());
+}
+
+
 int ast_json_dump_file(struct ast_json *root, FILE *output)
 {
 	if (root && output) {

Modified: team/dlee/jansson/tests/test_json.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/jansson/tests/test_json.c?view=diff&rev=378905&r1=378904&r2=378905
==============================================================================
--- team/dlee/jansson/tests/test_json.c (original)
+++ team/dlee/jansson/tests/test_json.c Wed Jan  9 23:01:01 2013
@@ -83,6 +83,7 @@
 	char *str = NULL;
 	FILE *file = NULL;
 	char *filename = NULL;
+	struct ast_str *astr = NULL;
 
 	auto void clean_vars(void);
 	/*!
@@ -104,6 +105,7 @@
 		file = NULL;
 		free(filename);
 		filename = NULL;
+		astr = NULL;
 	}
 
 	/*!
@@ -586,8 +588,7 @@
 	CLEAN_VARS();
 
 	/* dump/load string */
-	expected = ast_json_object_create();
-	ast_json_object_set(expected, "one", ast_json_integer_create(1));
+	expected = ast_json_pack("{ s: i }", "one", 1);
 	str = ast_json_dump_string(expected);
 	ast_test_check(res, NULL != str);
 	uut = ast_json_load_string(str, NULL);
@@ -595,8 +596,26 @@
 	ast_test_check(res, ast_json_equal(expected, uut));
 	CLEAN_VARS();
 
-	/* dumps NULL */
+	/* dump_string NULL */
 	ast_test_check(res, NULL == ast_json_dump_string(NULL));
+
+	/* dump ast_str */
+	expected = ast_json_pack("{ s: i }", "one", 1);
+	astr = ast_str_create(1); /* should expand to hold output */
+	uut_res = ast_json_dump_str(expected, &astr);
+	ast_test_check(res, 0 == uut_res);
+	uut = ast_json_load_string(ast_str_buffer(astr), NULL);
+	ast_test_check(res, NULL != uut);
+	ast_test_check(res, ast_json_equal(expected, uut));
+	ast_free(astr);
+	CLEAN_VARS();
+
+	/* dump ast_str growth failure */
+	expected = ast_json_pack("{ s: i }", "one", 1);
+	astr = ast_str_alloca(1); /* cannot grow */
+	uut_res = ast_json_dump_str(expected, &astr);
+	ast_test_check(res, 0 != uut_res);
+	CLEAN_VARS();
 
 	/* load buffer */
 	str = "{ \"one\": 1 } trailing garbage";




More information about the asterisk-commits mailing list