diff --git a/examples/pom.xml b/examples/pom.xml index 2677093..3138eb1 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -17,5 +17,10 @@ moshi ${project.version} + + com.squareup.moshi + moshi-adapters + ${project.version} + diff --git a/examples/src/main/java/com/squareup/moshi/recipes/CardAdapter.java b/examples/src/main/java/com/squareup/moshi/recipes/CardAdapter.java index 641f748..ff8a573 100644 --- a/examples/src/main/java/com/squareup/moshi/recipes/CardAdapter.java +++ b/examples/src/main/java/com/squareup/moshi/recipes/CardAdapter.java @@ -18,6 +18,8 @@ package com.squareup.moshi.recipes; import com.squareup.moshi.FromJson; import com.squareup.moshi.JsonDataException; import com.squareup.moshi.ToJson; +import com.squareup.moshi.recipes.models.Card; +import com.squareup.moshi.recipes.models.Suit; public final class CardAdapter { @ToJson String toJson(Card card) { diff --git a/examples/src/main/java/com/squareup/moshi/recipes/CustomTypeAdapter.java b/examples/src/main/java/com/squareup/moshi/recipes/CustomTypeAdapter.java index 959feb2..3424d22 100644 --- a/examples/src/main/java/com/squareup/moshi/recipes/CustomTypeAdapter.java +++ b/examples/src/main/java/com/squareup/moshi/recipes/CustomTypeAdapter.java @@ -17,6 +17,7 @@ package com.squareup.moshi.recipes; import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.Moshi; +import com.squareup.moshi.recipes.models.BlackjackHand; public final class CustomTypeAdapter { public void run() throws Exception { diff --git a/examples/src/main/java/com/squareup/moshi/recipes/ReadAndWriteRfc3339Dates.java b/examples/src/main/java/com/squareup/moshi/recipes/ReadAndWriteRfc3339Dates.java new file mode 100644 index 0000000..b48090a --- /dev/null +++ b/examples/src/main/java/com/squareup/moshi/recipes/ReadAndWriteRfc3339Dates.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2015 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.moshi.recipes; + +import com.squareup.moshi.JsonAdapter; +import com.squareup.moshi.Moshi; +import com.squareup.moshi.Rfc3339DateJsonAdapter; +import com.squareup.moshi.recipes.models.Tournament; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; +import java.util.concurrent.TimeUnit; + +public final class ReadAndWriteRfc3339Dates { + public void run() throws Exception { + Moshi moshi = new Moshi.Builder() + .add(Date.class, new Rfc3339DateJsonAdapter()) + .build(); + JsonAdapter 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)); + } + + public static void main(String[] args) throws Exception { + new ReadAndWriteRfc3339Dates().run(); + } + + private Date newDate(int year, int month, int day, int hour, int offset) { + Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT")); + calendar.set(year, month - 1, day, hour, 0, 0); + calendar.set(Calendar.MILLISECOND, 0); + return new Date(calendar.getTimeInMillis() - TimeUnit.HOURS.toMillis(offset)); + } +} diff --git a/examples/src/main/java/com/squareup/moshi/recipes/ReadJson.java b/examples/src/main/java/com/squareup/moshi/recipes/ReadJson.java index 8e5f9cf..b8d459c 100644 --- a/examples/src/main/java/com/squareup/moshi/recipes/ReadJson.java +++ b/examples/src/main/java/com/squareup/moshi/recipes/ReadJson.java @@ -17,6 +17,7 @@ package com.squareup.moshi.recipes; import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.Moshi; +import com.squareup.moshi.recipes.models.BlackjackHand; public final class ReadJson { public void run() throws Exception { diff --git a/examples/src/main/java/com/squareup/moshi/recipes/ReadJsonList.java b/examples/src/main/java/com/squareup/moshi/recipes/ReadJsonList.java index d1a8ab6..0919f8c 100644 --- a/examples/src/main/java/com/squareup/moshi/recipes/ReadJsonList.java +++ b/examples/src/main/java/com/squareup/moshi/recipes/ReadJsonList.java @@ -18,12 +18,14 @@ package com.squareup.moshi.recipes; import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.Moshi; import com.squareup.moshi.Types; +import com.squareup.moshi.recipes.models.Card; import java.lang.reflect.Type; import java.util.List; public final class ReadJsonList { public void run() throws Exception { - String json = "[\n" + String json = "" + + "[\n" + " {\n" + " \"rank\": \"4\",\n" + " \"suit\": \"CLUBS\"\n" diff --git a/examples/src/main/java/com/squareup/moshi/recipes/WriteJson.java b/examples/src/main/java/com/squareup/moshi/recipes/WriteJson.java index e232cda..f182d0c 100644 --- a/examples/src/main/java/com/squareup/moshi/recipes/WriteJson.java +++ b/examples/src/main/java/com/squareup/moshi/recipes/WriteJson.java @@ -17,11 +17,13 @@ package com.squareup.moshi.recipes; import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.Moshi; +import com.squareup.moshi.recipes.models.BlackjackHand; +import com.squareup.moshi.recipes.models.Card; import java.util.Arrays; -import static com.squareup.moshi.recipes.Suit.CLUBS; -import static com.squareup.moshi.recipes.Suit.HEARTS; -import static com.squareup.moshi.recipes.Suit.SPADES; +import static com.squareup.moshi.recipes.models.Suit.CLUBS; +import static com.squareup.moshi.recipes.models.Suit.HEARTS; +import static com.squareup.moshi.recipes.models.Suit.SPADES; public final class WriteJson { public void run() throws Exception { diff --git a/examples/src/main/java/com/squareup/moshi/recipes/BlackjackHand.java b/examples/src/main/java/com/squareup/moshi/recipes/models/BlackjackHand.java similarity index 95% rename from examples/src/main/java/com/squareup/moshi/recipes/BlackjackHand.java rename to examples/src/main/java/com/squareup/moshi/recipes/models/BlackjackHand.java index fccee88..ad785d6 100644 --- a/examples/src/main/java/com/squareup/moshi/recipes/BlackjackHand.java +++ b/examples/src/main/java/com/squareup/moshi/recipes/models/BlackjackHand.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.squareup.moshi.recipes; +package com.squareup.moshi.recipes.models; import java.util.List; diff --git a/examples/src/main/java/com/squareup/moshi/recipes/Card.java b/examples/src/main/java/com/squareup/moshi/recipes/models/Card.java similarity index 95% rename from examples/src/main/java/com/squareup/moshi/recipes/Card.java rename to examples/src/main/java/com/squareup/moshi/recipes/models/Card.java index cd0a874..33980e5 100644 --- a/examples/src/main/java/com/squareup/moshi/recipes/Card.java +++ b/examples/src/main/java/com/squareup/moshi/recipes/models/Card.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.squareup.moshi.recipes; +package com.squareup.moshi.recipes.models; public final class Card { public final char rank; diff --git a/examples/src/main/java/com/squareup/moshi/recipes/Suit.java b/examples/src/main/java/com/squareup/moshi/recipes/models/Suit.java similarity index 94% rename from examples/src/main/java/com/squareup/moshi/recipes/Suit.java rename to examples/src/main/java/com/squareup/moshi/recipes/models/Suit.java index 536b35f..86cb5c3 100644 --- a/examples/src/main/java/com/squareup/moshi/recipes/Suit.java +++ b/examples/src/main/java/com/squareup/moshi/recipes/models/Suit.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.squareup.moshi.recipes; +package com.squareup.moshi.recipes.models; public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES; diff --git a/examples/src/main/java/com/squareup/moshi/recipes/models/Tournament.java b/examples/src/main/java/com/squareup/moshi/recipes/models/Tournament.java new file mode 100644 index 0000000..38c7f05 --- /dev/null +++ b/examples/src/main/java/com/squareup/moshi/recipes/models/Tournament.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2015 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.squareup.moshi.recipes.models; + +import java.util.Date; + +public final class Tournament { + public final String name; + public final String location; + public final Date start; + + public Tournament(String name, String location, Date start) { + this.name = name; + this.location = location; + this.start = start; + } + + @Override public String toString() { + return name + " at " + location + " on " + start; + } +}