Created
December 6, 2019 09:59
-
-
Save Benjiko99/334d725943b8d7dbd9061a231d0d4f42 to your computer and use it in GitHub Desktop.
Moshi Adapter. Fields annotated with @JsonNullable will be serialized as `null` instead of being ignored.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Retention(AnnotationRetention.RUNTIME) | |
@JsonQualifier | |
annotation class JsonNullable | |
/** | |
* Fields annotated with @JsonNullable will be serialized as `null` instead of being ignored. | |
*/ | |
class JsonNullableAdapter : JsonAdapter.Factory { | |
override fun create(type: Type, annotations: MutableSet<out Annotation>, moshi: Moshi): JsonAdapter<Any>? { | |
val nextAnnotations = nextAnnotations(annotations, JsonNullable::class.java) ?: return null | |
return moshi.nextAdapter<Any>(this, type, nextAnnotations).serializeNulls() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setup: