Skip to content

Commit

Permalink
feat: new (debug) function json_printf_pretty
Browse files Browse the repository at this point in the history
Signed-off-by: Davide Madrisan <[email protected]>
  • Loading branch information
madrisan committed Mar 31, 2024
1 parent 370f3bb commit 06b5adc
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/json_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern "C"
int json_token_streq (const char *json, jsmntok_t * tok, const char *s);
char *json_token_tostr (char *json, jsmntok_t * t);
jsmntok_t *json_tokenise (const char *json, size_t *ntoken);
void json_printf_pretty (const char *json, char **dump, int indent);

#ifdef __cplusplus
}
Expand Down
82 changes: 80 additions & 2 deletions lib/json_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. */

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "jsmn.h"
#include "json_helpers.h"
#include "logging.h"
#include "system.h"
#include "xalloc.h"

int
json_token_streq (const char *json, jsmntok_t * tok, const char *s)
json_token_streq (const char *json, jsmntok_t *tok, const char *s)
{
if (tok->type == JSMN_STRING && (int) strlen (s) == tok->end - tok->start &&
strncmp (json + tok->start, s, tok->end - tok->start) == 0)
Expand All @@ -37,7 +39,7 @@ json_token_streq (const char *json, jsmntok_t * tok, const char *s)
}

char *
json_token_tostr (char *json, jsmntok_t * t)
json_token_tostr (char *json, jsmntok_t *t)
{
return xsubstrdup (json + t->start, t->end - t->start);
}
Expand Down Expand Up @@ -65,3 +67,79 @@ json_tokenise (const char *json, size_t *ntoken)
*ntoken = r;
return tokens;
}

static int
json_dump (const char *json, jsmntok_t *t, size_t count, int indent,
FILE *stream)
{
int i, j, k;

if (t->type == JSMN_PRIMITIVE)
{
fprintf (stream, "%.*s", t->end - t->start, json + t->start);
if (t->size == 0)
fprintf (stream, "\n");
return 1;
}
else if (t->type == JSMN_STRING)
{
fprintf (stream, "%.*s", t->end - t->start, json + t->start);
if (t->size == 0)
fprintf (stream, "\n");
return 1;
}
else if (t->type == JSMN_OBJECT)
{
j = 0;
jsmntok_t *token;

fprintf (stream, "\n");

for (i = 0; i < t->size; i++)
{
for (k = 0; k < indent; k++)
fprintf (stream, " ");

token = t + 1 + j;
j += json_dump (json, token, count - j, indent + 1, stream);

/* get the value of the previous JSON label */
if (token->size > 0)
{
fprintf (stream, ": ");
j += json_dump (json, t + 1 + j, count - j, indent + 1, stream);
}
}
return j + 1;
}
else if (t->type == JSMN_ARRAY)
{
j = 0;
fprintf (stream, "\n");

for (i = 0; i < t->size; i++)
{
for (k = 0; k < indent - 1; k++)
fprintf (stream, " ");

fprintf (stream, " - ");
j += json_dump (json, t + 1 + j, count - j, indent + 1, stream);
}

return j + 1;
}

return 0;
}

void
json_printf_pretty (const char *json, char **dump, int indent)
{
size_t ntoken, size;
FILE *stream = open_memstream (dump, &size);

jsmntok_t *tok = json_tokenise (json, &ntoken);
json_dump (json, tok, tok->size, indent, stream);

fclose (stream);
}

0 comments on commit 06b5adc

Please sign in to comment.