From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <9front-bounces@9front.inri.net> X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: from 9front.inri.net (9front.inri.net [168.235.81.73]) by inbox.vuxu.org (Postfix) with ESMTP id E4A4B271DA for ; Thu, 25 Jan 2024 06:54:40 +0100 (CET) Received: from mail-qk1-f172.google.com ([209.85.222.172]) by 9front; Thu Jan 25 00:53:11 -0500 2024 Received: by mail-qk1-f172.google.com with SMTP id af79cd13be357-7839ece697bso38266985a.0 for <9front@9front.org>; Wed, 24 Jan 2024 21:53:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706161988; x=1706766788; darn=9front.org; h=to:date:message-id:subject:mime-version:content-transfer-encoding :from:from:to:cc:subject:date:message-id:reply-to; bh=34OU0BkwF1vH2ZjV47uLM43NCjOnPXjZEvXsxsfwok4=; b=CaMZyqKqltaIQLMnXQgBZh0ahT8wPnKpcdaBXcFfmyNFzDyxvcTPkO5qiuNUUY2dYn gzoQFP8a7t41uOjakmwGKFunFhpSsinAsoMbJ9Pz3sCmm3bG6MDrQdjxtuG87awH0He4 RgtldtuXdDA5kUo9UTpYkR/aQZE3FcCVzXWYhIu7S9OzrAkGIBAhvlt5s0RMRHSvexBI X+Dh0I9bnSi4sYVPe21ljmboXzt1C9vsNVL6iRncR1IuFYyTfMkUTCXfPrXKZJoyCtM8 yu9j7SNTcU9RiF5Hf5P28/2Y8Evq5dGpiS28C3r0lrmm0ADRnoBPEfiAEOYB25wrorK6 x+NA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706161988; x=1706766788; h=to:date:message-id:subject:mime-version:content-transfer-encoding :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=34OU0BkwF1vH2ZjV47uLM43NCjOnPXjZEvXsxsfwok4=; b=st9bmJxoqNwGKLZzCB8WM4+aukr2d17BlTFF6OIyhzvxzloJ9isQ3TkvrnAdY4W5dg dAi5g/g4NxFYs+BZnHcbM/mJ548IJtofLCwynZZ2rnUtjDUPA8GIwX/LKAYwTdvblyty w6Usb0xVOy/w+b5ab7B5ry4D+uHxevXQvEeEP+DOh7chYL6OkPF0kSYc5J7mp7CkokTD j0E0Ie/gXzQRyyWcBgiC7YAIeVDG2yCy3xgMuDFjh8GOG2ZYW5UHdS5gQbPSqTX83Fv7 LrnMzt2Vj6MfHlFH1FudKbQ8eWHRIUt1IkkyT1ZWV12GzthUFgyZS7GEallbfUuDk7kN ST+A== X-Gm-Message-State: AOJu0Yxqb36gryhyEc2utTAfHqI3PtzoLPJpLBf3+eAuH2YNJ8/mvmew CQ96Q3a4Q4+aqkoeLfSprMWJ/75ikJZvizXZD1lE+zkHQwU4A9qRDfDJPqpe X-Google-Smtp-Source: AGHT+IEKVC/C+fs3QBXe7H8HffrqbFLBjTHWfjRmQFAqjRuS/2KO4impFYsisIFodM4ARz8qDTJNig== X-Received: by 2002:a05:620a:2282:b0:783:b33e:9823 with SMTP id o2-20020a05620a228200b00783b33e9823mr560992qkh.47.1706161988009; Wed, 24 Jan 2024 21:53:08 -0800 (PST) Received: from smtpclient.apple (c-73-210-224-225.hsd1.il.comcast.net. [73.210.224.225]) by smtp.gmail.com with ESMTPSA id s7-20020a05620a080700b007834b5042edsm4718132qks.106.2024.01.24.21.53.07 for <9front@9front.org> (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Jan 2024 21:53:07 -0800 (PST) From: Xiao-Yong Jin Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.400.31\)) Message-Id: <84AE50CF-6F43-477C-9044-A8A921F964A0@gmail.com> Date: Wed, 24 Jan 2024 23:52:56 -0600 To: 9front@9front.org X-Mailer: Apple Mail (2.3774.400.31) List-ID: <9front.9front.org> List-Help: X-Glyph: ➈ X-Bullshit: proven optimized GPU plugin reduce/map-scale manager Subject: [9front] str2json and jsonfmt Reply-To: 9front@9front.org Precedence: bulk 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 #include #include 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 #include #include 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; it==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); }