Search in sources :

Example 96 with JsonParseException

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();
    }
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Example 97 with JsonParseException

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;
}
Also used : JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) JsonParseException(com.google.gson.JsonParseException)

Example 98 with JsonParseException

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;
}
Also used : MyPetPlayerImpl(de.Keyle.MyPet.util.player.MyPetPlayerImpl) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException)

Example 99 with JsonParseException

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();
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) JsonWriter(com.google.gson.stream.JsonWriter) LinkedHashMap(java.util.LinkedHashMap) JsonElement(com.google.gson.JsonElement) TypeAdapter(com.google.gson.TypeAdapter) JsonReader(com.google.gson.stream.JsonReader) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 100 with JsonParseException

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;
    }
}
Also used : HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonParseException (com.google.gson.JsonParseException)267 JsonObject (com.google.gson.JsonObject)105 IOException (java.io.IOException)78 JsonElement (com.google.gson.JsonElement)71 Gson (com.google.gson.Gson)39 JsonArray (com.google.gson.JsonArray)30 JsonReader (com.google.gson.stream.JsonReader)28 InputStreamReader (java.io.InputStreamReader)28 JsonParser (com.google.gson.JsonParser)25 JsonPrimitive (com.google.gson.JsonPrimitive)25 Map (java.util.Map)25 InputStream (java.io.InputStream)23 GsonBuilder (com.google.gson.GsonBuilder)20 ArrayList (java.util.ArrayList)20 Type (java.lang.reflect.Type)17 JsonWriter (com.google.gson.stream.JsonWriter)15 StringReader (java.io.StringReader)13 HashMap (java.util.HashMap)13 LinkedHashMap (java.util.LinkedHashMap)13 HttpUrl (okhttp3.HttpUrl)13