Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement strict mode for JSONObject #884

Open
stleary opened this issue Apr 7, 2024 · 1 comment
Open

Implement strict mode for JSONObject #884

stleary opened this issue Apr 7, 2024 · 1 comment

Comments

@stleary
Copy link
Owner

stleary commented Apr 7, 2024

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:

  • The indentation in nextString(char, boolean) is incorrect.
  • There is a compiler warning in JSONTokener that should be fixed: (fixed in add javadoc for strictmode #886)
Warning:  Javadoc Warnings
Warning:  /home/runner/work/JSON-java/JSON-java/src/main/java/org/json/JSONTokener.java:294: warning: no @param for strictMode
Warning:  public String nextString(char quote, boolean strictMode) throws JSONException {
Warning:  ^
Warning: [WARNING] 1 warning
@rikkarth
Copy link
Contributor

No longer applies. As of latest merge #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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants