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.indragie.cmput301as1.Session.java

License:Open Source License

/**
 * Creates a new instance of {@link Session}
 * @param context The current context./* w w  w  .j av a2s  .  c  o m*/
 * @param user The user that the session belongs to.
 */
public Session(Context context, User user) {
    this.user = user;
    this.context = context;

    // Sort by starting date descending by default
    Comparator<ExpenseClaim> comparator = new StartDateDescendingComparator();

    ownedClaims = new ListModel<ExpenseClaim>(modelFilename(OWNED_CLAIMS_FILENAME_PREFIX, user), context);
    ownedClaims.addObserver(this);
    ownedClaims.setComparator(comparator);

    reviewalClaims = new ListModel<ExpenseClaim>(modelFilename(REVIEWAL_CLAIMS_FILENAME_PREFIX, user), context);
    reviewalClaims.addObserver(this);
    reviewalClaims.setComparator(comparator);

    pushQueue = new ElasticSearchQueue<ExpenseClaim>(context);
    pullQueue = new ElasticSearchQueue<List<ExpenseClaim>>(context);

    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new DateJSONSerializer());
    builder.registerTypeAdapter(Date.class, new DateJSONDeserializer());
    builder.registerTypeAdapter(ExpenseItem.class, new ExpenseItemJSONSerializer());
    builder.registerTypeAdapter(ExpenseItem.class,
            new ExpenseItemJSONDeserializer(new ExpenseItemReceiptController()));
    Gson gson = builder.create();

    apiClient = new ElasticSearchAPIClient(ElasticSearchConfiguration.getBaseURL(), gson);
}

From source file:com.intellij.tasks.jira.JiraUtil.java

License:Apache License

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

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.iyonger.apm.web.controller.FileEntryController.java

License:Apache License

/**
 * Initialize.// www .  j  a  v a  2s.  c  om
 */
@PostConstruct
public void init() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(FileEntry.class, new FileEntry.FileEntrySerializer());
    fileEntryGson = 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   w  w  w.java2  s. co  m*/
}

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();/* w ww. j  a  v  a 2s.  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.part3.Example2.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 BookSerialiser());
    gsonBuilder.setPrettyPrinting();/*from w ww.  jav  a2 s  .co m*/
    final Gson gson = gsonBuilder.create();

    final Author joshuaBloch = new Author();
    joshuaBloch.setId(1);
    joshuaBloch.setName("Joshua Bloch");

    final Author nealGafter = new Author();
    nealGafter.setId(2);
    nealGafter.setName("Neal Gafter");

    final Book javaPuzzlers = new Book();
    javaPuzzlers.setTitle("Java Puzzlers: Traps, Pitfalls, and Corner Cases");
    javaPuzzlers.setIsbn("032133678X");
    javaPuzzlers.setAuthors(new Author[] { joshuaBloch, nealGafter });

    final Book effectiveJava = new Book();
    effectiveJava.setTitle("Effective Java (2nd Edition)");
    effectiveJava.setIsbn("0321356683");
    effectiveJava.setAuthors(new Author[] { joshuaBloch });

    final Book[] books = new Book[] { javaPuzzlers, effectiveJava };

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

From source file:com.jbirdvegas.mgerrit.tasks.Deserializers.java

License:Apache License

public static void addDeserializers(@NotNull GsonBuilder gsonBuilder) {
    gsonBuilder.registerTypeAdapter(Projects.class, new Projects());
    gsonBuilder.registerTypeAdapter(Reviewer.class, d_reviewer);
    gsonBuilder.registerTypeAdapter(ReviewerList.class, d_reviewers);
    gsonBuilder.registerTypeAdapter(JSONCommit.class, d_commit);
}

From source file:com.jolira.st4j.impl.ServerTrackerImpl.java

License:Open Source License

@Override
public Collection<Map<String, Object>> proxyEvent(final Map<String, Object> eventInfo,
        final InputStream content) throws IllegalArgumentException {
    final GsonBuilder gsonBuilder = new GsonBuilder();

    gsonBuilder.registerTypeAdapter(Object.class, new NaturalDeserializer());

    final Gson parser = gsonBuilder.create();
    final Object _payload = parser.fromJson(new InputStreamReader(content), Object.class);

    LOG.info("proxying {}", _payload);

    if (_payload == null || !(_payload instanceof Map)) {
        throw new IllegalArgumentException("not a map: " + _payload);
    }/* ww w. j a  v a2  s. c  o  m*/

    @SuppressWarnings("unchecked")
    final Map<String, Object> payload = (Map<String, Object>) _payload;

    @SuppressWarnings("unchecked")
    final Collection<Map<String, Object>> _events = (Collection<Map<String, Object>>) payload.get("events");
    @SuppressWarnings("unchecked")
    final Collection<Map<String, Object>> _logs = (Collection<Map<String, Object>>) payload.get("logs");

    payload.remove("events");
    payload.remove("logs");
    merge(eventInfo, payload);

    if (_logs != null) {
        for (final Map<String, Object> log : _logs) {
            merge(payload, log);
            addLog(log);
        }
    }

    final Collection<Map<String, Object>> result = new ArrayList<Map<String, Object>>(1);

    if (_events != null) {
        for (final Map<String, Object> event : _events) {
            merge(payload, event);
            addEvent(event);
            result.add(event);
        }
    }

    return result;
}

From source file:com.julauncher.workers.InstanceInstaller.java

License:Open Source License

public InstanceInstaller(String instanceName, Pack pack, PackVersion version, boolean isReinstall,
        boolean isServer, String shareCode, boolean showModsChooser) {
    this.instanceName = instanceName;
    this.pack = pack;
    this.version = version;
    this.isReinstall = isReinstall;
    this.isServer = isServer;
    this.shareCode = shareCode;
    this.showModsChooser = showModsChooser;
    if (isServer) {
        serverLibraries = new ArrayList<File>();
    }//from   w  w w  . j a va 2s  .c om
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapterFactory(new EnumTypeAdapterFactory());
    builder.registerTypeAdapter(Date.class, new DateTypeAdapter());
    builder.registerTypeAdapter(File.class, new FileTypeAdapter());
    builder.setPrettyPrinting();
    this.gson = builder.create();
}