Last active
May 10, 2018 11:47
-
-
Save ibaca/71be7c73d8619d11182807b871c5975c to your computer and use it in GitHub Desktop.
Simple RxJava (Observable and Single) jackson serialiers.
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
package com.intendia.igestion.server.resource; | |
import com.fasterxml.jackson.core.JsonGenerator; | |
import com.fasterxml.jackson.databind.BeanDescription; | |
import com.fasterxml.jackson.databind.JavaType; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.JsonSerializer; | |
import com.fasterxml.jackson.databind.SerializationConfig; | |
import com.fasterxml.jackson.databind.SerializerProvider; | |
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper; | |
import com.fasterxml.jackson.databind.ser.Serializers; | |
import com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer; | |
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | |
import com.fasterxml.jackson.databind.type.TypeFactory; | |
import com.fasterxml.jackson.databind.util.Converter; | |
import com.fasterxml.jackson.databind.util.StdConverter; | |
import io.reactivex.Completable; | |
import io.reactivex.Maybe; | |
import io.reactivex.Observable; | |
import io.reactivex.Single; | |
import java.lang.reflect.Type; | |
import javax.annotation.Nullable; | |
public class RxJavaJacksonSerializers extends Serializers.Base { | |
@Override public JsonSerializer<?> findSerializer(SerializationConfig sc, JavaType jt, BeanDescription bd) { | |
Class<?> raw = jt.getRawClass(); | |
if (Observable.class.isAssignableFrom(raw)) { | |
JavaType vt = jt.containedType(0); | |
if (vt == null) vt = TypeFactory.unknownType(); | |
JavaType delegate = sc.getTypeFactory().constructParametrizedType(Iterable.class, Iterable.class, vt); | |
return new StdDelegatingSerializer(ObservableConverter.instance, delegate, null); | |
} | |
if (Maybe.class.isAssignableFrom(raw)) { | |
JavaType delegate = jt.containedType(0); | |
if (delegate == null) delegate = TypeFactory.unknownType(); | |
return new StdDelegatingSerializer(MaybeConverter.instance, delegate, null); | |
} | |
if (Single.class.isAssignableFrom(raw)) { | |
JavaType delegate = jt.containedType(0); | |
if (delegate == null) delegate = TypeFactory.unknownType(); | |
return new StdDelegatingSerializer(SingleConverter.instance, delegate, null); | |
} | |
if (Completable.class.isAssignableFrom(raw)) { | |
return CompletableSerializer.instance; | |
} | |
return super.findSerializer(sc, jt, bd); | |
} | |
static class ObservableConverter extends StdConverter<Object, Iterable<?>> { | |
static final Converter<Object, Iterable<?>> instance = new ObservableConverter(); | |
@Override public Iterable<?> convert(Object v) { | |
return ((Observable<?>) v).blockingIterable(); | |
} | |
} | |
static class MaybeConverter extends StdConverter<Object, Object> { | |
static final Converter<Object, Object> instance = new MaybeConverter(); | |
@Override public @Nullable Object convert(Object v) { | |
return ((Maybe<?>) v).blockingGet(); | |
} | |
} | |
static class SingleConverter extends StdConverter<Object, Object> { | |
static final Converter<Object, Object> instance = new SingleConverter(); | |
@Override public Object convert(Object v) { | |
return ((Single<?>) v).blockingGet(); | |
} | |
} | |
static class CompletableSerializer extends StdSerializer<Completable> { | |
static final JsonSerializer<Completable> instance = new CompletableSerializer(); | |
public CompletableSerializer() { | |
super(Completable.class); | |
} | |
@Override public void serialize(Completable v, JsonGenerator jg, SerializerProvider sp) { } | |
@Override public JsonNode getSchema(SerializerProvider sp, Type th) { return null; } | |
@Override public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment