List of usage examples for com.google.gson.reflect TypeToken get
public static <T> TypeToken<T> get(Class<T> type)
From source file:org.lanternpowered.server.script.function.value.json.IntValueProviderTypeAdapterFactory.java
License:MIT License
@Override protected IntValueProvider deserialize(TypeToken<IntValueProvider> type, JsonElement element, Gson gson) { final Class<? super IntValueProvider> raw = type.getRawType(); if (IntValueProvider.Constant.class.isAssignableFrom(raw)) { return gson.getDelegateAdapter(this, TypeToken.get(IntValueProvider.Constant.class)) .fromJsonTree(element);/* w w w . j a v a 2 s . co m*/ } else if (IntValueProvider.Range.class.isAssignableFrom(raw)) { return gson.getDelegateAdapter(this, TypeToken.get(IntValueProvider.Range.class)).fromJsonTree(element); } else if (raw == IntValueProvider.class) { // The actual condition type isn't provided, we will try // to assume the right type based in the json format if (element.isJsonPrimitive()) { final String value = element.getAsString(); if (value.startsWith(SCRIPT_PREFIX)) { return LanternScriptGameRegistry.get().compile(value, IntValueProvider.class).get(); } return gson.getDelegateAdapter(this, TypeToken.get(IntValueProvider.Constant.class)) .fromJsonTree(element); } } return super.deserialize(type, element, gson); }
From source file:org.lanternpowered.server.script.json.JsonSerializers.java
License:MIT License
/** * Registers all the json serializers to the {@link GsonBuilder}. * * @param gsonBuilder The gson builder//from w w w . j av a 2s.c o m * @return The gson builder */ public static GsonBuilder register(GsonBuilder gsonBuilder) { return gsonBuilder.registerTypeAdapter(Script.class, new ScriptJsonSerializer()) .registerTypeAdapter(ScriptFunction.class, new ScriptFunctionJsonSerializer()) // DoubleValueProvider .registerTypeAdapter(DoubleValueProvider.Constant.class, new ConstantDoubleValueProviderJsonSerializer()) .registerTypeAdapter(DoubleValueProvider.Range.class, new RangeDoubleValueProviderJsonSerializer()) .registerTypeAdapterFactory(new DoubleValueProviderTypeAdapterFactory( DoubleValueProviderTypeRegistryModule.get(), TypeToken.get(DoubleValueProvider.class))) // FloatValueProvider .registerTypeAdapter(FloatValueProvider.Constant.class, new ConstantFloatValueProviderJsonSerializer()) .registerTypeAdapter(FloatValueProvider.Range.class, new RangeFloatValueProviderJsonSerializer()) .registerTypeAdapterFactory(new FloatValueProviderTypeAdapterFactory( FloatValueProviderTypeRegistryModule.get(), TypeToken.get(FloatValueProvider.class))) // IntValueProvider .registerTypeAdapter(IntValueProvider.Constant.class, new ConstantIntValueProviderJsonSerializer()) .registerTypeAdapter(IntValueProvider.Range.class, new RangeIntValueProviderJsonSerializer()) .registerTypeAdapterFactory(new IntValueProviderTypeAdapterFactory( IntValueProviderTypeRegistryModule.get(), TypeToken.get(IntValueProvider.class))) // Action .registerTypeAdapter(MultiAction.class, new MultiActionJsonSerializer()) .registerTypeAdapter(ConditionalAction.class, new ConditionalActionJsonSerializer()) .registerTypeAdapter(EmptyAction.class, new EmptyActionJsonSerializer()) .registerTypeAdapterFactory( new ActionTypeAdapterFactory(ActionTypeRegistryModule.get(), TypeToken.get(Action.class))) // Condition .registerTypeAdapter(AndCondition.class, new AndConditionJsonSerializer()) .registerTypeAdapter(OrCondition.class, new OrConditionJsonSerializer()) .registerTypeAdapterFactory(new ConditionTypeAdapterFactory(ConditionTypeRegistryModule.get(), TypeToken.get(Condition.class))) // Catalog Types .registerTypeAdapterFactory(new CatalogTypeTypeAdapterFactory()) .registerTypeAdapter(SimpleOptionValueMap.class, new SimpleOptionValueMapJsonSerializer()) .registerTypeAdapter(UnmodifiableOptionValueMap.class, new UnmodifiableOptionValueMapJsonSerializer()); }
From source file:org.lanternpowered.server.script.json.ObjectTypeAdapterFactory.java
License:MIT License
protected JsonElement serialize(TypeToken<V> type, V value, Gson gson) throws IOException { //noinspection unchecked final Optional<O> optType = this.registry.getByClass((Class) value.getClass()); if (!optType.isPresent()) { throw new IOException( "Attempted to serialize a action type that is not registered: " + value.getClass().getName()); }/*from w w w . j av a 2 s.co m*/ final JsonObject json = new JsonObject(); json.addProperty(TYPE, optType.get().getId()); //noinspection unchecked final TypeAdapter<V> delegateTypeAdapter = gson.getDelegateAdapter(this, (TypeToken<V>) TypeToken.get(optType.get().getType())); json.add(DATA, delegateTypeAdapter.toJsonTree(value)); return json; }
From source file:org.lanternpowered.server.script.json.ObjectTypeAdapterFactory.java
License:MIT License
protected V deserialize(TypeToken<V> type, JsonElement element, Gson gson) { final JsonObject obj = element.getAsJsonObject(); final String valueTypeId = obj.get(TYPE).getAsString(); //noinspection unchecked final Optional<O> optType = this.registry.getById(valueTypeId); if (!optType.isPresent()) { throw new IllegalStateException("Unknown type id: " + valueTypeId); }//from w ww. j a va 2s. c o m //noinspection unchecked final TypeAdapter<V> delegateTypeAdapter = gson.getDelegateAdapter(this, (TypeToken<V>) TypeToken.get(optType.get().getType())); try { return delegateTypeAdapter.fromJsonTree(obj.get(DATA)); } catch (Exception e) { throw new IllegalStateException( "Failed to compile the " + this.typeToken.toString() + " from the json: " + obj.toString(), e); } }
From source file:org.lanternpowered.server.util.option.Option.java
License:MIT License
public static <V> Option<V> of(String id, Class<V> type) { return new Option<>(checkId(id), TypeToken.get(type), null); }
From source file:org.lanternpowered.server.util.option.Option.java
License:MIT License
public static <V> Option<V> of(String id, Class<V> type, @Nullable V defaultValue) { return new Option<>(checkId(id), TypeToken.get(type), defaultValue); }
From source file:org.microbule.util.jaxrs.WebTargetUtils.java
License:Apache License
public static <T> Optional<T> parseJsonResponse(Response response, Class<T> type) { return parseJsonResponse(response, TypeToken.get(type)); }
From source file:org.mule.runtime.extension.internal.persistence.DefaultImplementationTypeAdapterFactory.java
License:Open Source License
/** * @param gson The actual Gson serializer * @param type Implementation that GSON is trying to find a {@link TypeAdapter} * @param <C> type of objects that the {@link TypeAdapter} will create * @return if {@param type} is subclass of {@link #superClass} a {@link TypeAdapter}, that serializes and deserialize * {@link C} instances/*from ww w. ja v a2s .c om*/ */ @Override public <C> TypeAdapter<C> create(Gson gson, TypeToken<C> type) { if (superClass.isAssignableFrom(type.getRawType())) { return gson.getDelegateAdapter(this, TypeToken.get((Class<C>) clazz)); } return null; }
From source file:org.mule.runtime.extension.internal.persistence.OperationModelTypeAdapter.java
License:Open Source License
@Override public OperationModel read(JsonReader in) throws IOException { JsonObject json = new JsonParser().parse(in).getAsJsonObject(); if (!json.has(KIND)) { throw new IllegalArgumentException("Invalid json. Operation doesn't specify " + KIND); }// ww w. j av a2 s . c o m String kind = json.get(KIND).getAsString(); Class<? extends OperationModel> operationClass; if (OPERATION_KIND.equals(kind)) { operationClass = ImmutableOperationModel.class; } else if (SCOPE_KIND.equals(kind)) { operationClass = ImmutableScopeModel.class; } else if (ROUTER_KIND.equals(kind)) { operationClass = ImmutableRouterModel.class; } else { throw new IllegalArgumentException("Invalid json. Operation specifies unknown kind: " + kind); } return gson.getDelegateAdapter(typeAdapterFactory, TypeToken.get(operationClass)).fromJsonTree(json); }
From source file:org.mule.runtime.extension.internal.persistence.OperationModelTypeAdapter.java
License:Open Source License
private TypeAdapter<OperationModel> getDelegateAdapter(OperationModel value) { Class operationClass;/*from w w w. j a va 2 s . c o m*/ if (value instanceof ScopeModel) { operationClass = ImmutableScopeModel.class; } else if (value instanceof RouterModel) { operationClass = ImmutableRouterModel.class; } else { operationClass = ImmutableOperationModel.class; } return gson.getDelegateAdapter(typeAdapterFactory, TypeToken.get(operationClass)); }