Example usage for com.google.gson GsonBuilder create

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

Introduction

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

Prototype

public Gson create() 

Source Link

Document

Creates a Gson instance based on the current configuration.

Usage

From source file:com.intellij.tasks.trello.TrelloUtil.java

License:Apache License

private static Gson buildGson() {
    GsonBuilder gson = new GsonBuilder();
    gson.registerTypeAdapter(Date.class, new DateDeserializer());
    gson.registerTypeAdapter(LabelColor.class, new LabelColorDeserializer());
    return gson.create();
}

From source file:com.it.xushuai.baseapp.core.client.GsonUtils.java

License:Open Source License

/**
 * Create the standard {@link Gson} configuration
 *
 * @param serializeNulls/* www.  j  av a 2 s .co  m*/
 *            whether nulls should be serialized
 *
 * @return created gson, never null
 */
public static final Gson createGson(final boolean serializeNulls) {
    final GsonBuilder builder = new GsonBuilder();
    //      builder.registerTypeAdapter(Date.class, new DateFormatter());
    //      builder.registerTypeAdapter(Event.class, new EventFormatter());
    builder.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES);
    if (serializeNulls)
        builder.serializeNulls();
    return builder.create();
}

From source file:com.iyonger.apm.web.controller.FileEntryController.java

License:Apache License

/**
 * Initialize.//w  w  w  .  j a  v a2  s  .c o  m
 */
@PostConstruct
public void init() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(FileEntry.class, new FileEntry.FileEntrySerializer());
    fileEntryGson = gsonBuilder.create();
}

From source file:com.jalepo.interfacedemo.ConnectionModule.java

License:Apache License

/**
 * Provides a GSON instance for the Retrofit converter.  Used internally by this module.
 * @return GSON object//from w  w  w  . ja v  a  2s  .  com
 */
@Provides
@Singleton
Gson provideGson() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    return gsonBuilder.create();
}

From source file:com.jamesmorrisstudios.appbaselibrary.Serializer.java

License:Apache License

/**
 * Serializes a generic class.//w w  w. j  a v a2  s  .  c om
 *
 * @return The byte array of the save. Null on error
 */
@Nullable
public static byte[] serializeClass(@NonNull final Object obj, final boolean useCompression) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();
    try {
        byte[] data = gson.toJsonTree(obj).toString().getBytes(Charset.forName(Utils.stringType));
        if (useCompression) {
            return UtilsCompression.compress(data);
        } else {
            return data;
        }
    } catch (Exception e) {
        return null;
    }
}

From source file:com.jamesmorrisstudios.appbaselibrary.Serializer.java

License:Apache License

/**
 * @param bytes Byte array/*www.j  av a2 s.  com*/
 * @param clazz Class type
 * @param <T>   Generic...
 * @return The deserialized class. Null on error
 */
@Nullable
private static <T> T deserializeClassInternal(@NonNull final byte[] bytes, @NonNull final Class<T> clazz) {
    String st;
    try {
        st = new String(bytes, Utils.stringType);
    } catch (Exception e1) {
        //Log.v("Serializer", "Failed to deserialize: String conversion");
        return null;
    }
    try {
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        return gson.fromJson(st, clazz);
    } catch (Exception e) {
        e.printStackTrace();
        //Log.v("Serializer", "Failed to deserialize: builder: "+st);
        return null;
    }
}

From source file:com.jameswolfeoliver.pigeon.Dependancies.Modules.DefaultWebModule.java

License:Apache License

@Provides
@App
@Override
public Gson provideGson() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    return gsonBuilder.create();
}

From source file:com.jamierf.persistenthashmap.serializers.GsonSerializer.java

License:GNU General Public License

public GsonSerializer() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(ObjectContainer.class, new GenericTypeAdapter());
    builder.setDateFormat("yyMMddHHmmssSSSZ");
    gson = builder.create();
}

From source file:com.javacreed.examples.gson.part1.Main.java

License:Apache License

public static void main(final String[] args) throws IOException {
    // Configure GSON
    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Book.class, new BookTypeAdapter());
    gsonBuilder.setPrettyPrinting();/*from w  w  w . ja va2  s  .c  o m*/

    final Gson gson = gsonBuilder.create();

    final Book book = new Book();
    book.setAuthors(new Author[] { new Author(1, "Joshua Bloch") });
    book.setTitle("Effective Java");
    book.setIsbn("978-0321356680");

    final String json = gson.toJson(book);
    System.out.println(json);
}

From source file:com.javacreed.examples.gson.part2.Main.java

License:Apache License

public static void main(final String[] args) throws IOException {
    // Configure GSON
    final DataTypeAdapterFactory.Builder dtafBuilder = new DataTypeAdapterFactory.Builder();
    dtafBuilder.add(Book.class, new BookTypeAdapter());

    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(dtafBuilder.build());
    gsonBuilder.setPrettyPrinting();/* www .  j a  v a 2  s  .c  om*/

    final Gson gson = gsonBuilder.create();

    final Book book = new Book();
    book.setAuthors(new Author[] { new Author(1, "Joshua Bloch") });
    book.setTitle("Effective Java");
    book.setIsbn("978-0321356680");

    final String json = gson.toJson(book);
    System.out.println(json);
}