[BAEL-5553] Check whether a string is valid JSON in Java (#12136)

* [BAEL-5553] Check whether a string is valid JSON in Java

* [BAEL-5553] Move implementation to json-2 package

* [BAEL-5553] Move mapper creation outside method
This commit is contained in:
Dmytry Kovalenko 2022-05-02 19:11:24 +03:00 committed by GitHub
parent 0babebe608
commit c2cceb1d7e
7 changed files with 185 additions and 0 deletions

View File

@ -14,6 +14,11 @@
</parent> </parent>
<dependencies> <dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json.version}</version>
</dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
@ -148,6 +153,7 @@
<jsoniter.version>0.9.23</jsoniter.version> <jsoniter.version>0.9.23</jsoniter.version>
<moshi.version>1.9.2</moshi.version> <moshi.version>1.9.2</moshi.version>
<fastjson.version>1.2.21</fastjson.version> <fastjson.version>1.2.21</fastjson.version>
<json.version>20211205</json.version>
</properties> </properties>
</project> </project>

View File

@ -0,0 +1,32 @@
package com.baeldung.jsonvalidation;
import java.io.IOException;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
public class GsonValidator {
final TypeAdapter<JsonElement> strictAdapter = new Gson().getAdapter(JsonElement.class);
public boolean isValid(String json) {
try {
JsonParser.parseString(json);
} catch (JsonSyntaxException e) {
return false;
}
return true;
}
public boolean isValidStrict(String json) {
try {
strictAdapter.fromJson(json);
} catch (JsonSyntaxException | IOException e) {
return false;
}
return true;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.jsonvalidation;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonValidator {
final ObjectMapper mapper = new ObjectMapper();
public boolean isValid(String json) {
try {
mapper.readTree(json);
} catch (JacksonException e) {
return false;
}
return true;
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.jsonvalidation;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonValidator {
public boolean isValidObject(String json) {
try {
new JSONObject(json);
} catch (JSONException e) {
return false;
}
return true;
}
public boolean isValidJson(String json) {
try {
new JSONObject(json);
} catch (JSONException e) {
try {
new JSONArray(json);
} catch (JSONException ne) {
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.jsonvalidation;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class GsonValidatorUnitTest {
private final GsonValidator validator = new GsonValidator();
@Test
public void givenValidObjectJson_whenValidatingNonStrict_thenValid() {
String json = "{\"email\": \"example@com\", \"name\": \"John\"}";
assertTrue(validator.isValid(json));
}
@Test
public void givenValidArrayJson_whenValidatingNonStrict_thenValid() {
String json = "[{\"email\": \"example@com\", \"name\": \"John\"},{\"email\": \"example1@com\", \"name\": \"Bob\"}]";
assertTrue(validator.isValid(json));
}
@Test
public void givenInvalidJson_whenValidatingNonStrict_thenValid() {
String json = "Invalid_Json";
assertTrue(validator.isValid(json));
}
@Test
public void givenInvalidJson_whenValidatingStrict_thenInvalid() {
String json = "Invalid_Json";
assertFalse(validator.isValidStrict(json));
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.jsonvalidation;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class JacksonValidatorUnitTest {
private final JacksonValidator validator = new JacksonValidator();
@Test
public void givenValidObjectJson_whenValidating_thenValid() {
String json = "{\"email\": \"example@com\", \"name\": \"John\"}";
assertTrue(validator.isValid(json));
}
@Test
public void givenValidArrayJson_whenValidating_thenValid() {
String json = "[{\"email\": \"example@com\", \"name\": \"John\"},{\"email\": \"example1@com\", \"name\": \"Bob\"}]";
assertTrue(validator.isValid(json));
}
@Test
public void givenInvalidJson_whenValidating_thenInvalid() {
String json = "Invalid_Json";
assertFalse(validator.isValid(json));
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.jsonvalidation;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class JsonValidatorUnitTest {
private final JsonValidator validator = new JsonValidator();
@Test
public void givenValidObjectJson_whenValidatingObject_thenValid() {
String json = "{\"email\": \"example@com\", \"name\": \"John\"}";
assertTrue(validator.isValidObject(json));
}
@Test
public void givenInvalidJson_whenValidating_thenInvalid() {
String json = "Invalid_Json";
assertFalse(validator.isValidObject(json));
}
@Test
public void givenValidArrayJson_whenValidatingObject_thenInvalid() {
String json = "[{\"email\": \"example@com\", \"name\": \"John\"},{\"email\": \"example1@com\", \"name\": \"Bob\"}]";
assertFalse(validator.isValidObject(json));
}
@Test
public void givenValidJson_whenValidatingJson_thenValid() {
String json = "[{\"email\": \"example@com\", \"name\": \"John\"},{\"email\": \"example1@com\", \"name\": \"Bob\"}]";
assertTrue(validator.isValidJson(json));
}
}