From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 10025 invoked from network); 18 Nov 2022 14:39:14 -0000 Received: from 9front.inri.net (168.235.81.73) by inbox.vuxu.org with ESMTPUTF8; 18 Nov 2022 14:39:14 -0000 Received: from driusan.net ([207.148.18.58]) by 9front; Fri Nov 18 09:37:37 -0500 2022 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=driusan.net; s=20180128; h=From:Date:Subject:To; bh=R3gvE0vjaxr3vIl6PTiFOBVdDjuZlRMvrViQdPAUjJQ=; b=QhTW42MlTzwZsPd11za95iHgCSHWoGkLCWba/jXd5WDe7Ggi6RnNnSHOi+TC0Iu8lMtvE7u6JXhXEzmS5K5q6NxsSsPqebs/QSUqORfFRjqH7DZ93316J2enE6UtYMZ3I0xTb6to86jEw0uypBqm33atCzBsM+6CEGeaFP0iRp4PIdcgtkHtQzp2EzB+Bs1Q/Co0d9U3F3w7jzkTg1Cqa0SF/f/zqqzUpzM5qI46JDMlCDZI4iPKfv0yn0X1Ihoyq+kWpHbrq7htsJkFCixYmENfvYnQqmEH2G9T/BUpcCjeg5o58LmCLW9j1phvunxwpllX/MgX6sYMbWjgaxCGPA== Message-ID: <570432963688607A01A28D3D1092E22D@driusan.net> From: "Dave MacFarlane" Date: Fri, 18 Nov 2022 09:37:36 -0500 To: 9front@9front.org In-Reply-To: <4F579BAA82A5A3386F286262ED2A7806@eigenstate.org> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit List-ID: <9front.9front.org> List-Help: X-Glyph: ➈ X-Bullshit: shared property-scale factory-based framework Subject: Re: [9front] libjson can print invalid json Reply-To: 9front@9front.org Precedence: bulk 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 #include #include 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(""); }