Skip to content

Commit

Permalink
Add mjson_snprintf() and mjson_aprintf()
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed May 27, 2021
1 parent 65bc347 commit a01a2e5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ if (mjson_get_bool(s, strlen(s), "$.b[1]", &boolval)) printf("%d\n", boolval);

Print into a dynamically-allocated string:
```c
char *result = NULL; // It's important to initialise it to NULL
mjson_printf(mjson_print_dynamic_buf, &result, "{%Q:%d}", "a", (int) 123);
printf("%s\n", result); // {"a":123}
free(result); // Caller must deallocate result
char buf[100];
mjson_snprintf(buf, sizeof(buf), "{%Q:%d}", "a", (int) 123);
printf("%s\n", buf); // {"a":123}
```
Print into some custom target, for example, a network socket:
Expand Down Expand Up @@ -335,6 +334,23 @@ mjson_printf(&mjson_print_dynamic_buf, &s, "{%Q:%d, %Q:%M}", "a", 1, "b", m_prin
free(s);
```

## mjson_snprintf()

```c
int mjson_snprintf(char *buf, size_t len, const char *fmt, ...);
```
A convenience function that prints into a given string.
## mjson_aprintf()
```c
char *mjson_aprintf(const char *fmt, ...);
```

A convenience function that prints into an allocated string. A returned
pointer must be `free()`-ed by a caller.

## mjson_pretty()

```c
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=mjson
version=1.2.2
version=1.2.3
author=Cesanta Software Limited <[email protected]>
maintainer=Cesanta Software Limited <[email protected]>
sentence=JSON parser, emitter, and JSON-RPC engine
Expand Down
18 changes: 18 additions & 0 deletions mjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,24 @@ int mjson_print_dynamic_buf(const char *ptr, int len, void *fndata) {
}
}

int mjson_snprintf(char *buf, size_t len, const char *fmt, ...) {
va_list ap;
struct mjson_fixedbuf fb = {buf, (int) len, 0};
va_start(ap, fmt);
mjson_vprintf(mjson_print_fixed_buf, &fb, fmt, &ap);
va_end(ap);
return fb.len;
}

char *mjson_aprintf(const char *fmt, ...) {
va_list ap;
char *result = NULL;
va_start(ap, fmt);
mjson_vprintf(mjson_print_dynamic_buf, &result, fmt, &ap);
va_end(ap);
return result;
}

int mjson_print_null(const char *ptr, int len, void *userdata) {
(void) ptr;
(void) userdata;
Expand Down
3 changes: 3 additions & 0 deletions mjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ int mjson_print_null(const char *ptr, int len, void *userdata);
int mjson_print_fixed_buf(const char *ptr, int len, void *userdata);
int mjson_print_dynamic_buf(const char *ptr, int len, void *userdata);

int mjson_snprintf(char *buf, size_t len, const char *fmt, ...);
char *mjson_aprintf(const char *fmt, ...);

#if MJSON_ENABLE_PRETTY
int mjson_pretty(const char *, int, const char *, mjson_print_fn_t, void *);
#endif
Expand Down
10 changes: 10 additions & 0 deletions test/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,16 @@ static void test_printf(void) {
free(s);
}

{
char buf[100], *s = mjson_aprintf("[%d]", 123);
int n = mjson_snprintf(buf, sizeof(buf), "{%g}", 1.23);
ASSERT(s != NULL);
ASSERT(n == 6);
ASSERT(strcmp(s, "[123]") == 0);
ASSERT(strcmp(buf, "{1.23}") == 0);
free(s);
}

{
char s[] = "0\n\xfeg";
struct mjson_fixedbuf fb = {tmp, sizeof(tmp), 0};
Expand Down

0 comments on commit a01a2e5

Please sign in to comment.