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

No Exceptions Occur with Invalid JSON String #5

Closed
jredfox opened this issue Mar 7, 2019 · 4 comments
Closed

No Exceptions Occur with Invalid JSON String #5

jredfox opened this issue Mar 7, 2019 · 4 comments

Comments

@jredfox
Copy link

jredfox commented Mar 7, 2019

Original issue found here and still persists in current library:
fangyidong/json-simple#16

Test code no exceptions occur when it should occur:
https://pastebin.com/nq0CfHjX

Test 2 this is an invalid json string due to a dupe key when parsing. It''s fine if you want to replace a key using JSONObject.pu() in memory but, when parsing it from a file/invalid sting if they key already exists in that file/string then it is no longer acceptable

JSONParser parser = new JSONParser();
System.out.println("Done:" + parser.parse("{\"key\":1 \"key\":2}"));
@RalleYTN
Copy link
Owner

I actually think it is better to be liberal with input and conservative with the output. Is there any case in which it is necessary to actually check for this? I can't think of any practical cases.

@jredfox
Copy link
Author

jredfox commented Mar 13, 2019

no read the code there are no commas and it was still parsing

@RalleYTN RalleYTN reopened this Nov 27, 2022
@RalleYTN
Copy link
Owner

I will try to add a new method in the JSONParser which will validate JSON according to format definition.

@RalleYTN
Copy link
Owner

RalleYTN commented Dec 5, 2022

Fixed with newest commit.
By adding true as a second parameter to parse you will enable strict mode which will validate the existence of colons and commas. Adiitionally JSONObject and JSONArray now have new constructors that allow parsing with strict mode.

This code will now throw a JSONParseException:

JSONParser parser = new JSONParser();
System.out.println("Done:" + parser.parse("{\"key\":1 \"key\":2}", true));

@RalleYTN RalleYTN closed this as completed Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants