stleary / JSON-java

A reference implementation of a JSON package in Java.
http://stleary.github.io/JSON-java/index.html
Other
4.54k stars 2.56k forks source link

Implement strict mode for JSONObject #884

Closed stleary closed 1 week ago

stleary commented 7 months ago

Strict mode now works for JSONArray (see #877), but not for JSONObject or for JSONArrays embedded in JSONObjects. For example, the following code parses without error:

        String str = "{\"a\": [b]}";
        JSONObject jsonObject = new JSONObject(str, new JSONParserConfiguration().withStrictMode(true));

Several additional changes should be included with the PR:

rikkarth commented 6 months ago

No longer applies. As of latest merge https://github.com/stleary/JSON-java/pull/888

    @Test
    public void test(){
        String str = "{\"a\": [b]}";
        JSONObject jsonObject = new JSONObject(str, new JSONParserConfiguration().withStrictMode(true));
    }

output

org.json.JSONException: Value 'b' is not surrounded by quotes at 8 [character 9 line 1]

    at org.json.JSONTokener.syntaxError(JSONTokener.java:624)
    at org.json.JSONTokener.getValidNumberBooleanOrNullFromObject(JSONTokener.java:577)
    at org.json.JSONTokener.parsedUnquotedText(JSONTokener.java:569)
    at org.json.JSONTokener.nextSimpleValue(JSONTokener.java:539)
    at org.json.JSONTokener.nextValue(JSONTokener.java:483)
    at org.json.JSONArray.parseTokener(JSONArray.java:161)
    at org.json.JSONArray.<init>(JSONArray.java:106)
    at org.json.JSONTokener.nextValue(JSONTokener.java:478)
    at org.json.JSONObject.<init>(JSONObject.java:247)
    at org.json.JSONObject.<init>(JSONObject.java:453)
    at org.json.junit.JSONObjectTest.test(JSONObjectTest.java:3852)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)

Process finished with exit code -1
stleary commented 5 months ago

@rikkarth, Try this code:

    @Test
    public void foo() {
        JSONObject jsonObject = new JSONObject("{}abc", new JSONParserConfiguration().withStrictMode(true));
        JSONArray jsonArray = new JSONArray("[]abc", new JSONParserConfiguration().withStrictMode(true));
    }

The first test passes, but should not. The second test correctly detects the invalid chars at the end of the input. This happened because no work was done to implement invalid trailing char checking for JSONObject. We don't know whether other tests may fail because there are no unit tests for top level JSONObjects in strict mode.

rikkarth commented 3 months ago

Will address this starting next week.

stleary commented 1 week ago

Closing due to revert of strict mode. Will be addressed in a future commit.