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.2 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED autolearn=no autolearn_force=no version=3.4.4 Received: (qmail 22215 invoked from network); 17 Nov 2022 21:54:50 -0000 Received: from 9front.inri.net (168.235.81.73) by inbox.vuxu.org with ESMTPUTF8; 17 Nov 2022 21:54:50 -0000 Received: from driusan.net ([207.148.18.58]) by 9front; Thu Nov 17 16:52:47 -0500 2022 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=driusan.net; s=20180128; h=From:Date:Subject:To; bh=C+paoIpeVMFhzVnt+uJkhHmZTPrdrCY6lvgqXg/0TAY=; b=U6lLr/NKsBMnB99l6bd95R8XKeB4F3/FmGKIRldGQa3cVjLE5P9MTSJCX2LLzATLduvsoxhWGkJvMvaiharHht+Ho7dPW7621/YVR1DBle+8w56C9Txa0E05am4PTQyU0iiwm1lKPKTge8gkGVW8Nx35mSYoo7CyT/e/+TxipvkX1citXkyQFLBYG+QOshEtqjrdkfeCSfKOIhAflXLVy9Ifv4Tb5amiVPbqESw4sqrc7yV5NVPFFQK7f8Og0HtFBsLbjauc2DIWwDbViwQqeCWd2hGKcIghtkg7JstYbJDbKW9lEG2tL2TRbq6zCAdLHfGbRGBrGLwksI3d79uT3A== Message-ID: <923270033661C2AB404C7473549B4808@driusan.net> From: "Dave MacFarlane" Date: Thu, 17 Nov 2022 16:52:46 -0500 To: 9front@9front.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: reduce/map-based API-based core optimizer Subject: [9front] libjson can print invalid json Reply-To: 9front@9front.org Precedence: bulk print("%J", json); can print invalid json if a JSON string in the object has a quotation mark in it. I just threw together this patch to try and escape the strings from printjson() if anyone's interested in it. diff 30c5296f32b87d83529d772732726891e1261c9c uncommitted --- a/sys/src/libjson/printjson.c +++ b/sys/src/libjson/printjson.c @@ -52,6 +52,38 @@ } static int +printstring(Fmt *f, char *s) +{ + int slen = strlen(s); + int i, nq=0; + int r; + char *dup, lastq; + for(i = 0; i < slen; i++) + if (s[i] == '"') nq++; + + if (nq == 0) + return fmtprint(f, "\"%s\"", s); + + r = fmtprint(f, "\""); + + dup = strdup(s); + lastq = -1; + for(i = 0; i < slen; i++){ + if(dup[i] == '"') { + dup[i] = 0; + r += fmtprint(f, "%s\\\"", &dup[lastq+1]); + lastq = i; + } + } + + if (lastq != slen-1) + r += fmtprint(f, "%s", &dup[lastq+1]); + r += fmtprint(f, "\""); + free(dup); + return r; +} + +static int printjson(Fmt *f, JSON *j, int indent) { switch(j->t){ @@ -65,7 +97,7 @@ return fmtprint(f, "%f", j->n); break; case JSONString: - return fmtprint(f, "\"%s\"", j->s); + return printstring(f, j->s); break; case JSONArray: return printarray(f, j, indent+1);