Patchworkβ Fix invalid JSON output for NULL strings

login
register
about
Submitter Michal Sojka
Date 2010-03-14 20:44:18
Message ID <87ociqzl99.fsf@steelpick.localdomain>
Download mbox | patch
Permalink /patch/430/
State New
Headers show

Comments

Michal Sojka - 2010-03-14 20:44:18
On Sun, 14 Mar 2010 15:01:28 -0400, James Westby <jw+debian@jameswestby.net> wrote:
> On Sun, 14 Mar 2010 19:19:11 +0100, Michal Sojka <sojkam1@fel.cvut.cz> wrote:
> > Mails with empty body produced the following output:
> >   "body": [{"id": 1, "content-type": "text/plain", "content": (null)}]
> > The (null) is not valid JSON syntax.
> 
> Is this just something that can happen with the body?
> 
> I've see (null) in the emacs interface when I've done something silly
> such as opening a newer notmuch db with an old client.
> 
> Should all the attributes be guarded in a similar manner to ensure valid
> JSON?


You're right. When you import messages from mail archive such as
http://notmuchmail.org/pipermail/notmuch/, there are no To: headers.
This case is already handled by the current code, but what about this
more universal solution? It also solves my problem with notmuchsynch.

-- >8 --
Signed-off-by: Michal Sojka <sojkam1@fel.cvut.cz>
---
 json.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

Patch

diff --git a/json.c b/json.c
index 9614143..0885d3d 100644
--- a/json.c
+++ b/json.c
@@ -56,7 +56,7 @@  json_quote_str(const void *ctx, const char *str)
     int len = 0;
 
     if (!str)
-	return NULL;
+	return (char*)"null";
 
     for (ptr = str; *ptr; len++, ptr++) {
 	if (*ptr < 32 || *ptr == '\"' || *ptr == '\\')