Example usage for com.google.gson GsonBuilder registerTypeHierarchyAdapter

List of usage examples for com.google.gson GsonBuilder registerTypeHierarchyAdapter

Introduction

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

Prototype

@SuppressWarnings({ "unchecked", "rawtypes" })
public GsonBuilder registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter) 

Source Link

Document

Configures Gson for custom serialization or deserialization for an inheritance type hierarchy.

Usage

From source file:org.eclipse.leshan.server.demo.servlet.ObjectSpecServlet.java

License:Open Source License

public ObjectSpecServlet(LwM2mModelProvider pModelProvider) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeHierarchyAdapter(ObjectModel.class, new ObjectModelSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(ResourceModel.class, new ResourceModelSerializer());
    this.gson = gsonBuilder.create();

    // use the provider from the server and return a model by client
    modelProvider = pModelProvider;// w  w  w  .  j  a  va 2  s. c  om
}

From source file:org.eclipse.leshan.standalone.servlet.ClientServlet.java

License:Open Source License

public ClientServlet(LwM2mServer server) {
    this.server = server;

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeHierarchyAdapter(Client.class, new ClientSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mResponse.class, new ResponseSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeDeserializer());
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    this.gson = gsonBuilder.create();
}

From source file:org.eclipse.leshan.standalone.servlet.EventServlet.java

License:Open Source License

public EventServlet(LeshanServer server) {
    server.getClientRegistry().addListener(this.clientRegistryListener);
    server.getObservationRegistry().addListener(this.observationRegistryListener);

    // add an interceptor to each endpoint to trace all CoAP messages
    coapMessageTracer = new CoapMessageTracer(server.getClientRegistry());
    for (Endpoint endpoint : server.getCoapServer().getEndpoints()) {
        endpoint.addInterceptor(coapMessageTracer);
    }//from   ww w.  jav a  2  s. c o m

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeHierarchyAdapter(Client.class, new ClientSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeSerializer());
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    this.gson = gsonBuilder.create();
}

From source file:org.eclipse.leshan.standalone.servlet.ObjectSpecServlet.java

License:Open Source License

public ObjectSpecServlet() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeHierarchyAdapter(ObjectModel.class, new ObjectModelSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(ResourceModel.class, new ResourceModelSerializer());
    this.gson = gsonBuilder.create();

    // TODO use the provider from the server and return a model by client
    modelProvider = new StandardModelProvider();
}

From source file:org.fenixedu.bennu.spring.FenixEDUBaseController.java

License:Open Source License

protected void registerTypeAdapters(final GsonBuilder builder) {
    builder.registerTypeAdapter(LocalizedString.class, new LocalizedStringAdapter());
    builder.registerTypeAdapter(Country.class, new CountryAdapter());
    builder.registerTypeAdapter(District.class, new DistrictAdapter());
    builder.registerTypeAdapter(Municipality.class, new MunicipalityAdapter());
    builder.registerTypeHierarchyAdapter(DomainObject.class, new DomainObjectAdapter());
}

From source file:org.headsupdev.agile.app.issues.rest.IssuesApi.java

License:Open Source License

@Override
public void setupJson(GsonBuilder builder) {
    super.setupJson(builder);

    builder.registerTypeHierarchyAdapter(Milestone.class, new MilestoneAdapter());
}

From source file:org.jclouds.json.config.GsonModule.java

License:Apache License

