diff --git a/README.md b/README.md index 98989d9..e2e70e4 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ classes: _Note: The Kotlin examples of this README assume use of either Kotlin code gen or `KotlinJsonAdapterFactory` for reflection. Plain Java-based reflection is unsupported on Kotlin classes._ -
+
Java ```java @@ -20,7 +20,7 @@ System.out.println(blackjackHand); ```
-
+
Kotlin ```kotlin @@ -36,7 +36,7 @@ println(blackjackHand) And it can just as easily serialize Java or Kotlin objects as JSON: -
+
Java ```java @@ -52,7 +52,7 @@ System.out.println(json); ```
-
+
Kotlin ```kotlin @@ -81,7 +81,7 @@ Moshi has built-in support for reading and writing Java’s core data types: It supports your model classes by writing them out field-by-field. In the example above Moshi uses these classes: -
+
Java ```java @@ -103,7 +103,7 @@ enum Suit { ```
-
+
Kotlin ```kotlin @@ -159,7 +159,7 @@ suit in separate fields: `{"rank":"A","suit":"HEARTS"}`. With a type adapter, we encoding to something more compact: `"4H"` for the four of hearts or `"JD"` for the jack of diamonds: -
+
Java ```java @@ -184,7 +184,7 @@ class CardAdapter { ```
-
+
Kotlin ```kotlin @@ -211,7 +211,7 @@ class CardAdapter { Register the type adapter with the `Moshi.Builder` and we’re good to go. -
+
Java ```java @@ -221,7 +221,7 @@ Moshi moshi = new Moshi.Builder() ```
-
+
Kotlin ```kotlin @@ -265,7 +265,7 @@ We would like to combine these two fields into one string to facilitate the date later point. Also, we would like to have all variable names in CamelCase. Therefore, the `Event` class we want Moshi to produce like this: -
+
Java ```java @@ -276,7 +276,7 @@ class Event { ```
-
+
Kotlin ```kotlin @@ -291,7 +291,7 @@ Instead of manually parsing the JSON line per line (which we could also do) we c transformation automatically. We simply define another class `EventJson` that directly corresponds to the JSON structure: -
+
Java ```java @@ -303,7 +303,7 @@ class EventJson { ```
-
+
Kotlin ```kotlin @@ -321,7 +321,7 @@ to an `Event` it will first parse it to an `EventJson` as an intermediate step. serialize an `Event` Moshi will first create an `EventJson` object and then serialize that object as usual. -
+
Java ```java @@ -344,7 +344,7 @@ class EventJsonAdapter { ```
-
+
Kotlin ```kotlin @@ -371,7 +371,7 @@ class EventJsonAdapter { Again we register the adapter with Moshi. -
+
Java ```java @@ -381,7 +381,7 @@ Moshi moshi = new Moshi.Builder() ```
-
+
Kotlin ```kotlin @@ -393,7 +393,7 @@ val moshi = Moshi.Builder() We can now use Moshi to parse the JSON directly to an `Event`. -
+
Java ```java @@ -402,7 +402,7 @@ Event event = jsonAdapter.fromJson(json); ```
-
+
Kotlin ```kotlin @@ -424,7 +424,7 @@ Moshi provides a number of convenience methods for `JsonAdapter` objects: These factory methods wrap an existing `JsonAdapter` into additional functionality. For example, if you have an adapter that doesn't support nullable values, you can use `nullSafe()` to make it null safe: -
+
Java ```java @@ -445,7 +445,7 @@ System.out.println(nullDate); // null ```
-
+
Kotlin ```kotlin @@ -487,7 +487,7 @@ Say we have a JSON string of this structure: We can now use Moshi to parse the JSON string into a `List`. -
+
Java ```java @@ -498,7 +498,7 @@ List cards = adapter.fromJson(cardsJsonResponse); ```
-
+
Kotlin ```kotlin @@ -571,7 +571,7 @@ JSON has a field name containing a space: With `@Json` its corresponding Java or Kotlin class is easy: -
+
Java ```java @@ -584,7 +584,7 @@ class Player { ```
-
+
Kotlin ```kotlin @@ -618,7 +618,7 @@ Here’s a JSON message with two integers and a color: By convention, Android programs also use `int` for colors: -
+
Java ```java @@ -630,7 +630,7 @@ class Rectangle { ```
-
+
Kotlin ```kotlin @@ -654,7 +654,7 @@ But if we encoded the above Java or Kotlin class as JSON, the color isn't encode The fix is to define a qualifier annotation, itself annotated `@JsonQualifier`: -
+
Java ```java @@ -665,7 +665,7 @@ public @interface HexColor { ```
-
+
Kotlin ```kotlin @@ -678,7 +678,7 @@ annotation class HexColor Next apply this `@HexColor` annotation to the appropriate field: -
+
Java ```java @@ -690,7 +690,7 @@ class Rectangle { ```
-
+
Kotlin ```kotlin @@ -704,7 +704,7 @@ class Rectangle( And finally define a type adapter to handle it: -
+
Java ```java @@ -721,7 +721,7 @@ class ColorAdapter { ```
-
+
Kotlin ```kotlin @@ -746,7 +746,7 @@ shouldn’t need this `@JsonQualifier`, but it’s very handy for those that do. Some models declare fields that shouldn’t be included in JSON. For example, suppose our blackjack hand has a `total` field with the sum of the cards: -
+
Java ```java @@ -758,7 +758,7 @@ public final class BlackjackHand { ```
-
+
Kotlin ```kotlin @@ -773,7 +773,7 @@ class BlackjackHand( By default, all fields are emitted when encoding JSON, and all fields are accepted when decoding JSON. Prevent a field from being included by annotating them with `@Json(ignore = true)`. -
+
Java ```java @@ -786,7 +786,7 @@ public final class BlackjackHand { ```
-
+
Kotlin ```kotlin @@ -875,7 +875,7 @@ adapters to build upon the standard conversion. In this example, we turn serialize nulls, then delegate to the built-in adapter: -
+
Java ```java @@ -894,7 +894,7 @@ class TournamentWithNullsAdapter { ```
-
+
Kotlin ```kotlin @@ -922,7 +922,7 @@ the encoding and decoding process for any type, even without knowing about the t this example, we customize types annotated `@AlwaysSerializeNulls`, which an annotation we create, not built-in to Moshi: -
+
Java ```java @@ -932,7 +932,7 @@ public @interface AlwaysSerializeNulls {} ```
-
+
Kotlin ```kotlin @@ -942,7 +942,7 @@ annotation class AlwaysSerializeNulls ```
-
+
Java ```java @@ -955,7 +955,7 @@ static class Car { ```
-
+
Kotlin ```kotlin @@ -972,7 +972,7 @@ Each `JsonAdapter.Factory` interface is invoked by `Moshi` when it needs to buil user's type. The factory either returns an adapter to use, or null if it doesn't apply to the requested type. In our case we match all classes that have our annotation. -
+
Java ```java @@ -991,7 +991,7 @@ static class AlwaysSerializeNullsFactory implements JsonAdapter.Factory { ```
-
+
Kotlin ```kotlin