Skip to content

Commit 9891a14

Browse files
committed
JSON fixes for '\/'
1 parent 0bfc26b commit 9891a14

File tree

6 files changed

+38
-5
lines changed

6 files changed

+38
-5
lines changed

convex-core/src/main/java/convex/core/data/Strings.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public class Strings {
9292

9393
public static final StringShort SYMBOL = StringShort.intern("symbol");
9494

95+
public static final StringShort SOURCE = StringShort.intern("source");
96+
97+
9598
public static final Comparator<AString> lengthComparator = (a,b)->{
9699
return Long.signum(a.count()-b.count());
97100
};
@@ -100,6 +103,7 @@ public class Strings {
100103

101104

102105

106+
103107
/**
104108
* Reads a String from a Blob encoding.
105109
*

convex-core/src/main/java/convex/core/json/JSONReader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ public static AMap<AString,ACell> readObject(java.io.Reader r) throws IOExceptio
160160
}
161161

162162
public static AMap<AString,ACell> readObject(InputStream is) throws IOException {
163-
return readObject(CharStreams.fromStream(is));
163+
CharStream cs=CharStreams.fromStream(is);
164+
return readObject(cs);
164165
}
165166

166167
@SuppressWarnings("unchecked")

convex-core/src/main/java/convex/core/text/Text.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ public static String unescapeJava(String st) {
263263
case '\'':
264264
ch = '\'';
265265
break;
266+
case '/': // this is for JSON compatibility, a bit permissive in Java?
267+
ch = '/';
268+
break;
266269
// Hex Unicode: u????
267270
case 'u':
268271
if (i+6 > n) {

convex-core/src/test/java/convex/core/util/JSONTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public void testEscape() {
126126
@Test
127127
public void testPrettyJSON() {
128128
assertEquals("{\n \"foo\": \"bar\"\n}",JSON.printPretty(Maps.of("foo","bar")).toString());
129+
130+
// special case regression test
131+
assertEquals("\"\\\\/\"",JSON.toStringPretty(Strings.create("\\/")));
132+
assertEquals(Strings.create("\"\\\\/\""),JSON.printPretty(Strings.create("\\/")));
133+
129134
}
130135

131136
@Test
@@ -170,6 +175,11 @@ public void testJSONObjects() {
170175

171176
@Test
172177
public void testStrings() {
178+
assertEquals(Strings.intern("\n"),JSONReader.read("\"\\n\""));
179+
180+
// '\/' is valid JSON, you can unnecessarily escape a slash. WTF JSON?
181+
assertEquals(Strings.intern("/"),JSONReader.read("\"\\/\""));
182+
173183
assertEquals("\\",JSON.parse("\"\\\\\"").toString()); // i.e. '\'
174184

175185
doStringTest("");

convex-restapi/src/main/java/convex/restapi/api/ChainAPI.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,12 +1082,12 @@ public void query(Context ctx) throws InterruptedException {
10821082
addr=Address.parse(RT.get(req, Keywords.ADDRESS));
10831083
form=RT.get(req, Keywords.SOURCE);
10841084
} else {
1085-
Map<String, Object> req = getJSONBody(ctx);
1085+
AMap<AString, ACell> req = readJSONBody(ctx);
10861086
// System.out.println("query data: "+req+ " of type "+Utils.getClassName(req));
1087-
addr = Address.parse(req.get("address"));
1088-
Object srcValue = req.get("source");
1087+
addr = Address.parse(req.get(Strings.ADDRESS));
1088+
AString srcValue = RT.ensureString(req.get(Strings.SOURCE));
10891089
// System.out.println("query source: "+srcValue);
1090-
form = readCode(srcValue);
1090+
form = Reader.read(srcValue);
10911091
}
10921092

10931093
Result r = convex.querySync(form, addr);

convex-restapi/src/test/java/convex/restapi/test/RESTAPITest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ private void assertCad3RoundTrip(String cvxLiteral, String expectedHex) throws I
160160
}
161161

162162
@Test public void testQuery() throws IOException, InterruptedException {
163+
// { // Edge case with \/ in JSON
164+
// String query=JSON.toStringPretty(Maps.of("address",11,"source","#8\\/count"));
165+
// System.out.println("testQuery:"+query);
166+
// HttpResponse<String> res = post(API_PATH+"/query", query);
167+
// String r=res.body();
168+
// System.out.println(r);
169+
// AMap<AString,ACell> json=JSON.parse(r);
170+
// ACell value=json.getIn("value");
171+
//
172+
// assertEquals(200, res.statusCode());
173+
// assertTrue(value instanceof AString);
174+
// assertNull(json.getIn("errorCode"));
175+
// }
176+
177+
163178
{ // should be a bad request with bad JSON
164179
HttpResponse<String> res = post(API_PATH+"/query", "fddfgb");
165180
assertEquals(400, res.statusCode());

0 commit comments

Comments
 (0)