List of usage examples for com.google.gson JsonElement getAsString
public String getAsString()
From source file:com.fatboyindustrial.gsonjodatime.DurationConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the * specified type./*from w w w .j a v a 2 s . co m*/ * <p> * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects * for any non-trivial field of the returned object. However, you should never invoke it on the * the same type passing {@code json} since that will cause an infinite loop (Gson will call your * call-back method again). * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public Duration deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { // Do not try to deserialize null or empty values if (json.getAsString() == null || json.getAsString().isEmpty()) { return null; } return Duration.parse(json.getAsString()); }
From source file:com.fatboyindustrial.gsonjodatime.InstantConverter.java
License:Open Source License
@Override public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.getAsString() == null || json.getAsString().isEmpty()) { return null; }/*w w w.j a v a 2s . c o m*/ return Instant.parse(json.getAsString(), fmt); }
From source file:com.fatboyindustrial.gsonjodatime.IntervalConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the * specified type. <p>/* www .j a va2s . c o m*/ * * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects * for any non-trivial field of the returned object. However, you should never invoke it on the * the same type passing {@code json} since that will cause an infinite loop (Gson will call your * call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public Interval deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Interval(json.getAsString()); }
From source file:com.fatboyindustrial.gsonjodatime.LocalDateConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the * specified type. <p>/*from w w w . j av a 2s . co m*/ * * In the implementation of this call-back method, you should consider invoking * {@link com.google.gson.JsonDeserializationContext#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type)} method to create objects * for any non-trivial field of the returned object. However, you should never invoke it on the * the same type passing {@code json} since that will cause an infinite loop (Gson will call your * call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws com.google.gson.JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public LocalDate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final DateTimeFormatter fmt = DateTimeFormat.forPattern(PATTERN); return fmt.parseLocalDate(json.getAsString()); }
From source file:com.fatboyindustrial.gsonjodatime.LocalDateTimeConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the * specified type. <p>/*from www . j a v a2 s. c o m*/ * * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects * for any non-trivial field of the returned object. However, you should never invoke it on the * the same type passing {@code json} since that will cause an infinite loop (Gson will call your * call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final DateTimeFormatter fmt = DateTimeFormat.forPattern(PATTERN); return fmt.parseLocalDateTime(json.getAsString()); }
From source file:com.fatboyindustrial.gsonjodatime.LocalTimeConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the * specified type. <p>/* w w w . ja v a2 s . c o m*/ * * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects * for any non-trivial field of the returned object. However, you should never invoke it on the * the same type passing {@code json} since that will cause an infinite loop (Gson will call your * call-back method again). * * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public LocalTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final DateTimeFormatter fmt = DateTimeFormat.forPattern(PATTERN); return fmt.parseLocalTime(json.getAsString()); }
From source file:com.fatboyindustrial.gsonjodatime.PeriodConverter.java
License:Open Source License
/** * Gson invokes this call-back method during deserialization when it encounters a field of the * specified type. <p>/*from w w w. j a v a 2 s . c om*/ * * In the implementation of this call-back method, you should consider invoking * {@link JsonDeserializationContext#deserialize(JsonElement, Type)} method to create objects * for any non-trivial field of the returned object. However, you should never invoke it on the * the same type passing {@code json} since that will cause an infinite loop (Gson will call your * call-back method again). * @param json The Json data being deserialized * @param typeOfT The type of the Object to deserialize to * @return a deserialized object of the specified type typeOfT which is a subclass of {@code T} * @throws JsonParseException if json is not in the expected format of {@code typeOfT} */ @Override public Period deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { // Do not try to deserialize null or empty values if (json.getAsString() == null || json.getAsString().isEmpty()) { return null; } final PeriodFormatter fmt = ISOPeriodFormat.standard(); return fmt.parsePeriod(json.getAsString()); }
From source file:com.flipkart.android.proteus.builder.DataParsingLayoutBuilder.java
License:Apache License
public String isDataPath(JsonElement element) { if (!element.isJsonPrimitive()) { return null; }//from w ww . j av a 2 s . c om String attributeValue = element.getAsString(); if (attributeValue != null && !"".equals(attributeValue) && (attributeValue.charAt(0) == ProteusConstants.DATA_PREFIX || attributeValue.charAt(0) == ProteusConstants.REGEX_PREFIX)) { return attributeValue; } return null; }
From source file:com.flipkart.android.proteus.builder.LayoutBuilderFactory.java
License:Apache License
protected void registerFormatter(DataParsingLayoutBuilder layoutBuilder) { Formatter NumberFormatter = new Formatter() { private DecimalFormat formatter; @Override// w w w. j av a2 s. c om public String format(JsonElement elementValue) { double valueAsNumber; try { valueAsNumber = Double.parseDouble(elementValue.getAsString()); } catch (NumberFormatException e) { return elementValue.toString(); } formatter = new DecimalFormat("#,###"); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD) { formatter.setRoundingMode(RoundingMode.FLOOR); } formatter.setMinimumFractionDigits(0); formatter.setMaximumFractionDigits(2); return formatter.format(valueAsNumber); } @Override public String getName() { return "number"; } }; Formatter DateFormatter = new Formatter() { @SuppressLint("SimpleDateFormat") private SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @SuppressLint("SimpleDateFormat") private SimpleDateFormat to = new SimpleDateFormat("d MMM, E"); @Override public String format(JsonElement elementValue) { try { // 2015-06-18 12:01:37 Date date = from.parse(elementValue.getAsString()); return to.format(date); } catch (Exception e) { return elementValue.toString(); } } @Override public String getName() { return "date"; } }; Formatter IndexFormatter = new Formatter() { @Override public String format(JsonElement elementValue) { int valueAsNumber; try { valueAsNumber = Integer.parseInt(elementValue.getAsString()); } catch (NumberFormatException e) { return elementValue.toString(); } return String.valueOf(valueAsNumber + 1); } @Override public String getName() { return "index"; } }; Formatter joinFormatter = new Formatter() { @Override public String format(JsonElement elementValue) { if (elementValue.isJsonArray()) { return Utils.getStringFromArray(elementValue.getAsJsonArray(), ","); } else { return elementValue.toString(); } } @Override public String getName() { return "join"; } }; layoutBuilder.registerFormatter(NumberFormatter); layoutBuilder.registerFormatter(DateFormatter); layoutBuilder.registerFormatter(IndexFormatter); layoutBuilder.registerFormatter(joinFormatter); }
From source file:com.flipkart.android.proteus.parser.ParseHelper.java
License:Apache License
public static int parseVisibility(JsonElement element) { Integer returnValue = null;/*from ww w .j av a 2 s .c o m*/ if (element.isJsonPrimitive()) { String attributeValue = element.getAsString(); returnValue = sVisibilityMode.get(attributeValue); if (null == returnValue && (attributeValue.isEmpty() || FALSE.equals(attributeValue) || ProteusConstants.DATA_NULL.equals(attributeValue))) { returnValue = View.GONE; } } else if (element.isJsonNull()) { returnValue = View.GONE; } return returnValue == null ? View.VISIBLE : returnValue; }