List of usage examples for com.google.gson.reflect TypeToken getRawType
public final Class<? super T> getRawType()
From source file:com.hmwg.utils.GSONUtils.java
License:Apache License
/** * {@code JSON} ??//from w w w. j a v a 2 s . com * * @param <T> * ?? * @param json * {@code JSON} * @param token * {@code com.google.gson.reflect.TypeToken} * @param datePattern * ?? * @return {@code JSON} * @since 1.0 */ public static <T> T fromJson(String json, TypeToken<T> token, String datePattern) { if (isBlankString(json)) { return null; } GsonBuilder builder = new GsonBuilder(); if (isBlankString(datePattern)) { datePattern = DEFAULT_DATE_PATTERN; } builder.setDateFormat(datePattern); Gson gson = builder.setDateFormat("yyyy-MM-dd HH:mm:ss").create(); try { return gson.fromJson(json, token.getType()); } catch (Exception ex) { Log.i("ws", " ? " + token.getRawType().getName() + " !" + ex.getMessage()); return null; } }
From source file:com.ibm.og.json.type.ChoiceConfigTypeAdapterFactory.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { final Class<T> rawType = (Class<T>) type.getRawType(); if (!ChoiceConfig.class.equals(rawType)) { return null; }/*from w w w . j ava2 s . c o m*/ final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); return new TypeAdapter<T>() { @Override public void write(final JsonWriter out, final T value) throws IOException { delegate.write(out, value); } @Override @SuppressWarnings("rawtypes") public T read(final JsonReader in) throws IOException { final Class<?> genericType = (Class<?>) ((ParameterizedType) type.getType()) .getActualTypeArguments()[0]; final TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class); // the tree api is used here rather than the stream api so that the full object can be // inspected and we can differentiate between a ChoiceConfig<T> object or the underlying T // object itself. With the stream api there would be no way to rewind the stream once this // determination is made // // this logic allows the user to configure a choice of T in both the standard form, or // compactly if the default choice weight is sufficient e.g. // // standard form // {"choice": {fields for T object}, "weight": 1.0} <- weight is optional here // // compact form where default weight is acceptable // {fields for T object} final JsonElement element = jsonElementAdapter.read(in); if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); if (object.entrySet().size() <= 2 && object.has("choice")) { return delegate.fromJsonTree(element); } } return (T) new ChoiceConfig(gson.getAdapter(TypeToken.get(genericType)).fromJsonTree(element)); } }.nullSafe(); }
From source file:com.ibm.og.json.type.ContainerConfigTypeAdapterFactory.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { final Class<T> rawType = (Class<T>) type.getRawType(); if (!ContainerConfig.class.equals(rawType)) { return null; }//from ww w . j a v a 2 s. c o m final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); return new TypeAdapter<T>() { @Override public void write(final JsonWriter out, final T value) throws IOException { delegate.write(out, value); } @Override public T read(final JsonReader in) throws IOException { if (JsonToken.STRING == in.peek()) { return (T) new ContainerConfig(in.nextString()); } return delegate.read(in); } }.nullSafe(); }
From source file:com.ibm.og.json.type.FilesizeConfigTypeAdapterFactory.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { final Class<T> rawType = (Class<T>) type.getRawType(); if (!FilesizeConfig.class.equals(rawType)) { return null; }/* ww w . j a va 2 s .c o m*/ final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); return new TypeAdapter<T>() { @Override public void write(final JsonWriter out, final T value) throws IOException { delegate.write(out, value); } @Override public T read(final JsonReader in) throws IOException { if (JsonToken.NUMBER == in.peek()) { return (T) new FilesizeConfig(in.nextDouble()); } return delegate.read(in); } }.nullSafe(); }
From source file:com.ibm.og.json.type.OperationConfigTypeAdapterFactory.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { final Class<T> rawType = (Class<T>) type.getRawType(); if (!OperationConfig.class.equals(rawType)) { return null; }//from w ww . j a v a2s . co m final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); return new TypeAdapter<T>() { @Override public void write(final JsonWriter out, final T value) throws IOException { delegate.write(out, value); } @Override public T read(final JsonReader in) throws IOException { if (JsonToken.NUMBER == in.peek()) { return (T) new OperationConfig(in.nextDouble()); } return delegate.read(in); } }.nullSafe(); }
From source file:com.ibm.og.json.type.SelectionConfigTypeAdapterFactory.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { final Class<T> rawType = (Class<T>) type.getRawType(); if (!SelectionConfig.class.equals(rawType)) { return null; }/*from ww w . ja v a 2s . co m*/ final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); return new TypeAdapter<T>() { @Override public void write(final JsonWriter out, final T value) throws IOException { delegate.write(out, value); } @Override public T read(final JsonReader in) throws IOException { final Class<?> genericType = (Class<?>) ((ParameterizedType) type.getType()) .getActualTypeArguments()[0]; final JsonElement element = gson.getAdapter(JsonElement.class).read(in); if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); if (object.entrySet().size() <= 2 && object.has("choices")) { return delegate.fromJsonTree(object); } else { return (T) choice(genericType, object); } } else if (element.isJsonArray()) { return (T) choiceList(genericType, element.getAsJsonArray()); } return (T) choice(genericType, element); } private <S> SelectionConfig<S> choice(final Class<S> clazz, final JsonElement element) throws IOException { final SelectionConfig<S> config = new SelectionConfig<S>(); config.choices.add(gson.getAdapter(choiceToken(clazz)).fromJsonTree(element)); return config; } private <S> SelectionConfig<S> choiceList(final Class<S> clazz, final JsonArray array) throws IOException { final SelectionConfig<S> config = new SelectionConfig<S>(); config.choices = gson.getAdapter(choiceListToken(clazz)).fromJsonTree(array); return config; } // must use guava's TypeToken implementation to create a TypeToken instance with a dynamic // type; then convert back to gson's TypeToken implementation for use in calling code. See: // https://groups.google.com/forum/#!topic/guava-discuss/HdBuiO44uaw private <S> TypeToken<ChoiceConfig<S>> choiceToken(final Class<S> clazz) { @SuppressWarnings("serial") final com.google.common.reflect.TypeToken<ChoiceConfig<S>> choiceToken = new com.google.common.reflect.TypeToken<ChoiceConfig<S>>() { }.where(new TypeParameter<S>() { }, com.google.common.reflect.TypeToken.of(clazz)); return (TypeToken<ChoiceConfig<S>>) TypeToken.get(choiceToken.getType()); } private <S> TypeToken<List<ChoiceConfig<S>>> choiceListToken(final Class<S> clazz) { @SuppressWarnings("serial") final com.google.common.reflect.TypeToken<List<ChoiceConfig<S>>> choiceToken = new com.google.common.reflect.TypeToken<List<ChoiceConfig<S>>>() { }.where(new TypeParameter<S>() { }, com.google.common.reflect.TypeToken.of(clazz)); return (TypeToken<List<ChoiceConfig<S>>>) TypeToken.get(choiceToken.getType()); } }.nullSafe(); }
From source file:com.ibm.og.util.json.type.CaseInsensitiveEnumTypeAdapterFactory.java
License:Open Source License
@Override public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { @SuppressWarnings("unchecked") final Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.isEnum()) { return null; }/*from w w w .j a v a 2 s . c o m*/ return new TypeAdapter<T>() { @Override public void write(final JsonWriter out, final T value) throws IOException { out.value(value.toString().toLowerCase(Locale.US)); } @Override @SuppressWarnings("unchecked") public T read(final JsonReader in) throws IOException { final String s = in.nextString().toUpperCase(Locale.US); for (final Object enumEntry : rawType.getEnumConstants()) { if (enumEntry.toString().equals(s)) { return (T) enumEntry; } } throw new JsonSyntaxException(String.format("Could not parse into enum [%s]", s)); } }.nullSafe(); }
From source file:com.ibm.watson.developer_cloud.discovery.v1.model.collection.field.FieldAdapterFactory.java
License:Open Source License
@SuppressWarnings("unchecked") @Override// w ww . j ava2 s . co m public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { if (!Field.class.isAssignableFrom(typeToken.getRawType())) { return null; } return (TypeAdapter<T>) new FieldAdapter(); }
From source file:com.ibm.watson.developer_cloud.discovery.v1.model.common.EagerNumberAdapterFactory.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w w w . jav a2 s. c o m public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { if (!Number.class.isAssignableFrom(typeToken.getRawType())) { return null; } return (TypeAdapter<T>) new NumberTypeAdapter(); }
From source file:com.ibm.watson.developer_cloud.discovery.v1.model.query.AggregationAdapterFactory.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w w w . j a va 2s.c om public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { if (typeToken.getRawType() != Aggregation.class) { return null; } return (TypeAdapter<T>) new QueryAggregationTypeAdapter(gson); }