jacksonツールクラスjson回転リスト、json回転map、json回転bean、bean回転json

6259 ワード

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Json   ,  Jackson  
 * 
 * @author Val Song
 * @date Sep 4, 2017
 *
 */
public class JacksonUtils {

    /**
     *       json
     * 
     * @param t
     * @return
     * @throws JsonProcessingException
     */
    public static  String toJson(T t) throws JsonProcessingException {
        return OBJECT_MAPPER.get().writeValueAsString(t);
    }

    /**
     *  json   bean
     * 
     * @param json
     * @param valueType
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static  T toEntity(String json, Class valueType)
            throws JsonParseException, JsonMappingException, IOException {
        return OBJECT_MAPPER.get().readValue(json, valueType);
    }

    /**
     *  json   List
     * 
     * @param json
     * @param collectionClass
     * @param elementClass
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static  List toList(String json, Class extends List> collectionClass, Class elementClass)
            throws JsonParseException, JsonMappingException, IOException {
        JavaType javaType = OBJECT_MAPPER.get().getTypeFactory().constructCollectionType(collectionClass, elementClass);
        return OBJECT_MAPPER.get().readValue(json, javaType);
    }

    /**
     *  json   Map
     * 
     * @param json
     * @param mapClass
     * @param keyClass
     * @param valueClass
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static  Map toMap(String json, Class extends Map> mapClass, Class keyClass,
            Class valueClass) throws JsonParseException, JsonMappingException, IOException {
        JavaType javaType = OBJECT_MAPPER.get().getTypeFactory().constructMapType(mapClass, keyClass, valueClass);
        return OBJECT_MAPPER.get().readValue(json, javaType);
    }

    // ################################################################################################################

    /**
     *         
     * 
     * @throws IllegalAccessException
     */
    private JacksonUtils() throws IllegalAccessException {
        throw new IllegalAccessException("Can't create an instance!");
    }

    /**
     *   ThreadLocal    ,          
     */
    private static final ThreadLocal OBJECT_MAPPER = new ThreadLocal() {
        @Override
        protected ObjectMapper initialValue() {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //         
            return objectMapper;
        }
    };

}