Search in sources :

Example 1 with Rfc3339DateJsonAdapter

use of com.squareup.moshi.Rfc3339DateJsonAdapter in project moshi by square.

the class ReadAndWriteRfc3339Dates method run.

public void run() throws Exception {
    Moshi moshi = new Moshi.Builder().add(Date.class, new Rfc3339DateJsonAdapter()).build();
    JsonAdapter<Tournament> jsonAdapter = moshi.adapter(Tournament.class);
    // The RFC3339 JSON adapter can read dates with a timezone offset like '-05:00'.
    String lastTournament = "" + "{" + "  \"location\":\"Chainsaw\"," + "  \"name\":\"21 for 21\"," + "  \"start\":\"2015-09-01T20:00:00-05:00\"" + "}";
    System.out.println("Last tournament: " + jsonAdapter.fromJson(lastTournament));
    // The RFC3339 JSON adapter always writes dates with UTC, using a 'Z' suffix.
    Tournament nextTournament = new Tournament("Waterloo Classic", "Bauer Kitchen", newDate(2015, 10, 1, 20, -5));
    System.out.println("Next tournament JSON: " + jsonAdapter.toJson(nextTournament));
}
Also used : Moshi(com.squareup.moshi.Moshi) Rfc3339DateJsonAdapter(com.squareup.moshi.Rfc3339DateJsonAdapter) Tournament(com.squareup.moshi.recipes.models.Tournament) Date(java.util.Date)

Aggregations

Moshi (com.squareup.moshi.Moshi)1 Rfc3339DateJsonAdapter (com.squareup.moshi.Rfc3339DateJsonAdapter)1 Tournament (com.squareup.moshi.recipes.models.Tournament)1 Date (java.util.Date)1