se.fortnox.reactivewizard.client.JsonArrayDeserializer Maven / Gradle / Ivy
package se.fortnox.reactivewizard.client;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.async.ByteArrayFeeder;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.TokenBuffer;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import se.fortnox.reactivewizard.util.ReflectionUtil;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class JsonArrayDeserializer {
private final ObjectReader reader;
private final JsonParser parser;
private final ByteArrayFeeder inputFeeder;
private TokenBuffer tokenBuffer;
private int depth = -1;
public JsonArrayDeserializer(ObjectMapper objectMapper, Method method) {
Type type = ReflectionUtil.getTypeOfFluxOrMono(method);
JavaType javaType = TypeFactory.defaultInstance().constructType(type);
reader = objectMapper.readerFor(javaType);
try {
parser = objectMapper.getFactory().setCodec(objectMapper).createNonBlockingByteArrayParser();
inputFeeder = (ByteArrayFeeder) parser.getNonBlockingInputFeeder();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Performs deserialization.
* @param buf the data
* @return publisher
*/
public Publisher> process(byte[] buf) {
try {
inputFeeder.feedInput(buf, 0, buf.length);
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy