Example usage for com.google.gson GsonBuilder registerTypeAdapter

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

Introduction

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

Prototype

@SuppressWarnings({ "unchecked", "rawtypes" })
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) 

Source Link

Document

Configures Gson for custom serialization or deserialization.

Usage

From source file:com.shakirov.dragonsofmugloar.controller.DragonClassSerializer.java

public String toJson() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Dragon.class, this);
    Gson gson = builder.create();/*from  w  w w  .j av a 2  s .c om*/
    return gson.toJson(this.dragon);
}

From source file:com.shazam.shazamcrest.matcher.GsonProvider.java

License:Apache License

/**
  * Returns a {@link Gson} instance containing {@link ExclusionStrategy} based on the object types to ignore during
  * serialisation.//from   w  w w. j  a v  a 2 s .  c o m
  *
  * @param typesToIgnore the object types to exclude from serialisation
  * @param circularReferenceTypes cater for circular referenced objects
  * @return an instance of {@link Gson}
  */
public static Gson gson(final List<Class<?>> typesToIgnore, final List<Matcher<String>> fieldsToIgnore,
        Set<Class<?>> circularReferenceTypes) {
    final GsonBuilder gsonBuilder = initGson();

    if (!circularReferenceTypes.isEmpty()) {
        registerCircularReferenceTypes(circularReferenceTypes, gsonBuilder);
    }

    gsonBuilder.registerTypeAdapter(Optional.class, new OptionalSerializer());

    registerSetSerialisation(gsonBuilder);

    registerMapSerialisation(gsonBuilder);

    markSetAndMapFields(gsonBuilder);

    registerExclusionStrategies(gsonBuilder, typesToIgnore, fieldsToIgnore);

    return gsonBuilder.create();
}

From source file:com.sheepdog.mashmesh.integration.IntegrationTestHelper.java

License:Apache License

/**
 * Serialize a list of AvailableTimePeriod instances to JSON. Used to stub in a replacement
 * for available.js, because HttpUnit has no support for driving drag events.
 * @param availableTimePeriods the list of AvailableTimePeriods to serialize
 * @return a JSON string containing the serialized available time periods.
 */// w  w  w .j a va 2s  . c om
public String serializeAvailability(List<AvailableTimePeriod> availableTimePeriods) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(AvailableTimePeriod.class, new AvailableTimePeriodAdapter());
    Gson gson = gsonBuilder.create();
    return gson.toJson(availableTimePeriods);
}

From source file:com.sheepdog.mashmesh.servlets.EditUserProfileServlet.java

License:Apache License

private Gson getGsonForAvailableTimePeriods() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(AvailableTimePeriod.class, new AvailableTimePeriodAdapter());
    return gsonBuilder.create();
}

From source file:com.shopify.buy.dataprovider.BuyClientFactory.java

License:Open Source License

public static Gson createDefaultGson(Class forClass) {

    GsonBuilder builder = new GsonBuilder().setDateFormat(DateUtility.DEFAULT_DATE_PATTERN)
            .registerTypeAdapter(Date.class, new DateDeserializer());

    if (!Product.class.equals(forClass)) {
        builder.registerTypeAdapter(Product.class, new ProductDeserializer());
    }//  ww w.j ava 2  s .  c om

    if (!Checkout.class.equals(forClass)) {
        builder.registerTypeAdapter(Checkout.class, new CheckoutSerializer());
        builder.registerTypeAdapter(Checkout.class, new CheckoutDeserializer());
    }

    return builder.create();
}

From source file:com.shopify.buy.dataprovider.BuyClientUtils.java

License:Open Source License

public static Gson createDefaultGson(Class forClass) {

    GsonBuilder builder = new GsonBuilder().setDateFormat(DateUtility.DEFAULT_DATE_PATTERN)
            .registerTypeAdapter(Date.class, new DateUtility.DateDeserializer());

    if (!Product.class.equals(forClass)) {
        builder.registerTypeAdapter(Product.class, new Product.ProductDeserializer());
    }//from   www.j ava  2 s .c  o m

    if (!Checkout.class.equals(forClass)) {
        builder.registerTypeAdapter(Checkout.class, new Checkout.CheckoutSerializer());
        builder.registerTypeAdapter(Checkout.class, new Checkout.CheckoutDeserializer());
    }

    return builder.create();
}

From source file:com.sias.util.GSONConverter.java

private static Gson getGSON() {
    if (gson == null) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(Date.class, new CustomDateJsonSerializer());
        gson = gsonBuilder.create();//  w  ww. j a v  a  2  s  .  c o  m
    }
    return gson;
}

From source file:com.sk89q.worldedit.util.gson.GsonUtil.java

License:Open Source License

/**
 * Create a standard {@link GsonBuilder} for WorldEdit.
 *
 * @return a builder//from   w  w w  .jav  a  2s  . c om
 */
public static GsonBuilder createBuilder() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Vector.class, new VectorAdapter());
    return gsonBuilder;
}

From source file:com.sk89q.worldedit.world.registry.BundledBlockData.java

License:Open Source License

/**
 * Attempt to load the data from file./*from w w  w  .  j  av a  2s . c  om*/
 *
 * @throws IOException thrown on I/O error
 */
private void loadFromResource() throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Vector.class, new VectorAdapter());
    Gson gson = gsonBuilder.create();
    URL url = BundledBlockData.class.getResource("blocks.json");
    if (url == null) {
        throw new IOException("Could not find blocks.json");
    }
    String data = Resources.toString(url, Charset.defaultCharset());
    List<BlockEntry> entries = gson.fromJson(data, new TypeToken<List<BlockEntry>>() {
    }.getType());

    for (BlockEntry entry : entries) {
        entry.postDeserialization();
        idMap.put(entry.id, entry);
        legacyMap.put(entry.legacyId, entry);
    }
}

From source file:com.sk89q.worldedit.world.registry.BundledItemData.java

License:Open Source License

/**
 * Attempt to load the data from file.//from   ww w . j a  v  a  2 s  .c  o m
 *
 * @throws IOException thrown on I/O error
 */
private void loadFromResource() throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
    Gson gson = gsonBuilder.create();
    URL url = BundledItemData.class.getResource("items.json");
    if (url == null) {
        throw new IOException("Could not find items.json");
    }
    String data = Resources.toString(url, Charset.defaultCharset());
    List<ItemEntry> entries = gson.fromJson(data, new TypeToken<List<ItemEntry>>() {
    }.getType());

    for (ItemEntry entry : entries) {
        idMap.put(entry.id, entry);
    }
}