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 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; } }