From c69e595e4e45a3a6b3e5e8b96c19e0820e8ad484 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Wed, 5 Feb 2014 22:32:18 -0800 Subject: [PATCH] Do escaping in output strings --- jsoncat.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/jsoncat.c b/jsoncat.c index bda444d..5e614ff 100644 --- a/jsoncat.c +++ b/jsoncat.c @@ -16,7 +16,20 @@ void json_print(json_object *j, int depth) { if (j == NULL) { printf("NULL"); } else if (j->type == JSON_STRING) { - printf("\"%s\"", j->string); + printf("\""); + + char *cp; + for (cp = j->string; *cp != '\0'; cp++) { + if (*cp == '\\' || *cp == '"') { + printf("\\%c", *cp); + } else if (*cp >= 0 && *cp < ' ') { + printf("\\u%04x", *cp); + } else { + putchar(*cp); + } + } + + printf("\""); } else if (j->type == JSON_NUMBER) { printf("%f", j->number); } else if (j->type == JSON_NULL) {