Example usage for com.google.gson JsonParseException JsonParseException

List of usage examples for com.google.gson JsonParseException JsonParseException

Introduction

In this page you can find the example usage for com.google.gson JsonParseException JsonParseException.

Prototype

public JsonParseException(String msg, Throwable cause) 

Source Link

Document

Creates exception with the specified message and cause.

Usage

From source file:io.motown.operatorapi.json.gson.deserializer.TextualTokenTypeAdapterDeserializer.java

License:Apache License

@Override
public TextualToken deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    JsonObject obj;//from ww w  .  ja  v a2  s .  c  om
    String token;

    try {
        obj = json.getAsJsonObject();
    } catch (ClassCastException | IllegalStateException e) {
        throw new JsonParseException("TextualToken must be a JSON object", e);
    }

    try {
        token = obj.getAsJsonPrimitive(TOKEN_MEMBER) != null
                ? obj.getAsJsonPrimitive(TOKEN_MEMBER).getAsString()
                : "";
    } catch (ClassCastException | IllegalStateException e) {
        throw new JsonParseException("token must be a JSON string", e);
    }

    try {
        JsonPrimitive authenticationStatus = obj.getAsJsonPrimitive("status");
        if (authenticationStatus != null) {
            String status = authenticationStatus.getAsString();
            return new TextualToken(token, IdentifyingToken.AuthenticationStatus.valueOf(status));
        }
    } catch (ClassCastException | IllegalStateException e) {
        throw new JsonParseException("status must be a JSON string", e);
    }

    return new TextualToken(token);
}

From source file:matteroverdrive.client.resources.data.WeaponMetadataSectionSerializer.java

License:Open Source License

@Override
public WeaponMetadataSection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonobject = JsonUtils.getJsonElementAsJsonObject(json, "metadata section");
    Vec3 scopePosition = null;/*w  w  w .j  a  v a 2 s .  c o m*/
    try {
        JsonArray array = jsonobject.getAsJsonArray("scope_position");
        if (array.size() >= 3) {
            scopePosition = Vec3.createVectorHelper(array.get(0).getAsDouble(), array.get(1).getAsDouble(),
                    array.get(2).getAsDouble());
        }
    } catch (ClassCastException classcastexception) {
        throw new JsonParseException(
                "Invalid weapon->scope_position: expected array, was " + jsonobject.get("scope_position"),
                classcastexception);
    }
    return new WeaponMetadataSection(scopePosition);
}

From source file:mobi.jenkinsci.alm.assembla.client.CalendarSerializer.java

License:Apache License

@Override
public Calendar deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Calendar cal = Calendar.getInstance();
    String calString = json.getAsString();
    calString = fixTZ(calString);//from ww w . j av  a 2 s  . c o  m
    try {
        cal.setTime(calendarFormat.parse(calString));
    } catch (ParseException e) {
        throw new JsonParseException("Unparseable date " + calString, e);
    }
    return cal;
}

From source file:net.bhira.sample.common.JsonUtil.java

License:Open Source License

/**
 * Create a customized version of Gson that handles Date formats and timezones correctly. It
 * uses UTC date format for parsing and formating dates and expects the date string values to be
 * formatted in UTC timezone.//from w  w w  .ja  v  a  2 s  . com
 *
 * @return an instance of {@link com.google.gson.Gson}.
 */
public static Gson createGson() {

    // create a custom serializer for dates
    JsonSerializer<Date> serializer = new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            if (src == null) {
                return null;
            }
            SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
            formatter.setTimeZone(TimeZone.getTimeZone(TIMEZONE));
            return new JsonPrimitive(formatter.format(src));
        }
    };

    // create a custom de-serializer for dates
    JsonDeserializer<Date> deserializer = new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            if (json == null) {
                return null;
            }
            String date = json.getAsString();
            SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
            formatter.setTimeZone(TimeZone.getTimeZone(TIMEZONE));
            try {
                return formatter.parse(date);
            } catch (ParseException e) {
                throw new JsonParseException("Error parsing date " + date, e);
            }

        }
    };

    // create a custom gson that uses the custom date serializer/deserializer
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Date.class, serializer);
    gsonBuilder.registerTypeAdapter(Date.class, deserializer);
    return gsonBuilder.create();
}

From source file:net.doubledoordev.cmd.forge.ForgeFileJson.java

License:Open Source License

@Override
public ForgeFile deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {//from  ww  w.j a  va  2s. c o  m
        JsonArray array = json.getAsJsonArray();
        ForgeFile file = new ForgeFile();
        file.extention = array.get(0).getAsString();
        file.type = array.get(1).getAsString();
        file.md5 = array.get(2).getAsString();
        return file;
    } catch (Exception e) {
        throw new JsonParseException("Forge file format incorrect.", e);
    }
}

From source file:net.patrickpollet.gson.JsonBooleanDeserializer.java

License:Open Source License

public Boolean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {/*from ww  w  . j  a v a2 s .  c o  m*/
        String value = json.getAsJsonPrimitive().getAsString();
        return value.toLowerCase().equals("true");
    } catch (ClassCastException e) {
        throw new JsonParseException("Cannot parse json boolean '" + json.toString() + "'", e);
    }
}

From source file:nl.tue.gale.um.data.EntityValue.java

License:Open Source License

@Override
public EntityValue deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    EntityValue ev = new EntityValue();
    JsonObject obj = (JsonObject) json;/*from  ww w  .j a va 2s.  c o  m*/
    try {
        ev.setUriString(obj.getAsJsonPrimitive("uri").getAsString());
        ev.setValueBytes(Base64.decode(obj.getAsJsonPrimitive("value").getAsString()));
    } catch (Exception e) {
        throw new JsonParseException("unable to deserialize EntityValue: " + json.toString(), e);
    }
    return ev;
}

From source file:nz.ac.otago.psyanlab.common.model.util.AbsModelGsonAdapter.java

License:Open Source License

@Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext jctx)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String type = jsonObject.get("type").getAsString();
    JsonElement element = jsonObject.get("properties");

    try {//  w  ww . j a v  a2s . c o m
        return jctx.deserialize(element, Class.forName(mNamespace + type));
    } catch (ClassNotFoundException e) {
        throw new JsonParseException("Unknown element type: " + type, e);
    }
}

From source file:org.catrobat.catroid.io.BackpackScriptSerializerAndDeserializer.java

License:Open Source License

@Override
public Script deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    JsonObject jsonObject = json.getAsJsonObject();
    String type = jsonObject.get(TYPE).getAsString();
    JsonElement element = jsonObject.get(PROPERTY);

    try {//from  w w w .j av  a2  s. co m
        return context.deserialize(element, Class.forName(PACKAGE_NAME + type));
    } catch (ClassNotFoundException e) {
        Log.e(TAG, "Could not deserialize backpacked script element!");
        throw new JsonParseException("Unknown element type: " + type, e);
    }
}

From source file:org.commonjava.freeki.model.io.DateSerializer.java

License:Open Source License

@Override
public Date deserialize(final JsonElement src, final Type type, final JsonDeserializationContext ctx)
        throws JsonParseException {
    final String s = src.getAsString();
    if (s.trim().length() < 1) {
        return null;
    }/*from w  ww  . j a va2 s  . c  o  m*/

    try {
        return parseDate(s);
    } catch (final ParseException e) {
        throw new JsonParseException(String.format("Failed to parse date: '%s'. Reason: %s", s, e.getMessage()),
                e);
    }
}