Skip to content

Instantly share code, notes, and snippets.

@toastkidjp
Last active August 12, 2017 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toastkidjp/252bc2ac86de2c0a3625b9fed0b9fc62 to your computer and use it in GitHub Desktop.
Save toastkidjp/252bc2ac86de2c0a3625b9fed0b9fc62 to your computer and use it in GitHub Desktop.
package jp.toastkid.tab;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import okio.Buffer;
import org.junit.jupiter.api.Test;
import java.io.IOException;
/**
* Created by toastkidjp on 2017/08/12.
*/
public class TabTest {
@Test
public void test() {
final Tab tab = new Tab();
tab.setThumbnailPath("file://~~");
tab.setLastTitle("Google");
tab.addHistory(History.make("Title", "URL"));
final Moshi moshi = new Moshi.Builder().build();
final JsonAdapter<Tab> tabJsonAdapter = moshi.adapter(Tab.class);
        // Pretty Printing
final Buffer buffer = new Buffer();
final JsonWriter prettyPrintWriter = JsonWriter.of(buffer);
prettyPrintWriter.setIndent(" ");
try {
tabJsonAdapter.toJson(prettyPrintWriter, tab);
} catch (IOException e) {
e.printStackTrace();
}
final String json = buffer.readUtf8();
System.out.println(json);
// Not pretty printing.
//final String json = tabJsonAdapter.toJson(tab);
try {
final Tab fromJson = tabJsonAdapter.fromJson(json);
System.out.println(fromJson.getLastTitle());
System.out.println(fromJson.getThumbnailPath());
System.out.println(fromJson.getLatest().title());
System.out.println(fromJson.getLatest().url());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment