9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] str2json and jsonfmt
@ 2024-01-25  5:52 Xiao-Yong Jin
  2024-01-25  8:06 ` cinap_lenrek
  0 siblings, 1 reply; 13+ messages in thread
From: Xiao-Yong Jin @ 2024-01-25  5:52 UTC (permalink / raw)
  To: 9front

I wrote the following two json programs and found them helpful and
wanted to share with you.  Feel free to add it to the 9front
distribution if more people find them useful.

/* str2json.c */
#include <u.h>
#include <libc.h>
#include <json.h>

void
main(void)
{
	char *buf;
	long n;
	ulong m;
	struct JSON json;

	JSONfmtinstall();

	buf = nil;
	m = 0;
	for(;;){
		m += IOUNIT;
		buf = realloc(buf, m);
		if(!buf)
			sysfatal("realloc");
		n = readn(0, buf+m-IOUNIT, IOUNIT);
		if(n < IOUNIT){
			buf[m-IOUNIT+n] = '\0';
			break;
		}
	}

	json.t = JSONString;
	json.s = buf;

	print("%J", &json);

	free(buf);
	exits(nil);
}

/* jsonfmt.c */
#include <u.h>
#include <libc.h>
#include <json.h>

JSON*
jsonindex(JSON *j, vlong i)
{
	JSONEl *e;

	if(j->t != JSONArray){
		werrstr("not an array");
		return nil;
	}
	if(i < 0){
		werrstr("negative index");
		return nil;
	}
	for(e=j->first; e!=nil; e=e->next, --i)
		if(i==0)
			return e->val;
	werrstr("index out of bounds");
	return nil;
}

void
printjson(JSON *j, int raw)
{
	JSONEl *e;

	if(j->t == JSONString && raw)
		print("%s", j->s);
	else if(j->t == JSONArray && raw)
		for(e=j->first; e!=nil; e=e->next)
			print("%J\n", e->val);
	else
		print("%J\n", j);
}

void
usage(void)
{
	fprint(2, "usage: jsonfmt [-r] [names/indices...]\n");
	exits("usage");
}

void
main(int argc, char *argv[])
{
	char *buf;
	long n;
	ulong m;
	JSON *json, *obj;
	int i;
	int raw;

	JSONfmtinstall();

	buf = nil;
	m = 0;
	for(;;){
		m += IOUNIT;
		buf = realloc(buf, m);
		if(!buf)
			sysfatal("realloc");
		n = readn(0, buf+m-IOUNIT, IOUNIT);
		if(n < IOUNIT){
			buf[m-IOUNIT+n] = '\0';
			break;
		}
	}

	json = jsonparse(buf);
	if(!json)
		sysfatal("jsonfmt: %r");

	raw = 0;
	ARGBEGIN{
	case 'r':
		raw = 1;
		break;
	default:
		usage();
	}ARGEND

	if(argc == 0)
		printjson(json, raw);
	else for(i=0; i<argc; ++i){
		obj = nil;
		if(json->t==JSONObject)
			obj = jsonbyname(json, argv[i]);
		else if(json->t==JSONArray)
			obj = jsonindex(json, atoll(argv[i]));
		else
			werrstr("not a JSON array or object");
		if(obj)
			printjson(obj, raw);
		else
			sysfatal("jsonfmt: %r");
	}

	jsonfree(json);
	free(buf);
	exits(nil);
}


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2024-01-26 17:31 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-25  5:52 [9front] str2json and jsonfmt Xiao-Yong Jin
2024-01-25  8:06 ` cinap_lenrek
2024-01-25  8:57   ` phil9
2024-01-25  9:17     ` chris
2024-01-25 14:03       ` Alex Musolino
2024-01-25 13:42     ` Dave MacFarlane
2024-01-25 14:57       ` cinap_lenrek
2024-01-25 15:39         ` Dave MacFarlane
2024-01-25 16:16           ` Steve simon
2024-01-26  9:34             ` umbraticus
2024-01-25 14:19     ` hiro
2024-01-26 14:53     ` ori
2024-01-26 17:28       ` Steve Simon

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).