From 0c800d44284c53fb32751c0adf85f9da64f50b93 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Thu, 23 Jul 2020 00:24:48 -0500 Subject: [PATCH] JSON5 in dbStatic: Update bare-word JSON keys Our bare-word character set is wider than JSON5's. Quote any keys containing the extra characters so YAJL can parse them, but don't quote keys unnecessarily. Tests for this behavior are in dbStaticTest.db Adjust the other tests that read links parsed by the dbStatic parser that used bareword keys, which are no longer quoted. --- modules/database/src/ioc/dbStatic/dbYacc.y | 17 ++++++++++++++--- modules/database/test/ioc/db/dbPutLinkTest.c | 4 ++-- modules/database/test/ioc/db/dbStaticTest.db | 3 +++ .../test/std/rec/linkRetargetLinkTest.c | 6 +++--- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/modules/database/src/ioc/dbStatic/dbYacc.y b/modules/database/src/ioc/dbStatic/dbYacc.y index 80b031fac..cc563bcfd 100644 --- a/modules/database/src/ioc/dbStatic/dbYacc.y +++ b/modules/database/src/ioc/dbStatic/dbYacc.y @@ -32,8 +32,8 @@ static int yyAbort = 0; %token jsonNULL jsonTRUE jsonFALSE %token jsonNUMBER jsonSTRING jsonBARE -%type json_value json_object json_array -%type json_members json_pair json_elements json_string +%type json_value json_string json_object json_array +%type json_members json_pair json_key json_elements %% @@ -299,13 +299,24 @@ json_members: json_pair if (dbStaticDebug>2) printf("json %s\n", $$); }; -json_pair: json_string ':' json_value +json_pair: json_key ':' json_value { $$ = dbmfStrcat3($1, ":", $3); dbmfFree($1); dbmfFree($3); if (dbStaticDebug>2) printf("json %s\n", $$); }; +json_key: jsonSTRING + | jsonBARE +{ + /* A key containing any of these characters must be quoted for YAJL */ + if (strcspn($1, "+-.") < strlen($1)) { + $$ = dbmfStrcat3("\"", $1, "\""); + dbmfFree($1); + } + if (dbStaticDebug>2) printf("json %s\n", $$); +}; + json_string: jsonSTRING | jsonBARE { diff --git a/modules/database/test/ioc/db/dbPutLinkTest.c b/modules/database/test/ioc/db/dbPutLinkTest.c index 5158e3c91..830d37f6c 100644 --- a/modules/database/test/ioc/db/dbPutLinkTest.c +++ b/modules/database/test/ioc/db/dbPutLinkTest.c @@ -255,7 +255,7 @@ typedef struct { } testHWDataT; static const testHWDataT testHWData[] = { - {"rJSON_LINK", JSON_LINK, "{\"x\":true}", {0}, "{\"x\":true}"}, + {"rJSON_LINK", JSON_LINK, "{x:true}", {0}, "{x:true}"}, {"rVME_IO", VME_IO, "#C100 S101 @parm VME_IO", {100, 101}, "parm VME_IO"}, {"rCAMAC_IO", CAMAC_IO, "#B11 C12 N13 A14 F15 @parm CAMAC_IO", {11, 12, 13, 14, 15}, "parm CAMAC_IO"}, {"rAB_IO", AB_IO, "#L21 A22 C23 S24 @parm AB_IO", {21, 22, 23, 24}, "parm AB_IO"}, @@ -585,7 +585,7 @@ void testJLink(void) testdbPutFieldOk("j2.PROC", DBF_LONG, 1); testdbPutFieldOk("j3.PROC", DBF_LONG, 1); - testdbGetFieldEqual("j1.INP", DBF_STRING, "{\"z\":{\"good\":1}}"); + testdbGetFieldEqual("j1.INP", DBF_STRING, "{z:{good:1}}"); testdbGetFieldEqual("j1.VAL", DBF_LONG, 1); testdbGetFieldEqual("j2.VAL", DBF_LONG, 2); testdbGetFieldEqual("j3.VAL", DBF_LONG, 3); diff --git a/modules/database/test/ioc/db/dbStaticTest.db b/modules/database/test/ioc/db/dbStaticTest.db index 75e981f1a..bc25d1a23 100644 --- a/modules/database/test/ioc/db/dbStaticTest.db +++ b/modules/database/test/ioc/db/dbStaticTest.db @@ -81,4 +81,7 @@ record(x, "t1") { field(F64, -Infinity) field(F32, Nan) field(F64, Nan) + + info(i1, {x:0, +x:1, -x:2, .x:3}) + info(i2, Bare-word_string) } diff --git a/modules/database/test/std/rec/linkRetargetLinkTest.c b/modules/database/test/std/rec/linkRetargetLinkTest.c index bf98bc1f9..dab000167 100644 --- a/modules/database/test/std/rec/linkRetargetLinkTest.c +++ b/modules/database/test/std/rec/linkRetargetLinkTest.c @@ -75,18 +75,18 @@ static void testRetargetJLink(void) testdbGetFieldEqual("rec:j1", DBF_DOUBLE, 10.0); /* minimal args */ - testLongStrEq("rec:j1.INP$", "{\"calc\":{\"expr\":\"A+5\",\"args\":5}}"); + testLongStrEq("rec:j1.INP$", "{calc:{expr:\"A+5\",args:5}}"); /* with [] */ testPutLongStr("rec:j1.INP$", "{\"calc\":{\"expr\":\"A+5\",\"args\":[7]}}"); testdbPutFieldOk("rec:j1.PROC", DBF_LONG, 1); /* with const */ - testPutLongStr("rec:j1.INP$", "{\"calc\":{\"expr\":\"A+5\",\"args\":[{\"const\":7}]}}"); + testPutLongStr("rec:j1.INP$", "{calc:{expr:\"A+5\",args:[{const:7}]}}"); testdbPutFieldOk("rec:j1.PROC", DBF_LONG, 1); testdbGetFieldEqual("rec:j1", DBF_DOUBLE, 12.0); - testLongStrEq("rec:j1.INP$", "{\"calc\":{\"expr\":\"A+5\",\"args\":[{\"const\":7}]}}"); + testLongStrEq("rec:j1.INP$", "{calc:{expr:\"A+5\",args:[{const:7}]}}"); } MAIN(linkRetargetLinkTest)