Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- override fun serialize(src: Result<*, *>?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement? {
- return when (src) {
- // Ignore Loading or Err states
- is Result.Loading -> context?.serialize("")
- is Result.Err -> context?.serialize("")
- // Thanks God that the original type is passed inside the
- // ParameterizedType field.
- // It lets us to use it for a real serialization
- is Result.Ok -> {
- val parameterizedType = typeOfSrc as ParameterizedType
- // Basically we're serializing the Result<T, E> as a simple
- // T, only when the Result is .Ok
- return context?.serialize(src.value, parameterizedType.actualTypeArguments[0])
- }
- null -> null
- }
- }
Add Comment
Please, Sign In to add comment