use of com.google.gson.JsonParseException in project hub-alert by blackducksoftware.
the class AzureBoardsHttpExceptionMessageImprover method extractResponseContentMessage.
private Optional<String> extractResponseContentMessage(String responseContent) {
try {
JsonObject responseContentObject = gson.fromJson(responseContent, JsonObject.class);
JsonElement messageElement = responseContentObject.get(AZURE_ERROR_MESSAGE_FIELD_NAME);
return Optional.ofNullable(messageElement).filter(JsonElement::isJsonPrimitive).map(JsonElement::getAsJsonPrimitive).filter(JsonPrimitive::isString).map(JsonPrimitive::getAsString);
} catch (JsonParseException e) {
return Optional.empty();
}
}
use of com.google.gson.JsonParseException in project MyPet by xXKeyleXx.
the class ConfigurationJSON method load.
public boolean load() {
config = new JsonObject();
try (BufferedReader reader = new BufferedReader(new FileReader(jsonFile))) {
Gson gson = new Gson();
config = gson.fromJson(reader, JsonObject.class);
} catch (JsonParseException e) {
MyPetApi.getLogger().warning("Could not parse/load " + jsonFile.getName());
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
use of com.google.gson.JsonParseException in project MyPet by xXKeyleXx.
the class SqLiteRepository method resultSetToMyPetPlayer.
// Players ---------------------------------------------------------------------------------------------------------
private MyPetPlayer resultSetToMyPetPlayer(ResultSet resultSet) {
try {
if (resultSet.next()) {
MyPetPlayerImpl petPlayer;
UUID internalUUID = UUID.fromString(resultSet.getString("internal_uuid"));
String playerName = resultSet.getString("name");
UUID mojangUUID = resultSet.getString("mojang_uuid") != null ? UUID.fromString(resultSet.getString("mojang_uuid")) : null;
if (mojangUUID != null) {
petPlayer = new MyPetPlayerImpl(internalUUID, mojangUUID);
petPlayer.setLastKnownName(playerName);
} else if (playerName != null) {
petPlayer = new MyPetPlayerImpl(internalUUID, playerName);
} else {
MyPetApi.getLogger().warning("Player with no UUID or name found!");
return null;
}
petPlayer.setAutoRespawnEnabled(resultSet.getBoolean("auto_respawn"));
petPlayer.setAutoRespawnMin(resultSet.getInt("auto_respawn_min"));
petPlayer.setCaptureHelperActive(resultSet.getBoolean("capture_mode"));
petPlayer.setHealthBarActive(resultSet.getBoolean("health_bar"));
petPlayer.setPetLivingSoundVolume(resultSet.getFloat("pet_idle_volume"));
petPlayer.setExtendedInfo(TagStream.readTag(resultSet.getBytes("extended_info"), true));
try {
JsonObject jsonObject = gson.fromJson(resultSet.getString("multi_world"), JsonObject.class);
for (String uuid : jsonObject.keySet()) {
String petUUID = jsonObject.get(uuid).getAsString();
petPlayer.setMyPetForWorldGroup(uuid, UUID.fromString(petUUID));
}
} catch (JsonParseException e) {
e.printStackTrace();
}
return petPlayer;
}
} catch (SQLException | IOException e) {
e.printStackTrace();
}
return null;
}
use of com.google.gson.JsonParseException in project bayou by capergroup.
the class RuntimeTypeAdapterFactory method create.
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
if (type.getRawType() != baseType) {
return null;
}
final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>();
final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>();
for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
labelToDelegate.put(entry.getKey(), delegate);
subtypeToDelegate.put(entry.getValue(), delegate);
}
return new TypeAdapter<R>() {
@Override
public R read(JsonReader in) throws IOException {
JsonElement jsonElement = Streams.parse(in);
JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
if (labelJsonElement == null) {
throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName);
}
String label = labelJsonElement.getAsString();
// registration requires that subtype extends T
@SuppressWarnings("unchecked") TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label);
if (delegate == null) {
throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?");
}
return delegate.fromJsonTree(jsonElement);
}
@Override
public void write(JsonWriter out, R value) throws IOException {
Class<?> srcType = value.getClass();
String label = subtypeToLabel.get(srcType);
// registration requires that subtype extends T
@SuppressWarnings("unchecked") TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType);
if (delegate == null) {
throw new JsonParseException("cannot serialize " + srcType.getName() + "; did you forget to register a subtype?");
}
JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();
if (jsonObject.has(typeFieldName)) {
throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName);
}
JsonObject clone = new JsonObject();
clone.add(typeFieldName, new JsonPrimitive(label));
for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
clone.add(e.getKey(), e.getValue());
}
Streams.write(clone, out);
}
}.nullSafe();
}
use of com.google.gson.JsonParseException in project carbon-apimgt by wso2.
the class JWTUtil method getJWTClaims.
/**
* Parse a jwt assertion provided in string format and returns set of claims
* defined in the assertion.
*
* @param jwt jwt assertion
* @return claims as a {@link Map}. if jwt is not parse-able null will be returned.
*/
public static Map<String, String> getJWTClaims(String jwt) {
if (StringUtils.isNotEmpty(jwt)) {
Map<String, String> jwtClaims = new HashMap<>();
String[] jwtTokenArray = jwt.split(Pattern.quote("."));
// decoding JWT
try {
byte[] jwtByteArray = Base64.decodeBase64(jwtTokenArray[1].getBytes(StandardCharsets.UTF_8));
String jwtAssertion = new String(jwtByteArray, StandardCharsets.UTF_8);
JsonElement parsedJson = new JsonParser().parse(jwtAssertion);
if (parsedJson.isJsonObject()) {
JsonObject rootObject = parsedJson.getAsJsonObject();
for (Map.Entry<String, JsonElement> rootElement : rootObject.entrySet()) {
if (rootElement.getValue().isJsonPrimitive()) {
jwtClaims.put(rootElement.getKey(), rootElement.getValue().getAsString());
} else if (rootElement.getValue().isJsonArray()) {
JsonArray arrayElement = rootElement.getValue().getAsJsonArray();
List<String> element = new ArrayList<>();
for (JsonElement jsonElement : arrayElement) {
element.add(jsonElement.getAsString());
}
jwtClaims.put(rootElement.getKey(), String.join("|", element));
} else if (rootElement.getValue().isJsonObject()) {
getJWTClaimsArray(jwtClaims, (JsonObject) rootElement.getValue(), rootElement.getKey());
}
}
}
} catch (JsonParseException e) {
// gson throws runtime exceptions for parsing errors. We don't want to throw
// errors and break the flow from this util method. Therefore logging and
// returning null for error case
log.error("Error occurred while parsing jwt claims");
}
return jwtClaims;
} else {
return null;
}
}
Aggregations