mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Document Json.ignore in the README (#1456)
This commit is contained in:
17
README.md
17
README.md
@@ -739,7 +739,7 @@ class ColorAdapter {
|
|||||||
Use `@JsonQualifier` when you need different JSON encodings for the same type. Most programs
|
Use `@JsonQualifier` when you need different JSON encodings for the same type. Most programs
|
||||||
shouldn’t need this `@JsonQualifier`, but it’s very handy for those that do.
|
shouldn’t need this `@JsonQualifier`, but it’s very handy for those that do.
|
||||||
|
|
||||||
### Omit fields with `transient`
|
### Omitting fields
|
||||||
|
|
||||||
Some models declare fields that shouldn’t be included in JSON. For example, suppose our blackjack
|
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:
|
hand has a `total` field with the sum of the cards:
|
||||||
@@ -769,14 +769,15 @@ class BlackjackHand(
|
|||||||
</details>
|
</details>
|
||||||
|
|
||||||
By default, all fields are emitted when encoding JSON, and all fields are accepted when decoding
|
By default, all fields are emitted when encoding JSON, and all fields are accepted when decoding
|
||||||
JSON. Prevent a field from being included by adding Java’s `transient` keyword or Kotlin's `@Transient` annotation:
|
JSON. Prevent a field from being included by annotating them with `@Json(ignore = true)`.
|
||||||
|
|
||||||
<details open>
|
<details open>
|
||||||
<summary>Java</summary>
|
<summary>Java</summary>
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public final class BlackjackHand {
|
public final class BlackjackHand {
|
||||||
private transient int total;
|
@Json(ignore = true)
|
||||||
|
private int total;
|
||||||
|
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
@@ -788,16 +789,20 @@ public final class BlackjackHand {
|
|||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
class BlackjackHand(...) {
|
class BlackjackHand(...) {
|
||||||
@Transient var total: Int
|
@Json(ignore = true)
|
||||||
|
var total: Int = 0
|
||||||
|
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
Transient fields are omitted when writing JSON. When reading JSON, the field is skipped even if the
|
These fields are omitted when writing JSON. When reading JSON, the field is skipped even if the
|
||||||
JSON contains a value for the field. Instead, it will get a default value.
|
JSON contains a value for the field. Instead, it will get a default value. In Kotlin, these fields
|
||||||
|
_must_ have a default value if they are in the primary constructor.
|
||||||
|
|
||||||
|
Note that you can also use Java’s `transient` keyword or Kotlin's `@Transient` annotation on these fields
|
||||||
|
for the same effect.
|
||||||
|
|
||||||
### Default Values & Constructors
|
### Default Values & Constructors
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user