com.nytimes.android.external.store.middleware.jackson.JacksonStringParser Maven / Gradle / Ivy
package com.nytimes.android.external.store.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.store.base.Parser;
import java.io.IOException;
import java.lang.reflect.Type;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
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
@Nullable
public Parsed call(@Nonnull String source) {
try {
return objectMapper.readValue(source, parsedType);
} catch (IOException e) {
return null;
}
}
}