mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Switch to spotless and format code (#1196)
* Add spotless configuration * Reformat! * Add copyright config for build.gradle.kts files * Add toeholds for headers
This commit is contained in:
@@ -26,9 +26,7 @@ public final class ByteStrings {
|
||||
public void run() throws Exception {
|
||||
String json = "\"TW9zaGksIE9saXZlLCBXaGl0ZSBDaGluPw\"";
|
||||
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(ByteString.class, new Base64ByteStringAdapter())
|
||||
.build();
|
||||
Moshi moshi = new Moshi.Builder().add(ByteString.class, new Base64ByteStringAdapter()).build();
|
||||
JsonAdapter<ByteString> jsonAdapter = moshi.adapter(ByteString.class);
|
||||
|
||||
ByteString byteString = jsonAdapter.fromJson(json);
|
||||
@@ -40,12 +38,14 @@ public final class ByteStrings {
|
||||
* breaks or whitespace is included in the encoded form.
|
||||
*/
|
||||
public final class Base64ByteStringAdapter extends JsonAdapter<ByteString> {
|
||||
@Override public ByteString fromJson(JsonReader reader) throws IOException {
|
||||
@Override
|
||||
public ByteString fromJson(JsonReader reader) throws IOException {
|
||||
String base64 = reader.nextString();
|
||||
return ByteString.decodeBase64(base64);
|
||||
}
|
||||
|
||||
@Override public void toJson(JsonWriter writer, ByteString value) throws IOException {
|
||||
@Override
|
||||
public void toJson(JsonWriter writer, ByteString value) throws IOException {
|
||||
String string = value.base64();
|
||||
writer.value(string);
|
||||
}
|
||||
|
@@ -22,20 +22,27 @@ import com.squareup.moshi.recipes.models.Card;
|
||||
import com.squareup.moshi.recipes.models.Suit;
|
||||
|
||||
public final class CardAdapter {
|
||||
@ToJson String toJson(Card card) {
|
||||
@ToJson
|
||||
String toJson(Card card) {
|
||||
return card.rank + card.suit.name().substring(0, 1);
|
||||
}
|
||||
|
||||
@FromJson Card fromJson(String card) {
|
||||
@FromJson
|
||||
Card fromJson(String card) {
|
||||
if (card.length() != 2) throw new JsonDataException("Unknown card: " + card);
|
||||
|
||||
char rank = card.charAt(0);
|
||||
switch (card.charAt(1)) {
|
||||
case 'C': return new Card(rank, Suit.CLUBS);
|
||||
case 'D': return new Card(rank, Suit.DIAMONDS);
|
||||
case 'H': return new Card(rank, Suit.HEARTS);
|
||||
case 'S': return new Card(rank, Suit.SPADES);
|
||||
default: throw new JsonDataException("unknown suit: " + card);
|
||||
case 'C':
|
||||
return new Card(rank, Suit.CLUBS);
|
||||
case 'D':
|
||||
return new Card(rank, Suit.DIAMONDS);
|
||||
case 'H':
|
||||
return new Card(rank, Suit.HEARTS);
|
||||
case 'S':
|
||||
return new Card(rank, Suit.SPADES);
|
||||
default:
|
||||
throw new JsonDataException("unknown suit: " + card);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -31,11 +31,9 @@ import javax.annotation.Nullable;
|
||||
|
||||
public final class CustomAdapterFactory {
|
||||
public void run() throws Exception {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(new SortedSetAdapterFactory())
|
||||
.build();
|
||||
JsonAdapter<SortedSet<String>> jsonAdapter = moshi.adapter(
|
||||
Types.newParameterizedType(SortedSet.class, String.class));
|
||||
Moshi moshi = new Moshi.Builder().add(new SortedSetAdapterFactory()).build();
|
||||
JsonAdapter<SortedSet<String>> jsonAdapter =
|
||||
moshi.adapter(Types.newParameterizedType(SortedSet.class, String.class));
|
||||
|
||||
TreeSet<String> model = new TreeSet<>();
|
||||
model.add("a");
|
||||
@@ -48,9 +46,9 @@ public final class CustomAdapterFactory {
|
||||
|
||||
/**
|
||||
* This class composes an adapter for any element type into an adapter for a sorted set of those
|
||||
* elements. For example, given a {@code JsonAdapter<MovieTicket>}, use this to get a
|
||||
* {@code JsonAdapter<SortedSet<MovieTicket>>}. It works by looping over the input elements when
|
||||
* both reading and writing.
|
||||
* elements. For example, given a {@code JsonAdapter<MovieTicket>}, use this to get a {@code
|
||||
* JsonAdapter<SortedSet<MovieTicket>>}. It works by looping over the input elements when both
|
||||
* reading and writing.
|
||||
*/
|
||||
static final class SortedSetAdapter<T> extends JsonAdapter<SortedSet<T>> {
|
||||
private final JsonAdapter<T> elementAdapter;
|
||||
@@ -59,7 +57,8 @@ public final class CustomAdapterFactory {
|
||||
this.elementAdapter = elementAdapter;
|
||||
}
|
||||
|
||||
@Override public SortedSet<T> fromJson(JsonReader reader) throws IOException {
|
||||
@Override
|
||||
public SortedSet<T> fromJson(JsonReader reader) throws IOException {
|
||||
TreeSet<T> result = new TreeSet<>();
|
||||
reader.beginArray();
|
||||
while (reader.hasNext()) {
|
||||
@@ -69,7 +68,8 @@ public final class CustomAdapterFactory {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override public void toJson(JsonWriter writer, SortedSet<T> set) throws IOException {
|
||||
@Override
|
||||
public void toJson(JsonWriter writer, SortedSet<T> set) throws IOException {
|
||||
writer.beginArray();
|
||||
for (T element : set) {
|
||||
elementAdapter.toJson(writer, element);
|
||||
@@ -85,7 +85,8 @@ public final class CustomAdapterFactory {
|
||||
* uses that to create an adapter for the set.
|
||||
*/
|
||||
static class SortedSetAdapterFactory implements JsonAdapter.Factory {
|
||||
@Override public @Nullable JsonAdapter<?> create(
|
||||
@Override
|
||||
public @Nullable JsonAdapter<?> create(
|
||||
Type type, Set<? extends Annotation> annotations, Moshi moshi) {
|
||||
if (!annotations.isEmpty()) {
|
||||
return null; // Annotations? This factory doesn't apply.
|
||||
|
@@ -15,15 +15,13 @@
|
||||
*/
|
||||
package com.squareup.moshi.recipes;
|
||||
|
||||
|
||||
import com.squareup.moshi.FromJson;
|
||||
import com.squareup.moshi.Json;
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.Moshi;
|
||||
import com.squareup.moshi.JsonReader;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import com.squareup.moshi.Moshi;
|
||||
import java.io.IOException;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public final class CustomAdapterWithDelegate {
|
||||
public void run() throws Exception {
|
||||
@@ -42,10 +40,14 @@ public final class CustomAdapterWithDelegate {
|
||||
}
|
||||
|
||||
private enum Stage {
|
||||
@Json(name = "not-started") NOT_STARTED,
|
||||
@Json(name = "in-progress") IN_PROGRESS,
|
||||
@Json(name = "rejected") REJECTED,
|
||||
@Json(name = "completed") COMPLETED
|
||||
@Json(name = "not-started")
|
||||
NOT_STARTED,
|
||||
@Json(name = "in-progress")
|
||||
IN_PROGRESS,
|
||||
@Json(name = "rejected")
|
||||
REJECTED,
|
||||
@Json(name = "completed")
|
||||
COMPLETED
|
||||
}
|
||||
|
||||
private static final class StageAdapter {
|
||||
|
@@ -21,11 +21,7 @@ import com.squareup.moshi.recipes.models.Player;
|
||||
|
||||
public final class CustomFieldName {
|
||||
public void run() throws Exception {
|
||||
String json = ""
|
||||
+ "{"
|
||||
+ " \"username\": \"jesse\","
|
||||
+ " \"lucky number\": 32"
|
||||
+ "}\n";
|
||||
String json = "" + "{" + " \"username\": \"jesse\"," + " \"lucky number\": 32" + "}\n";
|
||||
|
||||
Moshi moshi = new Moshi.Builder().build();
|
||||
JsonAdapter<Player> jsonAdapter = moshi.adapter(Player.class);
|
||||
|
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.squareup.moshi.recipes;
|
||||
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import com.squareup.moshi.FromJson;
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.JsonQualifier;
|
||||
@@ -22,20 +24,17 @@ import com.squareup.moshi.Moshi;
|
||||
import com.squareup.moshi.ToJson;
|
||||
import java.lang.annotation.Retention;
|
||||
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
public final class CustomQualifier {
|
||||
public void run() throws Exception {
|
||||
String json = ""
|
||||
+ "{\n"
|
||||
+ " \"color\": \"#ff0000\",\n"
|
||||
+ " \"height\": 768,\n"
|
||||
+ " \"width\": 1024\n"
|
||||
+ "}\n";
|
||||
String json =
|
||||
""
|
||||
+ "{\n"
|
||||
+ " \"color\": \"#ff0000\",\n"
|
||||
+ " \"height\": 768,\n"
|
||||
+ " \"width\": 1024\n"
|
||||
+ "}\n";
|
||||
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(new ColorAdapter())
|
||||
.build();
|
||||
Moshi moshi = new Moshi.Builder().add(new ColorAdapter()).build();
|
||||
JsonAdapter<Rectangle> jsonAdapter = moshi.adapter(Rectangle.class);
|
||||
|
||||
Rectangle rectangle = jsonAdapter.fromJson(json);
|
||||
@@ -51,22 +50,25 @@ public final class CustomQualifier {
|
||||
int height;
|
||||
@HexColor int color;
|
||||
|
||||
@Override public String toString() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%dx%d #%06x", width, height, color);
|
||||
}
|
||||
}
|
||||
|
||||
@Retention(RUNTIME)
|
||||
@JsonQualifier
|
||||
public @interface HexColor {
|
||||
}
|
||||
public @interface HexColor {}
|
||||
|
||||
static class ColorAdapter {
|
||||
@ToJson String toJson(@HexColor int rgb) {
|
||||
@ToJson
|
||||
String toJson(@HexColor int rgb) {
|
||||
return String.format("#%06x", rgb);
|
||||
}
|
||||
|
||||
@FromJson @HexColor int fromJson(String rgb) {
|
||||
@FromJson
|
||||
@HexColor
|
||||
int fromJson(String rgb) {
|
||||
return Integer.parseInt(rgb.substring(1), 16);
|
||||
}
|
||||
}
|
||||
|
@@ -21,18 +21,17 @@ import com.squareup.moshi.recipes.models.BlackjackHand;
|
||||
|
||||
public final class CustomTypeAdapter {
|
||||
public void run() throws Exception {
|
||||
String json = ""
|
||||
+ "{\n"
|
||||
+ " \"hidden_card\": \"6S\",\n"
|
||||
+ " \"visible_cards\": [\n"
|
||||
+ " \"4C\",\n"
|
||||
+ " \"AH\"\n"
|
||||
+ " ]\n"
|
||||
+ "}\n";
|
||||
String json =
|
||||
""
|
||||
+ "{\n"
|
||||
+ " \"hidden_card\": \"6S\",\n"
|
||||
+ " \"visible_cards\": [\n"
|
||||
+ " \"4C\",\n"
|
||||
+ " \"AH\"\n"
|
||||
+ " ]\n"
|
||||
+ "}\n";
|
||||
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(new CardAdapter())
|
||||
.build();
|
||||
Moshi moshi = new Moshi.Builder().add(new CardAdapter()).build();
|
||||
JsonAdapter<BlackjackHand> jsonAdapter = moshi.adapter(BlackjackHand.class);
|
||||
|
||||
BlackjackHand blackjackHand = jsonAdapter.fromJson(json);
|
||||
|
@@ -35,7 +35,8 @@ public final class DefaultOnDataMismatchAdapter<T> extends JsonAdapter<T> {
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
@Override public T fromJson(JsonReader reader) throws IOException {
|
||||
@Override
|
||||
public T fromJson(JsonReader reader) throws IOException {
|
||||
// Use a peeked reader to leave the reader in a known state even if there's an exception.
|
||||
JsonReader peeked = reader.peekJson();
|
||||
T result;
|
||||
@@ -52,13 +53,15 @@ public final class DefaultOnDataMismatchAdapter<T> extends JsonAdapter<T> {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override public void toJson(JsonWriter writer, T value) throws IOException {
|
||||
@Override
|
||||
public void toJson(JsonWriter writer, T value) throws IOException {
|
||||
delegate.toJson(writer, value);
|
||||
}
|
||||
|
||||
public static <T> Factory newFactory(final Class<T> type, final T defaultValue) {
|
||||
return new Factory() {
|
||||
@Override public @Nullable JsonAdapter<?> create(
|
||||
@Override
|
||||
public @Nullable JsonAdapter<?> create(
|
||||
Type requestedType, Set<? extends Annotation> annotations, Moshi moshi) {
|
||||
if (type != requestedType) return null;
|
||||
JsonAdapter<T> delegate = moshi.nextAdapter(this, type, annotations);
|
||||
|
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.squareup.moshi.recipes;
|
||||
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import com.squareup.moshi.Json;
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.JsonQualifier;
|
||||
@@ -29,38 +31,38 @@ import java.lang.reflect.Type;
|
||||
import java.util.Set;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
final class FallbackEnum {
|
||||
@Retention(RUNTIME)
|
||||
@JsonQualifier
|
||||
public @interface Fallback {
|
||||
/**
|
||||
* The enum name.
|
||||
*/
|
||||
/** The enum name. */
|
||||
String value();
|
||||
}
|
||||
|
||||
public static final class FallbackEnumJsonAdapter<T extends Enum<T>> extends JsonAdapter<T> {
|
||||
public static final Factory FACTORY = new Factory() {
|
||||
@Nullable @Override @SuppressWarnings("unchecked")
|
||||
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
|
||||
Class<?> rawType = Types.getRawType(type);
|
||||
if (!rawType.isEnum()) {
|
||||
return null;
|
||||
}
|
||||
if (annotations.size() != 1) {
|
||||
return null;
|
||||
}
|
||||
Annotation annotation = annotations.iterator().next();
|
||||
if (!(annotation instanceof Fallback)) {
|
||||
return null;
|
||||
}
|
||||
Class<Enum> enumType = (Class<Enum>) rawType;
|
||||
Enum<?> fallback = Enum.valueOf(enumType, ((Fallback) annotation).value());
|
||||
return new FallbackEnumJsonAdapter<>(enumType, fallback);
|
||||
}
|
||||
};
|
||||
public static final Factory FACTORY =
|
||||
new Factory() {
|
||||
@Nullable
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public JsonAdapter<?> create(
|
||||
Type type, Set<? extends Annotation> annotations, Moshi moshi) {
|
||||
Class<?> rawType = Types.getRawType(type);
|
||||
if (!rawType.isEnum()) {
|
||||
return null;
|
||||
}
|
||||
if (annotations.size() != 1) {
|
||||
return null;
|
||||
}
|
||||
Annotation annotation = annotations.iterator().next();
|
||||
if (!(annotation instanceof Fallback)) {
|
||||
return null;
|
||||
}
|
||||
Class<Enum> enumType = (Class<Enum>) rawType;
|
||||
Enum<?> fallback = Enum.valueOf(enumType, ((Fallback) annotation).value());
|
||||
return new FallbackEnumJsonAdapter<>(enumType, fallback);
|
||||
}
|
||||
};
|
||||
|
||||
final Class<T> enumType;
|
||||
final String[] nameStrings;
|
||||
@@ -86,46 +88,51 @@ final class FallbackEnum {
|
||||
}
|
||||
}
|
||||
|
||||
@Override public T fromJson(JsonReader reader) throws IOException {
|
||||
@Override
|
||||
public T fromJson(JsonReader reader) throws IOException {
|
||||
int index = reader.selectString(options);
|
||||
if (index != -1) return constants[index];
|
||||
reader.nextString();
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override public void toJson(JsonWriter writer, T value) throws IOException {
|
||||
@Override
|
||||
public void toJson(JsonWriter writer, T value) throws IOException {
|
||||
writer.value(nameStrings[value.ordinal()]);
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "JsonAdapter(" + enumType.getName() + ").defaultValue( " + defaultValue + ")";
|
||||
}
|
||||
}
|
||||
|
||||
static final class Example {
|
||||
enum Transportation {
|
||||
WALKING, BIKING, TRAINS, PLANES
|
||||
WALKING,
|
||||
BIKING,
|
||||
TRAINS,
|
||||
PLANES
|
||||
}
|
||||
|
||||
@Fallback("WALKING") final Transportation transportation;
|
||||
@Fallback("WALKING")
|
||||
final Transportation transportation;
|
||||
|
||||
Example(Transportation transportation) {
|
||||
this.transportation = transportation;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return transportation.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(FallbackEnumJsonAdapter.FACTORY)
|
||||
.build();
|
||||
Moshi moshi = new Moshi.Builder().add(FallbackEnumJsonAdapter.FACTORY).build();
|
||||
JsonAdapter<Example> adapter = moshi.adapter(Example.class);
|
||||
System.out.println(adapter.fromJson("{\"transportation\":\"CARS\"}"));
|
||||
}
|
||||
|
||||
private FallbackEnum() {
|
||||
}
|
||||
private FallbackEnum() {}
|
||||
}
|
||||
|
@@ -25,12 +25,13 @@ public final class FromJsonWithoutStrings {
|
||||
// For some reason our JSON has date and time as separate fields. We will clean that up during
|
||||
// parsing: Moshi will first parse the JSON directly to an EventJson and from that the
|
||||
// EventJsonAdapter will create the actual Event.
|
||||
String json = ""
|
||||
+ "{\n"
|
||||
+ " \"title\": \"Blackjack tournament\",\n"
|
||||
+ " \"begin_date\": \"20151010\",\n"
|
||||
+ " \"begin_time\": \"17:04\"\n"
|
||||
+ "}\n";
|
||||
String json =
|
||||
""
|
||||
+ "{\n"
|
||||
+ " \"title\": \"Blackjack tournament\",\n"
|
||||
+ " \"begin_date\": \"20151010\",\n"
|
||||
+ " \"begin_time\": \"17:04\"\n"
|
||||
+ "}\n";
|
||||
|
||||
Moshi moshi = new Moshi.Builder().add(new EventJsonAdapter()).build();
|
||||
JsonAdapter<Event> jsonAdapter = moshi.adapter(Event.class);
|
||||
@@ -55,23 +56,30 @@ public final class FromJsonWithoutStrings {
|
||||
String title;
|
||||
String beginDateAndTime;
|
||||
|
||||
@Override public String toString() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Event{"
|
||||
+ "title='" + title + '\''
|
||||
+ ", beginDateAndTime='" + beginDateAndTime + '\''
|
||||
+ "title='"
|
||||
+ title
|
||||
+ '\''
|
||||
+ ", beginDateAndTime='"
|
||||
+ beginDateAndTime
|
||||
+ '\''
|
||||
+ '}';
|
||||
}
|
||||
}
|
||||
|
||||
private static final class EventJsonAdapter {
|
||||
@FromJson Event eventFromJson(EventJson eventJson) {
|
||||
@FromJson
|
||||
Event eventFromJson(EventJson eventJson) {
|
||||
Event event = new Event();
|
||||
event.title = eventJson.title;
|
||||
event.beginDateAndTime = eventJson.begin_date + " " + eventJson.begin_time;
|
||||
return event;
|
||||
}
|
||||
|
||||
@ToJson EventJson eventToJson(Event event) {
|
||||
@ToJson
|
||||
EventJson eventToJson(Event event) {
|
||||
EventJson json = new EventJson();
|
||||
json.title = event.title;
|
||||
json.begin_date = event.beginDateAndTime.substring(0, 8);
|
||||
|
@@ -31,10 +31,11 @@ import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
public final class MultipleFormats {
|
||||
public void run() throws Exception {
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(new MultipleFormatsCardAdapter())
|
||||
.add(new CardStringAdapter())
|
||||
.build();
|
||||
Moshi moshi =
|
||||
new Moshi.Builder()
|
||||
.add(new MultipleFormatsCardAdapter())
|
||||
.add(new CardStringAdapter())
|
||||
.build();
|
||||
|
||||
JsonAdapter<Card> cardAdapter = moshi.adapter(Card.class);
|
||||
|
||||
@@ -48,13 +49,18 @@ public final class MultipleFormats {
|
||||
|
||||
/** Handles cards either as strings "5D" or as objects {"suit": "SPADES", "rank": 5}. */
|
||||
public final class MultipleFormatsCardAdapter {
|
||||
@ToJson void toJson(JsonWriter writer, Card value,
|
||||
@CardString JsonAdapter<Card> stringAdapter) throws IOException {
|
||||
@ToJson
|
||||
void toJson(JsonWriter writer, Card value, @CardString JsonAdapter<Card> stringAdapter)
|
||||
throws IOException {
|
||||
stringAdapter.toJson(writer, value);
|
||||
}
|
||||
|
||||
@FromJson Card fromJson(JsonReader reader, @CardString JsonAdapter<Card> stringAdapter,
|
||||
JsonAdapter<Card> defaultAdapter) throws IOException {
|
||||
@FromJson
|
||||
Card fromJson(
|
||||
JsonReader reader,
|
||||
@CardString JsonAdapter<Card> stringAdapter,
|
||||
JsonAdapter<Card> defaultAdapter)
|
||||
throws IOException {
|
||||
if (reader.peek() == JsonReader.Token.STRING) {
|
||||
return stringAdapter.fromJson(reader);
|
||||
} else {
|
||||
@@ -65,28 +71,35 @@ public final class MultipleFormats {
|
||||
|
||||
/** Handles cards as strings only. */
|
||||
public final class CardStringAdapter {
|
||||
@ToJson String toJson(@CardString Card card) {
|
||||
@ToJson
|
||||
String toJson(@CardString Card card) {
|
||||
return card.rank + card.suit.name().substring(0, 1);
|
||||
}
|
||||
|
||||
@FromJson @CardString Card fromJson(String card) {
|
||||
@FromJson
|
||||
@CardString
|
||||
Card fromJson(String card) {
|
||||
if (card.length() != 2) throw new JsonDataException("Unknown card: " + card);
|
||||
|
||||
char rank = card.charAt(0);
|
||||
switch (card.charAt(1)) {
|
||||
case 'C': return new Card(rank, Suit.CLUBS);
|
||||
case 'D': return new Card(rank, Suit.DIAMONDS);
|
||||
case 'H': return new Card(rank, Suit.HEARTS);
|
||||
case 'S': return new Card(rank, Suit.SPADES);
|
||||
default: throw new JsonDataException("unknown suit: " + card);
|
||||
case 'C':
|
||||
return new Card(rank, Suit.CLUBS);
|
||||
case 'D':
|
||||
return new Card(rank, Suit.DIAMONDS);
|
||||
case 'H':
|
||||
return new Card(rank, Suit.HEARTS);
|
||||
case 'S':
|
||||
return new Card(rank, Suit.SPADES);
|
||||
default:
|
||||
throw new JsonDataException("unknown suit: " + card);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@JsonQualifier
|
||||
@interface CardString {
|
||||
}
|
||||
@interface CardString {}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new MultipleFormats().run();
|
||||
|
@@ -27,23 +27,22 @@ 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();
|
||||
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\""
|
||||
+ "}";
|
||||
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));
|
||||
Tournament nextTournament =
|
||||
new Tournament("Waterloo Classic", "Bauer Kitchen", newDate(2015, 10, 1, 20, -5));
|
||||
System.out.println("Next tournament JSON: " + jsonAdapter.toJson(nextTournament));
|
||||
}
|
||||
|
||||
|
@@ -21,23 +21,24 @@ import com.squareup.moshi.recipes.models.BlackjackHand;
|
||||
|
||||
public final class ReadJson {
|
||||
public void run() throws Exception {
|
||||
String json = ""
|
||||
+ "{\n"
|
||||
+ " \"hidden_card\": {\n"
|
||||
+ " \"rank\": \"6\",\n"
|
||||
+ " \"suit\": \"SPADES\"\n"
|
||||
+ " },\n"
|
||||
+ " \"visible_cards\": [\n"
|
||||
+ " {\n"
|
||||
+ " \"rank\": \"4\",\n"
|
||||
+ " \"suit\": \"CLUBS\"\n"
|
||||
+ " },\n"
|
||||
+ " {\n"
|
||||
+ " \"rank\": \"A\",\n"
|
||||
+ " \"suit\": \"HEARTS\"\n"
|
||||
+ " }\n"
|
||||
+ " ]\n"
|
||||
+ "}\n";
|
||||
String json =
|
||||
""
|
||||
+ "{\n"
|
||||
+ " \"hidden_card\": {\n"
|
||||
+ " \"rank\": \"6\",\n"
|
||||
+ " \"suit\": \"SPADES\"\n"
|
||||
+ " },\n"
|
||||
+ " \"visible_cards\": [\n"
|
||||
+ " {\n"
|
||||
+ " \"rank\": \"4\",\n"
|
||||
+ " \"suit\": \"CLUBS\"\n"
|
||||
+ " },\n"
|
||||
+ " {\n"
|
||||
+ " \"rank\": \"A\",\n"
|
||||
+ " \"suit\": \"HEARTS\"\n"
|
||||
+ " }\n"
|
||||
+ " ]\n"
|
||||
+ "}\n";
|
||||
|
||||
Moshi moshi = new Moshi.Builder().build();
|
||||
JsonAdapter<BlackjackHand> jsonAdapter = moshi.adapter(BlackjackHand.class);
|
||||
|
@@ -24,21 +24,22 @@ import java.util.List;
|
||||
|
||||
public final class ReadJsonList {
|
||||
public void run() throws Exception {
|
||||
String json = ""
|
||||
+ "[\n"
|
||||
+ " {\n"
|
||||
+ " \"rank\": \"4\",\n"
|
||||
+ " \"suit\": \"CLUBS\"\n"
|
||||
+ " },\n"
|
||||
+ " {\n"
|
||||
+ " \"rank\": \"A\",\n"
|
||||
+ " \"suit\": \"HEARTS\"\n"
|
||||
+ " },\n"
|
||||
+ " {\n"
|
||||
+ " \"rank\": \"J\",\n"
|
||||
+ " \"suit\": \"SPADES\"\n"
|
||||
+ " }\n"
|
||||
+ "]";
|
||||
String json =
|
||||
""
|
||||
+ "[\n"
|
||||
+ " {\n"
|
||||
+ " \"rank\": \"4\",\n"
|
||||
+ " \"suit\": \"CLUBS\"\n"
|
||||
+ " },\n"
|
||||
+ " {\n"
|
||||
+ " \"rank\": \"A\",\n"
|
||||
+ " \"suit\": \"HEARTS\"\n"
|
||||
+ " },\n"
|
||||
+ " {\n"
|
||||
+ " \"rank\": \"J\",\n"
|
||||
+ " \"suit\": \"SPADES\"\n"
|
||||
+ " }\n"
|
||||
+ "]";
|
||||
|
||||
Moshi moshi = new Moshi.Builder().build();
|
||||
|
||||
|
@@ -25,11 +25,12 @@ public final class RecoverFromTypeMismatch {
|
||||
public void run() throws Exception {
|
||||
String json = "[\"DIAMONDS\", \"STARS\", \"HEARTS\"]";
|
||||
|
||||
Moshi moshi = new Moshi.Builder()
|
||||
.add(DefaultOnDataMismatchAdapter.newFactory(Suit.class, Suit.CLUBS))
|
||||
.build();
|
||||
JsonAdapter<List<Suit>> jsonAdapter = moshi.adapter(
|
||||
Types.newParameterizedType(List.class, Suit.class));
|
||||
Moshi moshi =
|
||||
new Moshi.Builder()
|
||||
.add(DefaultOnDataMismatchAdapter.newFactory(Suit.class, Suit.CLUBS))
|
||||
.build();
|
||||
JsonAdapter<List<Suit>> jsonAdapter =
|
||||
moshi.adapter(Types.newParameterizedType(List.class, Suit.class));
|
||||
|
||||
List<Suit> suits = jsonAdapter.fromJson(json);
|
||||
System.out.println(suits);
|
||||
|
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.squareup.moshi.recipes;
|
||||
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import com.squareup.moshi.JsonAdapter;
|
||||
import com.squareup.moshi.JsonQualifier;
|
||||
import com.squareup.moshi.JsonReader;
|
||||
@@ -30,20 +32,18 @@ import java.lang.reflect.Type;
|
||||
import java.util.Set;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
final class Unwrap {
|
||||
private Unwrap() {
|
||||
}
|
||||
private Unwrap() {}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String json = ""
|
||||
+ "{\"data\":"
|
||||
+ " {\n"
|
||||
+ " \"rank\": \"4\",\n"
|
||||
+ " \"suit\": \"CLUBS\"\n"
|
||||
+ " }"
|
||||
+ "}";
|
||||
String json =
|
||||
""
|
||||
+ "{\"data\":"
|
||||
+ " {\n"
|
||||
+ " \"rank\": \"4\",\n"
|
||||
+ " \"suit\": \"CLUBS\"\n"
|
||||
+ " }"
|
||||
+ "}";
|
||||
Moshi moshi = new Moshi.Builder().add(EnvelopeJsonAdapter.FACTORY).build();
|
||||
JsonAdapter<Card> adapter = moshi.adapter(Card.class, Enveloped.class);
|
||||
Card out = adapter.fromJson(json);
|
||||
@@ -51,23 +51,28 @@ final class Unwrap {
|
||||
}
|
||||
|
||||
public static final class EnvelopeJsonAdapter extends JsonAdapter<Object> {
|
||||
public static final JsonAdapter.Factory FACTORY = new Factory() {
|
||||
@Override public @Nullable JsonAdapter<?> create(
|
||||
Type type, Set<? extends Annotation> annotations, Moshi moshi) {
|
||||
Set<? extends Annotation> delegateAnnotations =
|
||||
Types.nextAnnotations(annotations, Enveloped.class);
|
||||
if (delegateAnnotations == null) {
|
||||
return null;
|
||||
}
|
||||
Type envelope =
|
||||
Types.newParameterizedTypeWithOwner(EnvelopeJsonAdapter.class, Envelope.class, type);
|
||||
JsonAdapter<Envelope<?>> delegate = moshi.nextAdapter(this, envelope, delegateAnnotations);
|
||||
return new EnvelopeJsonAdapter(delegate);
|
||||
}
|
||||
};
|
||||
public static final JsonAdapter.Factory FACTORY =
|
||||
new Factory() {
|
||||
@Override
|
||||
public @Nullable JsonAdapter<?> create(
|
||||
Type type, Set<? extends Annotation> annotations, Moshi moshi) {
|
||||
Set<? extends Annotation> delegateAnnotations =
|
||||
Types.nextAnnotations(annotations, Enveloped.class);
|
||||
if (delegateAnnotations == null) {
|
||||
return null;
|
||||
}
|
||||
Type envelope =
|
||||
Types.newParameterizedTypeWithOwner(
|
||||
EnvelopeJsonAdapter.class, Envelope.class, type);
|
||||
JsonAdapter<Envelope<?>> delegate =
|
||||
moshi.nextAdapter(this, envelope, delegateAnnotations);
|
||||
return new EnvelopeJsonAdapter(delegate);
|
||||
}
|
||||
};
|
||||
|
||||
@Retention(RUNTIME) @JsonQualifier public @interface Enveloped {
|
||||
}
|
||||
@Retention(RUNTIME)
|
||||
@JsonQualifier
|
||||
public @interface Enveloped {}
|
||||
|
||||
private static final class Envelope<T> {
|
||||
final T data;
|
||||
@@ -83,11 +88,13 @@ final class Unwrap {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override public Object fromJson(JsonReader reader) throws IOException {
|
||||
@Override
|
||||
public Object fromJson(JsonReader reader) throws IOException {
|
||||
return delegate.fromJson(reader).data;
|
||||
}
|
||||
|
||||
@Override public void toJson(JsonWriter writer, Object value) throws IOException {
|
||||
@Override
|
||||
public void toJson(JsonWriter writer, Object value) throws IOException {
|
||||
delegate.toJson(writer, new Envelope<>(value));
|
||||
}
|
||||
}
|
||||
|
@@ -15,21 +15,21 @@
|
||||
*/
|
||||
package com.squareup.moshi.recipes;
|
||||
|
||||
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;
|
||||
|
||||
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.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 {
|
||||
BlackjackHand blackjackHand = new BlackjackHand(
|
||||
new Card('6', SPADES),
|
||||
Arrays.asList(new Card('4', CLUBS), new Card('A', HEARTS)));
|
||||
BlackjackHand blackjackHand =
|
||||
new BlackjackHand(
|
||||
new Card('6', SPADES), Arrays.asList(new Card('4', CLUBS), new Card('A', HEARTS)));
|
||||
|
||||
Moshi moshi = new Moshi.Builder().build();
|
||||
JsonAdapter<BlackjackHand> jsonAdapter = moshi.adapter(BlackjackHand.class);
|
||||
|
@@ -27,7 +27,8 @@ public final class BlackjackHand {
|
||||
this.visible_cards = visibleCards;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "hidden=" + hidden_card + ",visible=" + visible_cards;
|
||||
}
|
||||
}
|
||||
|
@@ -24,7 +24,8 @@ public final class Card {
|
||||
this.suit = suit;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s%s", rank, suit);
|
||||
}
|
||||
}
|
||||
|
@@ -26,7 +26,8 @@ public final class Player {
|
||||
this.luckyNumber = luckyNumber;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return username + " gets lucky with " + luckyNumber;
|
||||
}
|
||||
}
|
||||
|
@@ -16,9 +16,13 @@
|
||||
package com.squareup.moshi.recipes.models;
|
||||
|
||||
public enum Suit {
|
||||
CLUBS, DIAMONDS, HEARTS, SPADES;
|
||||
CLUBS,
|
||||
DIAMONDS,
|
||||
HEARTS,
|
||||
SPADES;
|
||||
|
||||
@Override public String toString() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().substring(0, 1);
|
||||
}
|
||||
}
|
||||
|
@@ -28,7 +28,8 @@ public final class Tournament {
|
||||
this.start = start;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return name + " at " + location + " on " + start;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user