Search in sources :

Example 36 with JsonParseException

use of com.google.gson.JsonParseException in project incubator-gobblin by apache.

the class DatasetDescriptorImpl method setComplianceField.

private void setComplianceField() {
    Preconditions.checkArgument(this.complianceFieldPath.isPresent());
    try {
        JsonObject descriptorObject = new JsonParser().parse(this.descriptor).getAsJsonObject();
        List<String> list = DOT_SPLITTER.splitToList(this.complianceFieldPath.get());
        for (int i = 0; i < list.size() - 1; i++) {
            descriptorObject = descriptorObject.getAsJsonObject(list.get(i));
        }
        this.complianceField = descriptorObject.get(list.get(list.size() - 1)).getAsString();
    } catch (JsonParseException | NullPointerException e) {
        log.warn("Compliance field not found at path " + this.complianceFieldPath.get() + " in the descriptor " + this.descriptor);
        Throwables.propagate(e);
    }
}
Also used : JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) JsonParser(com.google.gson.JsonParser)

Example 37 with JsonParseException

use of com.google.gson.JsonParseException in project id4i-api_client-java by BlueRainSoftware.

the class JSON method deserialize.

/**
 * Deserialize the given JSON string to Java object.
 *
 * @param <T>        Type
 * @param body       The JSON string
 * @param returnType The type to deserialize into
 * @return The deserialized Java object
 */
@SuppressWarnings("unchecked")
public <T> T deserialize(String body, Type returnType) {
    try {
        if (isLenientOnJson) {
            JsonReader jsonReader = new JsonReader(new StringReader(body));
            // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
            jsonReader.setLenient(true);
            return gson.fromJson(jsonReader, returnType);
        } else {
            return gson.fromJson(body, returnType);
        }
    } catch (JsonParseException e) {
        // return the response body string directly for the String return type;
        if (returnType.equals(String.class))
            return (T) body;
        else
            throw (e);
    }
}
Also used : StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader) JsonParseException(com.google.gson.JsonParseException)

Example 38 with JsonParseException

use of com.google.gson.JsonParseException in project BaseProject by wareine.

the class ExceptionHandle method handleException.

public static ResponseException handleException(Throwable e) {
    ResponseException ex;
    if (e instanceof HttpException) {
        HttpException httpException = (HttpException) e;
        ex = new ResponseException(e, ERROR.HTTP_ERROR);
        switch(httpException.code()) {
            case UNAUTHORIZED:
            case FORBIDDEN:
            case NOT_FOUND:
            case REQUEST_TIMEOUT:
            case GATEWAY_TIMEOUT:
            case INTERNAL_SERVER_ERROR:
            case BAD_GATEWAY:
            case SERVICE_UNAVAILABLE:
            default:
                ex.message = "网络错误";
                break;
        }
        return ex;
    } else if (e instanceof ServerException) {
        ServerException resultException = (ServerException) e;
        ex = new ResponseException(resultException, resultException.code);
        ex.message = resultException.message;
        return ex;
    } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
        ex = new ResponseException(e, ERROR.PARSE_ERROR);
        ex.message = "解析错误";
        return ex;
    } else if (e instanceof ConnectException) {
        ex = new ResponseException(e, ERROR.NETWORD_ERROR);
        ex.message = "连接失败";
        return ex;
    } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
        ex = new ResponseException(e, ERROR.SSL_ERROR);
        ex.message = "证书验证失败";
        return ex;
    } else if (e instanceof ConnectTimeoutException) {
        ex = new ResponseException(e, ERROR.TIMEOUT_ERROR);
        ex.message = "连接超时";
        return ex;
    } else if (e instanceof java.net.SocketTimeoutException) {
        ex = new ResponseException(e, ERROR.TIMEOUT_ERROR);
        ex.message = "连接超时";
        return ex;
    } else {
        ex = new ResponseException(e, ERROR.UNKNOWN);
        ex.message = "未知错误";
        return ex;
    }
}
Also used : JSONException(org.json.JSONException) HttpException(retrofit2.HttpException) JsonParseException(com.google.gson.JsonParseException) ParseException(android.net.ParseException) JsonParseException(com.google.gson.JsonParseException) ConnectException(java.net.ConnectException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 39 with JsonParseException

use of com.google.gson.JsonParseException in project cdap by caskdata.

the class ProtoConstraintCodec method deserialize.

@Override
public ProtoConstraint deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (json == null) {
        return null;
    }
    if (!(json instanceof JsonObject)) {
        throw new JsonParseException("Expected a JsonObject but found a " + json.getClass().getName());
    }
    JsonObject object = (JsonObject) json;
    JsonElement typeJson = object.get("type");
    ProtoConstraint.Type constraintType = context.deserialize(typeJson, ProtoConstraint.Type.class);
    Class<? extends ProtoConstraint> subClass = typeClassMap.get(constraintType);
    if (subClass == null) {
        throw new JsonParseException("Unable to map constraint type " + constraintType + " to a run constraint class");
    }
    ProtoConstraint constraint = context.deserialize(json, subClass);
    constraint.validate();
    return constraint;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Example 40 with JsonParseException

use of com.google.gson.JsonParseException in project be5 by DevelopmentOnTheEdge.

the class HashLink method parse.

public static HashLink parse(String code) {
    try {
        JsonObject parsed = new JsonParser().parse(code).getAsJsonObject();
        String action = parsed.get("action").getAsString();
        Map<String, String> namedArgs = getNamedArgs(parsed);
        List<String> positionalArgs = getPositionalArgs(parsed);
        return new HashLink(action, positionalArgs, namedArgs);
    } catch (IllegalStateException | ClassCastException e) {
        throw new IllegalArgumentException(e);
    } catch (JsonParseException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonParseException (com.google.gson.JsonParseException)210 JsonObject (com.google.gson.JsonObject)81 JsonElement (com.google.gson.JsonElement)55 IOException (java.io.IOException)54 Gson (com.google.gson.Gson)34 InputStreamReader (java.io.InputStreamReader)24 JsonArray (com.google.gson.JsonArray)21 InputStream (java.io.InputStream)20 GsonBuilder (com.google.gson.GsonBuilder)18 JsonParser (com.google.gson.JsonParser)18 JsonPrimitive (com.google.gson.JsonPrimitive)18 Type (java.lang.reflect.Type)17 Map (java.util.Map)17 JsonReader (com.google.gson.stream.JsonReader)16 ArrayList (java.util.ArrayList)14 HttpUrl (okhttp3.HttpUrl)13 Request (okhttp3.Request)13 Response (okhttp3.Response)13 JsonSyntaxException (com.google.gson.JsonSyntaxException)10 SocketTimeoutException (java.net.SocketTimeoutException)8