Search in sources :

Example 56 with JsonParseException

use of com.google.gson.JsonParseException in project gerrit by GerritCodeReview.

the class EditDeserializer method get.

private static int get(final JsonArray a, final int idx) throws JsonParseException {
    final JsonElement v = a.get(idx);
    if (!v.isJsonPrimitive()) {
        throw new JsonParseException("Expected array of 4 for Edit type");
    }
    final JsonPrimitive p = (JsonPrimitive) v;
    if (!p.isNumber()) {
        throw new JsonParseException("Expected array of 4 for Edit type");
    }
    return p.getAsInt();
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JsonParseException(com.google.gson.JsonParseException)

Example 57 with JsonParseException

use of com.google.gson.JsonParseException in project gerrit by GerritCodeReview.

the class EditDeserializer method deserialize.

@Override
public Edit deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
    if (json.isJsonNull()) {
        return null;
    }
    if (!json.isJsonArray()) {
        throw new JsonParseException("Expected array for Edit type");
    }
    final JsonArray o = (JsonArray) json;
    final int cnt = o.size();
    if (cnt < 4 || cnt % 4 != 0) {
        throw new JsonParseException("Expected array of 4 for Edit type");
    }
    if (4 == cnt) {
        return new Edit(get(o, 0), get(o, 1), get(o, 2), get(o, 3));
    }
    List<Edit> l = new ArrayList<>((cnt / 4) - 1);
    for (int i = 4; i < cnt; ) {
        int as = get(o, i++);
        int ae = get(o, i++);
        int bs = get(o, i++);
        int be = get(o, i++);
        l.add(new Edit(as, ae, bs, be));
    }
    return new ReplaceEdit(get(o, 0), get(o, 1), get(o, 2), get(o, 3), l);
}
Also used : JsonArray(com.google.gson.JsonArray) ArrayList(java.util.ArrayList) JsonParseException(com.google.gson.JsonParseException)

Example 58 with JsonParseException

use of com.google.gson.JsonParseException in project gerrit by GerritCodeReview.

the class SupplierDeserializer method deserialize.

@Override
public Supplier<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    checkArgument(typeOfT instanceof ParameterizedType);
    ParameterizedType parameterizedType = (ParameterizedType) typeOfT;
    if (parameterizedType.getActualTypeArguments().length != 1) {
        throw new JsonParseException("Expected one parameter type in Supplier interface.");
    }
    Type supplierOf = parameterizedType.getActualTypeArguments()[0];
    return Suppliers.ofInstance(context.deserialize(json, supplierOf));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonParseException(com.google.gson.JsonParseException)

Example 59 with JsonParseException

use of com.google.gson.JsonParseException in project gerrit by GerritCodeReview.

the class EventDeserializer method deserialize.

@Override
public Event deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("Not an object");
    }
    JsonElement typeJson = json.getAsJsonObject().get("type");
    if (typeJson == null || !typeJson.isJsonPrimitive() || !typeJson.getAsJsonPrimitive().isString()) {
        throw new JsonParseException("Type is not a string: " + typeJson);
    }
    String type = typeJson.getAsJsonPrimitive().getAsString();
    Class<?> cls = EventTypes.getClass(type);
    if (cls == null) {
        throw new JsonParseException("Unknown event type: " + type);
    }
    return context.deserialize(json, cls);
}
Also used : JsonElement(com.google.gson.JsonElement) JsonParseException(com.google.gson.JsonParseException)

Example 60 with JsonParseException

use of com.google.gson.JsonParseException in project wh-app-android by WhiteHouse.

the class DateFormatter method deserialize.

public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonParseException exception = null;
    final String value = json.getAsString();
    for (DateFormat format : formats) {
        try {
            synchronized (format) {
                return format.parse(value);
            }
        } catch (ParseException e) {
            exception = new JsonParseException(e);
        }
    }
    throw exception;
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) JsonParseException(com.google.gson.JsonParseException) ParseException(java.text.ParseException) JsonParseException(com.google.gson.JsonParseException)

Aggregations

JsonParseException (com.google.gson.JsonParseException)66 JsonObject (com.google.gson.JsonObject)24 IOException (java.io.IOException)17 JsonElement (com.google.gson.JsonElement)15 Gson (com.google.gson.Gson)14 GsonBuilder (com.google.gson.GsonBuilder)9 Type (java.lang.reflect.Type)9 JsonArray (com.google.gson.JsonArray)7 JsonParser (com.google.gson.JsonParser)6 SocketTimeoutException (java.net.SocketTimeoutException)5 JsonDeserializationContext (com.google.gson.JsonDeserializationContext)4 UnknownHostException (java.net.UnknownHostException)4 ArrayList (java.util.ArrayList)4 Resources (android.content.res.Resources)3 TextView (android.widget.TextView)3 RutgersServerIOException (com.tevinjeffrey.rutgersct.rutgersapi.exceptions.RutgersServerIOException)3 InputStreamReader (java.io.InputStreamReader)3 ParseException (java.text.ParseException)3 Date (java.util.Date)3 ResourceLocation (net.minecraft.util.ResourceLocation)3