Last active
August 12, 2017 03:02
-
-
Save toastkidjp/252bc2ac86de2c0a3625b9fed0b9fc62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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