View Javadoc
1   package org.oxerr.okcoin.rest.dto.valuereader;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.nio.charset.Charset;
6   
7   import com.fasterxml.jackson.databind.JsonMappingException;
8   import com.fasterxml.jackson.databind.ObjectMapper;
9   
10  public class JsonValueReader<T> implements ValueReader<T> {
11  
12  	private final ObjectMapper objectMapper;
13  
14  	private final Class<T> valueType;
15  
16  	public JsonValueReader(ObjectMapper objectMapper, Class<T> valueType) {
17  		this.objectMapper = objectMapper;
18  		this.valueType = valueType;
19  	}
20  
21  	/**
22  	 * {@inheritDoc}
23  	 */
24  	@Override
25  	public T read(InputStream inputStream, String mimeType, Charset charset) throws IOException {
26  		try {
27  			return objectMapper.readValue(inputStream, valueType);
28  		} catch (JsonMappingException jme) {
29  			if (jme.getCause() instanceof RuntimeException) {
30  				throw (RuntimeException) jme.getCause();
31  			} else {
32  				throw jme;
33  			}
34  		}
35  	}
36  
37  }