Skip to content

Commit 439cf7f

Browse files
committed
Allow comments in JSON parsing
1 parent 01a3743 commit 439cf7f

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

convex-core/src/main/antlr4/convex/core/json/reader/antlr/JSON.g4

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ fragment EXP
8080
: [Ee] [+-]? [0-9]+
8181
;
8282

83+
// Multi-line comments (ignored)
84+
MULTILINE_COMMENT
85+
: '/*' .* '*/' -> skip
86+
;
87+
88+
// Single-line comments (ignored)
89+
SINGLELINE_COMMENT
90+
: '//' ~[\r\n]* -> skip
91+
;
92+
8393
WS
8494
: [ \t\n\r]+ -> skip
8595
;

convex-core/src/test/java/convex/core/utils/JSONUtilsTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ public void testJSON() {
122122
assertEquals(Maps.of("[\"\" 3]",4), RT.cvm(JSONUtils.json(Maps.of(Vectors.of("",3),4))));
123123
}
124124

125+
@Test
126+
public void testJSONComments() {
127+
assertEquals(Vectors.of(true,null),JSONUtils.parse("[true, /* \n */ null]"));
128+
assertEquals(RT.cvm(12),JSONUtils.parse("12 //foo"));
129+
assertEquals(RT.cvm(12),JSONUtils.parse("//foo \n 12"));
130+
assertEquals(RT.cvm(12),JSONUtils.parse("//foo /* \n 12"));
131+
132+
assertThrows(ParseException.class,()->JSONUtils.parse("/* 67")); // comment not closed
133+
assertThrows(ParseException.class,()->JSONUtils.parse("//")); // no value
134+
135+
}
136+
125137
@Test
126138
public void testJSONRoundTrips() {
127139

convex-java/src/main/java/convex/java/JSON.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.Map;
1010

1111
import org.json.simple.JSONObject;
12-
import org.json.simple.JSONValue;
1312
import org.json.simple.parser.JSONParser;
1413
import org.json.simple.parser.ParseException;
1514

0 commit comments

Comments
 (0)