List of usage examples for com.google.gson JsonParseException JsonParseException
public JsonParseException(Throwable cause)
From source file:com.urswolfer.intellij.plugin.gerrit.rest.gson.DateDeserializer.java
License:Apache License
@Override public Date deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { String date = jsonElement.getAsString(); try {//from w w w.ja va 2s . c o m return DATE_FORMAT.get().parse(date); } catch (ParseException e) { throw new JsonParseException(e); } }
From source file:com.ushahidi.android.data.api.DateDeserializer.java
License:Open Source License
@Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final SimpleDateFormat PARSER = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.getDefault()); try {//from ww w. ja v a2s .c om if (TextUtils.isDigitsOnly(json.getAsString())) { return new Date(new java.util.Date(json.getAsLong())); } return new Date(PARSER.parse(json.getAsString())); } catch (ParseException e) { throw new JsonParseException(e); } }
From source file:com.ushahidi.platform.mobile.app.data.api.DateDeserializer.java
License:Open Source License
@Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.getDefault()); try {/*from w w w . ja va 2s. c om*/ if (TextUtils.isDigitsOnly(json.getAsString())) { return new Date(new java.util.Date(json.getAsLong())); } return new Date(parser.parse(json.getAsString())); } catch (ParseException e) { throw new JsonParseException(e); } }
From source file:com.vmware.dcp.common.serialization.ObjectMapTypeConverter.java
License:Open Source License
@Override public Map<String, Object> deserialize(JsonElement json, Type unused, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("The json element is not valid"); }/*from ww w . java2 s . c o m*/ Map<String, Object> result = new HashMap<String, Object>(); JsonObject jsonObject = json.getAsJsonObject(); for (Entry<String, JsonElement> entry : jsonObject.entrySet()) { String key = entry.getKey(); JsonElement element = entry.getValue(); if (element.isJsonObject()) { result.put(key, element.toString()); } else if (element.isJsonNull()) { result.put(key, null); } else if (element.isJsonPrimitive()) { result.put(key, element.getAsString()); } else { throw new JsonParseException("The json element is not valid for key:" + key + " value:" + element); } } return result; }
From source file:com.vmware.xenon.common.serialization.ObjectCollectionTypeConverter.java
License:Open Source License
@Override public Collection<Object> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonArray()) { throw new JsonParseException("Expecting a json array object but found: " + json); }/*from w w w. jav a2 s . c om*/ Collection<Object> result; if (TYPE_SET.equals(type)) { result = new HashSet<>(); } else if (TYPE_LIST.equals(type) || TYPE_COLLECTION.equals(type)) { result = new LinkedList<>(); } else { throw new JsonParseException("Unexpected target type: " + type); } JsonArray jsonArray = json.getAsJsonArray(); for (JsonElement entry : jsonArray) { if (entry.isJsonNull()) { result.add(null); } else if (entry.isJsonPrimitive()) { JsonPrimitive elem = entry.getAsJsonPrimitive(); Object value = null; if (elem.isBoolean()) { value = elem.getAsBoolean(); } else if (elem.isString()) { value = elem.getAsString(); } else if (elem.isNumber()) { // We don't know if this is an integer, long, float or double... BigDecimal num = elem.getAsBigDecimal(); try { value = num.longValueExact(); } catch (ArithmeticException e) { value = num.doubleValue(); } } else { throw new RuntimeException("Unexpected value type for json element:" + elem); } result.add(value); } else { // keep JsonElement to prevent stringified json issues result.add(entry); } } return result; }
From source file:com.vmware.xenon.common.serialization.RequestRouteConverter.java
License:Open Source License
@Override public Route deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("The json element is not valid"); }//from w w w . ja va 2s . co m JsonObject jsonObject = json.getAsJsonObject(); Route route = new Route(); String action = checkAndGetFromJson(jsonObject, "action"); route.action = action == null ? null : Action.valueOf(action); route.description = checkAndGetFromJson(jsonObject, "description"); try { String requestType = checkAndGetFromJson(jsonObject, "requestType"); route.requestType = requestType == null ? null : Class.forName(requestType); String responseType = checkAndGetFromJson(jsonObject, "responseType"); route.responseType = responseType == null ? null : Class.forName(responseType); } catch (ClassNotFoundException e) { throw new IllegalStateException(e); } return route; }
From source file:com.wallellen.wechat.mp.util.json.WxMpUserCumulateGsonAdapter.java
License:Open Source License
public WxMpUserCumulate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { WxMpUserCumulate cumulate = new WxMpUserCumulate(); JsonObject summaryJsonObject = json.getAsJsonObject(); try {/*from w w w .j ava 2 s . co m*/ String refDate = GsonHelper.getString(summaryJsonObject, "ref_date"); if (refDate != null) { cumulate.setRefDate(SIMPLE_DATE_FORMAT.parse(refDate)); } cumulate.setCumulateUser(GsonHelper.getInteger(summaryJsonObject, "cumulate_user")); } catch (ParseException e) { throw new JsonParseException(e); } return cumulate; }
From source file:com.wallellen.wechat.mp.util.json.WxMpUserSummaryGsonAdapter.java
License:Open Source License
public WxMpUserSummary deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { WxMpUserSummary summary = new WxMpUserSummary(); JsonObject summaryJsonObject = json.getAsJsonObject(); try {/*from www.jav a 2s .com*/ String refDate = GsonHelper.getString(summaryJsonObject, "ref_date"); if (refDate != null) { summary.setRefDate(SIMPLE_DATE_FORMAT.parse(refDate)); } summary.setUserSource(GsonHelper.getInteger(summaryJsonObject, "user_source")); summary.setNewUser(GsonHelper.getInteger(summaryJsonObject, "new_user")); summary.setCancelUser(GsonHelper.getInteger(summaryJsonObject, "cancel_user")); } catch (ParseException e) { throw new JsonParseException(e); } return summary; }
From source file:com.wix.mediaplatform.gson.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (null == type || !baseType.isAssignableFrom(type.getRawType())) { return null; }/*from ww w .j av a 2 s . c om*/ final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<>(); 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().get(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T 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); @SuppressWarnings("unchecked") // registration requires that subtype extends T 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(); }
From source file:com.wxk.util.DateFormatter.java
License:MIT License
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonParseException exception = null; final String value = json.getAsString(); for (DateFormat format : formats) { try {//from w w w .ja v a 2 s . c o m synchronized (format) { return format.parse(value); } } catch (ParseException e) { exception = new JsonParseException(e); } } throw exception; }