com.nytimes.android.external.store3.middleware.jackson.JacksonStringParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of middleware-jackson3 Show documentation
Show all versions of middleware-jackson3 Show documentation
Store3 is built with RxJava2
The newest version!
package com.nytimes.android.external.store3.middleware.jackson;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nytimes.android.external.store3.base.Parser;
import com.nytimes.android.external.store3.util.ParserException;
import java.io.IOException;
import java.lang.reflect.Type;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import io.reactivex.annotations.NonNull;
public class JacksonStringParser implements Parser {
private final ObjectMapper objectMapper;
private final JavaType parsedType;
public JacksonStringParser(@Nonnull JsonFactory jsonFactory, @Nonnull Type type) {
objectMapper = new ObjectMapper(jsonFactory);
parsedType = objectMapper.constructType(type);
}
@Inject
public JacksonStringParser(@Nonnull ObjectMapper objectMapper, @Nonnull Type type) {
this.objectMapper = objectMapper;
parsedType = objectMapper.constructType(type);
}
@Override
public Parsed apply(@NonNull String s) throws ParserException {
try {
return objectMapper.readValue(s, parsedType);
} catch (IOException e) {
throw new ParserException(e.getMessage(), e);
}
}
}