Reputation: 123
I'm parsing a Json file which should be formatted according to json's formal specification, which doesn't include the possibility of wrapping keys and values in single quotes ('). However, using either org.json
or gson
, the implementation is accepting those entries and successfully converting the file to a JsonObject
. I need that to fail (there is a second validation in my flow which doesn't allow single quotes).
I've been looking into ways of configuring the parsers so that they fail. I've read that Jackson
's JsonParser
doesn't allow that by default, having then the possibility to explicitly change that. Could I do the same with one of the previously mentioned libraries, or should I use Jackson
instead?
Thank you!
Upvotes: 2
Views: 72
Reputation: 159086
org.json
is open-source, so you can build a modified version that doesn't allow single-quoted string literals.
In the JSONTokener
class, modify the nextValue()
method.
public Object nextValue() throws JSONException {
char c = this.nextClean();
String string;
switch (c) {
case '"':
case '\'': // ← ← ← ← ← ← ← ← ← ← ← ← REMOVE OR COMMENT THIS LINE
return this.nextString(c);
case '{':
this.back();
return new JSONObject(this);
case '[':
this.back();
return new JSONArray(this);
}
...
Upvotes: 4