Date adapter example.
This commit is contained in:
parent
b6f48a9d94
commit
1f0e7f61ad
11 changed files with 114 additions and 7 deletions
|
@ -17,5 +17,10 @@
|
||||||
<artifactId>moshi</artifactId>
|
<artifactId>moshi</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.moshi</groupId>
|
||||||
|
<artifactId>moshi-adapters</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -18,6 +18,8 @@ package com.squareup.moshi.recipes;
|
||||||
import com.squareup.moshi.FromJson;
|
import com.squareup.moshi.FromJson;
|
||||||
import com.squareup.moshi.JsonDataException;
|
import com.squareup.moshi.JsonDataException;
|
||||||
import com.squareup.moshi.ToJson;
|
import com.squareup.moshi.ToJson;
|
||||||
|
import com.squareup.moshi.recipes.models.Card;
|
||||||
|
import com.squareup.moshi.recipes.models.Suit;
|
||||||
|
|
||||||
public final class CardAdapter {
|
public final class CardAdapter {
|
||||||
@ToJson String toJson(Card card) {
|
@ToJson String toJson(Card card) {
|
||||||
|
|
|
@ -17,6 +17,7 @@ package com.squareup.moshi.recipes;
|
||||||
|
|
||||||
import com.squareup.moshi.JsonAdapter;
|
import com.squareup.moshi.JsonAdapter;
|
||||||
import com.squareup.moshi.Moshi;
|
import com.squareup.moshi.Moshi;
|
||||||
|
import com.squareup.moshi.recipes.models.BlackjackHand;
|
||||||
|
|
||||||
public final class CustomTypeAdapter {
|
public final class CustomTypeAdapter {
|
||||||
public void run() throws Exception {
|
public void run() throws Exception {
|
||||||
|
|
|
@ -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<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));
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,6 +17,7 @@ package com.squareup.moshi.recipes;
|
||||||
|
|
||||||
import com.squareup.moshi.JsonAdapter;
|
import com.squareup.moshi.JsonAdapter;
|
||||||
import com.squareup.moshi.Moshi;
|
import com.squareup.moshi.Moshi;
|
||||||
|
import com.squareup.moshi.recipes.models.BlackjackHand;
|
||||||
|
|
||||||
public final class ReadJson {
|
public final class ReadJson {
|
||||||
public void run() throws Exception {
|
public void run() throws Exception {
|
||||||
|
|
|
@ -18,12 +18,14 @@ package com.squareup.moshi.recipes;
|
||||||
import com.squareup.moshi.JsonAdapter;
|
import com.squareup.moshi.JsonAdapter;
|
||||||
import com.squareup.moshi.Moshi;
|
import com.squareup.moshi.Moshi;
|
||||||
import com.squareup.moshi.Types;
|
import com.squareup.moshi.Types;
|
||||||
|
import com.squareup.moshi.recipes.models.Card;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public final class ReadJsonList {
|
public final class ReadJsonList {
|
||||||
public void run() throws Exception {
|
public void run() throws Exception {
|
||||||
String json = "[\n"
|
String json = ""
|
||||||
|
+ "[\n"
|
||||||
+ " {\n"
|
+ " {\n"
|
||||||
+ " \"rank\": \"4\",\n"
|
+ " \"rank\": \"4\",\n"
|
||||||
+ " \"suit\": \"CLUBS\"\n"
|
+ " \"suit\": \"CLUBS\"\n"
|
||||||
|
|
|
@ -17,11 +17,13 @@ package com.squareup.moshi.recipes;
|
||||||
|
|
||||||
import com.squareup.moshi.JsonAdapter;
|
import com.squareup.moshi.JsonAdapter;
|
||||||
import com.squareup.moshi.Moshi;
|
import com.squareup.moshi.Moshi;
|
||||||
|
import com.squareup.moshi.recipes.models.BlackjackHand;
|
||||||
|
import com.squareup.moshi.recipes.models.Card;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import static com.squareup.moshi.recipes.Suit.CLUBS;
|
import static com.squareup.moshi.recipes.models.Suit.CLUBS;
|
||||||
import static com.squareup.moshi.recipes.Suit.HEARTS;
|
import static com.squareup.moshi.recipes.models.Suit.HEARTS;
|
||||||
import static com.squareup.moshi.recipes.Suit.SPADES;
|
import static com.squareup.moshi.recipes.models.Suit.SPADES;
|
||||||
|
|
||||||
public final class WriteJson {
|
public final class WriteJson {
|
||||||
public void run() throws Exception {
|
public void run() throws Exception {
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.squareup.moshi.recipes;
|
package com.squareup.moshi.recipes.models;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.squareup.moshi.recipes;
|
package com.squareup.moshi.recipes.models;
|
||||||
|
|
||||||
public final class Card {
|
public final class Card {
|
||||||
public final char rank;
|
public final char rank;
|
|
@ -13,7 +13,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.squareup.moshi.recipes;
|
package com.squareup.moshi.recipes.models;
|
||||||
|
|
||||||
public enum Suit {
|
public enum Suit {
|
||||||
CLUBS, DIAMONDS, HEARTS, SPADES;
|
CLUBS, DIAMONDS, HEARTS, SPADES;
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue