I need to check if a given input string is a valid json. Unfortunately, all the suggestions I found around the internet, that is to deserialize the given string by using Jackson, Gson, javax.json do not fail on extra characters at the end.
I need such check for a valid json to fail on following inputs
{"a": "b"}
foobar{}
or
{"a":"b"}
{"1":"2"}
or
{"a":"b"},
{"1":"2"}
all the deserializers unfortunately stop after the first closing bracket b"}
and basically validate that {"a":"b"}
is a valid JSON while omitting the extra characters.
Any idea how to identify the extra characters at the end of the JSON string, or how to enable strict parsing with common Java JSON libraries? Or maybe use completely different tool so that check passes on valid JSONs and fails on cases I'm listing above?
SOLUTION
As a matter of fact, Google GSON with setLenient(false)
(the default) happens to validate the input string exactly as I need. So doing
new Gson().fromJson(inputString, Object.class)
is the way to go.