Skip to content

Commit

Permalink
Add json_sprintf and json_vsprintf
Browse files Browse the repository at this point in the history
Fixes #392
  • Loading branch information
akheron committed Feb 9, 2018
1 parent 3e81f78 commit efe6c7b
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -496,6 +496,7 @@ if (NOT JANSSON_WITHOUT_TESTS)
test_object
test_pack
test_simple
test_sprintf
test_unpack)

# Doing arithmetic on void pointers is not allowed by Microsofts compiler
Expand Down
10 changes: 10 additions & 0 deletions doc/apiref.rst
Expand Up @@ -399,6 +399,16 @@ length-aware functions if you wish to embed null bytes in strings.

.. versionadded:: 2.7

.. function:: json_t *json_sprintf(const char *format, ...)
json_t *json_vsprintf(const char *format, va_list ap)
.. refcounting:: new

Construct a JSON string from a format string and varargs, just like
:func:`printf()`.

.. versionadded:: 2.11


Number
======
Expand Down
2 changes: 2 additions & 0 deletions src/jansson.def
Expand Up @@ -3,6 +3,8 @@ EXPORTS
json_true
json_false
json_null
json_sprintf
json_vsprintf
json_string
json_stringn
json_string_nocheck
Expand Down
5 changes: 5 additions & 0 deletions src/jansson.h
Expand Up @@ -289,6 +289,11 @@ int json_unpack(json_t *root, const char *fmt, ...);
int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...);
int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);

/* sprintf */

json_t *json_sprintf(const char *fmt, ...);
json_t *json_vsprintf(const char *fmt, va_list ap);


/* equality */

Expand Down
34 changes: 34 additions & 0 deletions src/value.c
Expand Up @@ -787,6 +787,40 @@ static json_t *json_string_copy(const json_t *string)
return json_stringn_nocheck(s->value, s->length);
}

json_t *json_vsprintf(const char *fmt, va_list ap) {
int length;
char *buf;
va_list aq;
va_copy(aq, ap);

length = vsnprintf(NULL, 0, fmt, ap);
if (length == 0)
return json_string("");

buf = jsonp_malloc(length + 1);
if (!buf)
return NULL;

vsnprintf(buf, length + 1, fmt, aq);
if (!utf8_check_string(buf, length)) {
jsonp_free(buf);
return NULL;
}

return jsonp_stringn_nocheck_own(buf, length);
}

json_t *json_sprintf(const char *fmt, ...) {
json_t *result;
va_list ap;

va_start(ap, fmt);
result = json_vsprintf(fmt, ap);
va_end(ap);

return result;
}


/*** integer ***/

Expand Down
3 changes: 2 additions & 1 deletion test/.gitignore
Expand Up @@ -7,14 +7,15 @@ suites/api/test_dump
suites/api/test_dump_callback
suites/api/test_equal
suites/api/test_load
suites/api/test_load_callback
suites/api/test_loadb
suites/api/test_memory_funcs
suites/api/test_number
suites/api/test_object
suites/api/test_pack
suites/api/test_simple
suites/api/test_sprintf
suites/api/test_unpack
suites/api/test_load_callback
run-suites.log
run-suites.trs
test-suite.log
Expand Down
2 changes: 2 additions & 0 deletions test/suites/api/Makefile.am
Expand Up @@ -14,6 +14,7 @@ check_PROGRAMS = \
test_object \
test_pack \
test_simple \
test_sprintf \
test_unpack

test_array_SOURCES = test_array.c util.h
Expand All @@ -27,6 +28,7 @@ test_number_SOURCES = test_number.c util.h
test_object_SOURCES = test_object.c util.h
test_pack_SOURCES = test_pack.c util.h
test_simple_SOURCES = test_simple.c util.h
test_sprintf_SOURCES = test_sprintf.c util.h
test_unpack_SOURCES = test_unpack.c util.h

AM_CPPFLAGS = -I$(top_builddir)/src -I$(top_srcdir)/src
Expand Down
22 changes: 22 additions & 0 deletions test/suites/api/test_sprintf.c
@@ -0,0 +1,22 @@
#include <string.h>
#include <jansson.h>
#include "util.h"


static void test_sprintf() {
json_t *s = json_sprintf("foo bar %d", 42);
if (!s)
fail("json_sprintf returned NULL");
if (!json_is_string(s))
fail("json_sprintf didn't return a JSON string");
if (strcmp(json_string_value(s), "foo bar 42"))
fail("json_sprintf generated an unexpected string");

json_decref(s);
}


static void run_tests()
{
test_sprintf();
}

0 comments on commit efe6c7b

Please sign in to comment.