9front - general discussion about 9front
 help / color / mirror / Atom feed
From: "Dave MacFarlane" <driusan@driusan.net>
To: 9front@9front.org
Subject: Re: [9front] libjson can print invalid json
Date: Fri, 18 Nov 2022 09:37:36 -0500	[thread overview]
Message-ID: <570432963688607A01A28D3D1092E22D@driusan.net> (raw)
In-Reply-To: <4F579BAA82A5A3386F286262ED2A7806@eigenstate.org>

It certainly looks more elegant than my code, but when I tested it it's missing the
beginning and ending quote character of the json string.

After I added them, it passed the tests I wrote for my code.

static int
printstring(Fmt *f, char *s)
{
	int n;
	
	n = fmtrune(f, '"');
	for(; *s; s++){
		switch(*s){
		case '\\':	n += fmtstrcpy(f, "\\\\");	break;
		case '\f':	n += fmtstrcpy(f, "\\f");	break;
		case '\b':	n += fmtstrcpy(f, "\\b");	break;
		case '\n':	n += fmtstrcpy(f, "\\n");	break;
		case '\r':	n += fmtstrcpy(f, "\\r");	break;
		case '\"':	n += fmtstrcpy(f, "\\\"");	break;
		default:
			if(*s < 0x20)
				n += fmtprint(f, "\\u%04x", *s);
			else
				n += fmtrune(f, *s);
		}
	}
	n = fmtrune(f, '"');
	return n;
}

Here's what I used to test it:

#include <u.h>
#include <libc.h>
#include <json.h>

void testJSON(char *s) {
	print("%s\n", s);
	JSON *j = jsonparse(s);
	print("%J\n", j);
	char *str = smprint("%J", j);
	assert(jsonparse(str) != nil);
	free(j);
	free(str);
}
void main(void) {
	JSON *j;
	char *str;

	JSONfmtinstall();

	testJSON("{\"abc\": \"\"}");
	testJSON("{\"abc\": \"hello\"}");
	testJSON("{\"abc\": \"\\\"hello\"}");
	testJSON("{\"abc\": \"hello\\\"\"}");
	testJSON("{\"abc\": \"he\\\"llo\"}");
	testJSON("{\"abc\": \"\\\"he\\\"llo\\\"\"}");
	testJSON("{\"abc\": \"hello\\t\"}");
	exits("");
}


  reply	other threads:[~2022-11-18 14:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-17 21:52 Dave MacFarlane
2022-11-18  4:30 ` ori
2022-11-18 14:37   ` Dave MacFarlane [this message]
2022-11-18 14:52     ` Dave MacFarlane

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=570432963688607A01A28D3D1092E22D@driusan.net \
    --to=driusan@driusan.net \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).