@SuppressWarnings("rawtypes")
@Provides/*from ww  w  . j av  a 2 s  . c  om*/
@Singleton
Gson provideGson(TypeAdapter<JsonBall> jsonAdapter, DateAdapter adapter, ByteListAdapter byteListAdapter,
        ByteArrayAdapter byteArrayAdapter, PropertiesAdapter propertiesAdapter, JsonAdapterBindings bindings,
        CredentialsAdapterFactory credentialsAdapterFactory, OptionalTypeAdapterFactory optional,
        SetTypeAdapterFactory set, ImmutableSetTypeAdapterFactory immutableSet, MapTypeAdapterFactory map,
        MultimapTypeAdapterFactory multimap, IterableTypeAdapterFactory iterable,
        CollectionTypeAdapterFactory collection, ListTypeAdapterFactory list,
        ImmutableListTypeAdapterFactory immutableList, FluentIterableTypeAdapterFactory fluentIterable,
        ImmutableMapTypeAdapterFactory immutableMap, DefaultExclusionStrategy exclusionStrategy) {

    FieldNamingStrategy serializationPolicy = new AnnotationOrNameFieldNamingStrategy(
            ImmutableSet.of(new ExtractSerializedName(), new ExtractNamed()));

    GsonBuilder builder = new GsonBuilder().setFieldNamingStrategy(serializationPolicy)
            .setExclusionStrategies(exclusionStrategy);

    // simple (type adapters)
    builder.registerTypeAdapter(Properties.class, propertiesAdapter.nullSafe());
    builder.registerTypeAdapter(Date.class, adapter.nullSafe());
    builder.registerTypeAdapter(byte[].class, byteArrayAdapter.nullSafe());
    builder.registerTypeAdapter(JsonBall.class, jsonAdapter.nullSafe());
    builder.registerTypeAdapterFactory(credentialsAdapterFactory);
    builder.registerTypeAdapterFactory(optional);
    builder.registerTypeAdapterFactory(iterable);
    builder.registerTypeAdapterFactory(collection);
    builder.registerTypeAdapterFactory(list);
    builder.registerTypeAdapter(new TypeToken<List<Byte>>() {
    }.getType(), byteListAdapter.nullSafe());
    builder.registerTypeAdapterFactory(immutableList);
    builder.registerTypeAdapterFactory(set);
    builder.registerTypeAdapterFactory(immutableSet);
    builder.registerTypeAdapterFactory(map);
    builder.registerTypeAdapterFactory(multimap);
    builder.registerTypeAdapterFactory(fluentIterable);
    builder.registerTypeAdapterFactory(immutableMap);

    AnnotationConstructorNamingStrategy deserializationPolicy = new AnnotationConstructorNamingStrategy(
            ImmutableSet.of(ConstructorProperties.class, SerializedNames.class, Inject.class),
            ImmutableSet.of(new ExtractNamed()));

    builder.registerTypeAdapterFactory(new DeserializationConstructorAndReflectiveTypeAdapterFactory(
            new ConstructorConstructor(ImmutableMap.<Type, InstanceCreator<?>>of()), serializationPolicy,
            Excluder.DEFAULT, deserializationPolicy));

    // complicated (serializers/deserializers as they need context to operate)
    builder.registerTypeHierarchyAdapter(Enum.class, new EnumTypeAdapterThatReturnsFromValue());

    for (Map.Entry<Type, Object> binding : bindings.getBindings().entrySet()) {
        builder.registerTypeAdapter(binding.getKey(), binding.getValue());
    }

    for (TypeAdapterFactory factory : bindings.getFactories()) {
        builder.registerTypeAdapterFactory(factory);
    }

    return builder.create();
}

From source file:org.openhab.io.neeo.internal.NeeoUtil.java

License:Open Source License

/**
 * Builds and returns a {@link GsonBuilder} suitable to serialize a {@link NeeoDevice} using the specified
 * {@link NeeoService} and {@link ServiceContext}. Will call {@link #createGsonBuilder()} and then add a number of
 * NEEO specific serializers. This will use the {@link NeeoDeviceSerializer} - do not use this call if you need a
 * {@link NeeoBrainDeviceSerializer}//from   w  w w.j  a v a 2s . co  m
 *
 * @param service a possibly null {@link NeeoService}
 * @param context a possibly null {@link ServiceContext}
 * @return a non-null {@link GsonBuilder} to use
 */
public static GsonBuilder createNeeoDeviceGsonBuilder(@Nullable NeeoService service,
        @Nullable ServiceContext context) {
    final GsonBuilder gsonBuilder = createGsonBuilder();
    gsonBuilder.registerTypeAdapter(NeeoThingUID.class, new NeeoThingUIDSerializer());
    gsonBuilder.registerTypeAdapter(NeeoDeviceChannelKind.class, new NeeoDeviceChannelKindSerializer());
    gsonBuilder.registerTypeAdapter(NeeoCapabilityType.class, new NeeoCapabilityTypeSerializer());
    gsonBuilder.registerTypeAdapter(ItemSubType.class, new ItemSubTypeSerializer());
    gsonBuilder.registerTypeAdapter(ListUiAction.class, new ListUiActionSerializer());
    gsonBuilder.registerTypeHierarchyAdapter(NeeoDeviceChannel.class, new NeeoDeviceChannelSerializer(context));
    gsonBuilder.registerTypeAdapter(NeeoDeviceType.class, new NeeoDeviceTypeSerializer());
    gsonBuilder.registerTypeAdapter(NeeoDevice.class, new NeeoDeviceSerializer(service, context));
    return gsonBuilder;
}

From source file:org.projectforge.rest.JsonUtils.java

License:Open Source License

private static Gson createGson() {
    final GsonBuilder builder = new GsonBuilder()//
            .registerTypeAdapter(java.sql.Date.class, new UTCDateTypeAdapter())//
            .registerTypeAdapter(java.util.Date.class, new UTCDateTimeTypeAdapter());
    for (final Map.Entry<Class<?>, Object> entry : typeAdapterMap.entrySet()) {
        builder.registerTypeHierarchyAdapter(entry.getKey(), entry.getValue());
    }//from   w w w .j av  a  2 s. c o m
    return builder.create();
}

From source file:org.springframework.data.cloudant.core.CloudantTemplate.java

License:Apache License

public void setDataAdapter(Map<Class<?>, Object> adapters) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(DateTime.class, new DateTimeDataAdapter());
    gsonBuilder.registerTypeHierarchyAdapter(BaseDocument.class, new UnmappedDataAdapter());
    if (adapters != null) {
        for (Class<?> type : adapters.keySet()) {
            gsonBuilder.registerTypeAdapter(type, adapters.get(type));
        }//from   w  w  w  .  j  a  va 2  s  .c  o m
    }
    gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    this.client.setGsonBuilder(gsonBuilder);
}