Android Open Source - tape Gson Converter






From Project

Back to project page tape.

License

The source code is released under:

Apache License

If you think the Android project tape listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

// Copyright 2012 Square, Inc.
package com.squareup.tape.sample;
//from   w  w w.  j  a v  a  2s. com
import com.google.gson.Gson;
import com.squareup.tape.FileObjectQueue;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

/**
 * Use GSON to serialize classes to a bytes.
 * <p>
 * Note: This will only work when concrete classes are specified for {@code T}. If you want to specify an interface for
 * {@code T} then you need to also include the concrete class name in the serialized byte array so that you can
 * deserialize to the appropriate type.
 */
public class GsonConverter<T> implements FileObjectQueue.Converter<T> {
  private final Gson gson;
  private final Class<T> type;

  public GsonConverter(Gson gson, Class<T> type) {
    this.gson = gson;
    this.type = type;
  }

  @Override public T from(byte[] bytes) {
    Reader reader = new InputStreamReader(new ByteArrayInputStream(bytes));
    return gson.fromJson(reader, type);
  }

  @Override public void toStream(T object, OutputStream bytes) throws IOException {
    Writer writer = new OutputStreamWriter(bytes);
    gson.toJson(object, writer);
    writer.close();
  }
}




Java Source Code List

com.squareup.tape.FileException.java
com.squareup.tape.FileObjectQueue.java
com.squareup.tape.InMemoryObjectQueue.java
com.squareup.tape.ObjectQueue.java
com.squareup.tape.QueueFile.java
com.squareup.tape.SerializedConverter.java
com.squareup.tape.TaskInjector.java
com.squareup.tape.TaskQueue.java
com.squareup.tape.Task.java
com.squareup.tape.sample.GsonConverter.java
com.squareup.tape.sample.ImageUploadQueueSizeEvent.java
com.squareup.tape.sample.ImageUploadSuccessEvent.java
com.squareup.tape.sample.ImageUploadTaskQueue.java
com.squareup.tape.sample.ImageUploadTaskService.java
com.squareup.tape.sample.ImageUploadTask.java
com.squareup.tape.sample.SampleActivity.java
com.squareup.tape.sample.SampleApplication.java