Java使用ObjectMapper做数据转换的常用工具类实现

1.介绍

ObjectMapper类是Jackson库的主要类,它提供一些功能将数据集或对象转换的实现

2.依赖包


<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-databindartifactId>
    <version>2.9.5version>
dependency>


<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-coreartifactId>
    <version>2.9.5version>
dependency>


<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-annotationsartifactId>
    <version>2.9.5version>
dependency>

3.工具类实现

package org.tempuri;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonUtils {
    
    private static ObjectMapper mapper = new ObjectMapper();
    
    static {
        //序列化的时候序列对象的所有属性
        mapper.setSerializationInclusion(Include.ALWAYS);

        //反序列化的时候如果多了其他属性,不抛出异常
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        //如果是空对象的时候,不抛异常
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

        //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

    /**
      * 对象转为Json字符串
     * @param data
     * @return
     */
    public static String transferToJson(Object data) {
        String jsonStr = null;
        try {
            jsonStr = mapper.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return jsonStr;
    }
    
    /**
      * 对象转为byte数组
     * @param data
     * @return
     */
    public static byte[] transferToBytes(Object data) {
        byte[] byteArr = null;
        try {
            byteArr = mapper.writeValueAsBytes(data);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return byteArr;
    }
    
    /**
     * json字符串转为对象
     * @param str
     * @param valueType
     * @return
     */
    public static  T jsonToTransfer(String str, Class valueType) {
        T data = null;
        try {
            data = mapper.readValue(str, valueType);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }
    
    /**
     * byte数组转为对象
     * @param byteArr
     * @param valueType
     * @return
     */
    public static  T bytesToTransfer(byte[] byteArr, Class valueType) {
        T data = null;
        try {
            data = mapper.readValue(byteArr, valueType);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }
    
    public static void main(String[] args) {
        List> list = new ArrayList<>();
        Map map1 = new HashMap<>();
        map1.put("id", "1");
        map1.put("name", "我是一");
        list.add(map1);
        Map map3 = new HashMap<>();
        map3.put("id", "3");
        map3.put("name", "我是三");
        list.add(map3);
        Map map2 = new HashMap<>();
        map2.put("id", "2");
        map2.put("name", "我是二");
        list.add(map2);
        System.out.println(list);
        System.out.println(transferToJson(list));
    }
    
}

你可能感兴趣的:(Java使用ObjectMapper做数据转换的常用工具类实现)