Guest User

Untitled

a guest
Apr 25th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. override fun serialize(src: Result<*, *>?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement? {
  2. return when (src) {
  3.  
  4. // Ignore Loading or Err states
  5. is Result.Loading -> context?.serialize("")
  6. is Result.Err -> context?.serialize("")
  7.  
  8. // Thanks God that the original type is passed inside the
  9. // ParameterizedType field.
  10. // It lets us to use it for a real serialization
  11. is Result.Ok -> {
  12. val parameterizedType = typeOfSrc as ParameterizedType
  13. // Basically we're serializing the Result<T, E> as a simple
  14. // T, only when the Result is .Ok
  15. return context?.serialize(src.value, parameterizedType.actualTypeArguments[0])
  16. }
  17.  
  18. null -> null
  19. }
  20. }
Add Comment
Please, Sign In to add comment