All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.forkjoin.apikit.JacksonJsonConvert Maven / Gradle / Ivy

There is a newer version: 2.1.3
Show newest version
package org.forkjoin.apikit;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * @author [email protected] on 2017/5/17.
 */
public class JacksonJsonConvert implements JsonConvert {
    private ObjectMapper mapper = new ObjectMapper();

    public JacksonJsonConvert(ObjectMapper mapper) {
        this.mapper = mapper;
    }

    public ObjectMapper getMapper() {
        return mapper;
    }

    public void setMapper(ObjectMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public  T deserialize(String json, final Type type) {
        try {
            return mapper.readValue(json, new TypeReference(){
                public Type getType() {
                    return type;
                }
            });
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}