Skip to content

Commit d807492

Browse files
committed
updating JSON grammar based on Antlr V4 reference grammar to properly handle escaped newlines
1 parent fa0f391 commit d807492

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

core/src/main/antlr4/oracle/weblogic/deploy/json/JSON.g4

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ value
3939

4040

4141
STRING
42-
: '"' (ESC | ~ ["\\])* '"'
42+
: '"' (ESC | SAFECODEPOINT)* '"'
4343
;
4444
fragment ESC
4545
: '\\' (["\\/bfnrt] | UNICODE)
@@ -50,6 +50,9 @@ fragment UNICODE
5050
fragment HEX
5151
: [0-9a-fA-F]
5252
;
53+
fragment SAFECODEPOINT
54+
: ~ ["\\\u0000-\u001F]
55+
;
5356
NUMBER
5457
: '-'? INT ('.' [0-9] +)? EXP?
5558
;

core/src/test/java/oracle/weblogic/deploy/json/JsonTranslatorTest.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@
1111

1212
import org.junit.jupiter.api.Test;
1313
import org.junit.jupiter.api.function.Executable;
14+
import org.python.core.PyDictionary;
1415

1516
import static java.nio.charset.StandardCharsets.UTF_8;
17+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1618
import static org.junit.jupiter.api.Assertions.assertThrows;
1719

18-
public class JsonTranslatorTest {
20+
class JsonTranslatorTest {
1921

2022
/**
2123
* Verify that a lexical error will throw an Exception
2224
*/
2325
@Test
24-
public void testLexicalError() {
26+
void testLexicalError() {
2527
// Temporarily disable logging for this test. Intended lexical failure will log a stack trace.
2628
Logger logger = Logger.getLogger("wlsdeploy.json");
2729
Level originalLevel = logger.getLevel();
@@ -40,4 +42,36 @@ public void execute() throws Throwable {
4042

4143
logger.setLevel(originalLevel);
4244
}
45+
46+
@Test
47+
void testMultilineStringsError() {
48+
Logger logger = Logger.getLogger("wlsdeploy.json");
49+
Level originalLevel = logger.getLevel();
50+
logger.setLevel(Level.OFF);
51+
52+
String text = "{ \"abc\": \"xyz\n123\" }";
53+
InputStream stream = new ByteArrayInputStream(text.getBytes(UTF_8));
54+
final JsonStreamTranslator translator = new JsonStreamTranslator("String", stream);
55+
assertThrows(JsonException.class, new Executable() {
56+
@Override
57+
public void execute() throws Throwable {
58+
translator.parse();
59+
}
60+
}, "Test must raise JsonException when model has a value with a newline in it");
61+
logger.setLevel(originalLevel);
62+
}
63+
64+
@Test
65+
void testEscapedMultilineStrings() throws Exception {
66+
Logger logger = Logger.getLogger("wlsdeploy.json");
67+
Level originalLevel = logger.getLevel();
68+
logger.setLevel(Level.OFF);
69+
70+
String text = "{ \"abc\": \"xyz\\n123\" }";
71+
InputStream stream = new ByteArrayInputStream(text.getBytes(UTF_8));
72+
final JsonStreamTranslator translator = new JsonStreamTranslator("String", stream);
73+
PyDictionary actual = translator.parse();
74+
assertNotNull(actual, "Escaped Multiline String should parse correctly");
75+
logger.setLevel(originalLevel);
76+
}
4377
}

0 commit comments

Comments
 (0)