jaskSon二次封装,使用更方便

原帖地址找不到了,抱歉,如有原作者要求我即可删除。。。

public class JacksonUtil {

    static ObjectMapper mapper;

    /**
     * ObjectMapper 配置及初始化
     */
    public static ObjectMapper Instance() {

        if (mapper == null) {
            mapper = new ObjectMapper();
            mapper.getSerializationConfig().withDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
            mapper.getDeserializationConfig().withDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
            mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
            mapper.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true) ;
            mapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()
            {

                @Override
                public void serialize(
                        Object value,
                        JsonGenerator jg,
                        SerializerProvider sp) throws IOException, JsonProcessingException
                {
                    jg.writeString("");
                }
            });
        }
        return mapper;
    }


    /**
     * 将原始Json转为T类型实体
     */
    public static <T> T ToEntity(String content, Class<T> classT) {
        T result = null;
        try {
            result = Instance().readValue(content, classT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 将原始Json转为T类型实体集合
     */
    public static <T> List<T> ToEntityS(String content, Class<T> classT) {
        List<T> result = null;
        try {
            JavaType javaType = Instance().getTypeFactory().constructParametricType(List.class, classT);
            result = Instance().readValue(content, javaType);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }


    /**
     * 遍历到指定节点并将节点中内容转为T类型实体
     */
    public static <T> T ToEntityByNode(String content, Class<T> classT, String... path) {
        T result = null;
        try {
            result = Instance().readValue(GetNodeString(content, path), classT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 遍历到指定节点并将节点中内容转为T类型实体集合
     */
    public static <T> List<T> ToEntitySByNode(String content, Class<T> classT, String... path) {
        List<T> result = null;
        try {
            JavaType javaType = Instance().getTypeFactory().constructParametricType(List.class, classT);
            result = Instance().readValue(GetNodeString(content, path), javaType);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 获取指定节点的原始值
     * key,value(String)
     */
    public static String GetNodeStringValue(String content, String... path) {
        String result = null;
        try {
            JsonNode jsonNode = Instance().readTree(content);

            for (String p : path) {
                jsonNode = jsonNode.path(p);
            }
            result = jsonNode.getTextValue();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 获取指定节点boolean值
     * key,value(String)
     */
    public static boolean GetNodeBooleanValue(String content, String... path) {
        boolean result = false;
        try {
            JsonNode jsonNode = Instance().readTree(content);

            for (String p : path) {
                jsonNode = jsonNode.path(p);
            }
            result = jsonNode.getBooleanValue();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 获取指定节点的原始值
     * key,value(Int)
     */
    public static int GetNodeIntValue(String content, String... path) {
        int result = 0;
        try {
            JsonNode jsonNode = Instance().readTree(content);
            for (String p : path) {
                jsonNode = jsonNode.path(p);
            }
            result = jsonNode.getIntValue();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 以字符串形式获取指定节点中的内容(*与数值不同,节点中内容可能为 Json Array 等,并非原始值)
     */
    public static String GetNodeString(String content, String... path) {
        String result = null;
        try {
            JsonNode jsonNode = Instance().readTree(content);
            for (String p : path) {
                jsonNode = jsonNode.path(p);
            }
            result = jsonNode + "";
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }


    /**
     * Object 转为  Json
     */
    public static String ToJSon(Object obj) {
        String result = null;
        try {
            result = Instance().writeValueAsString(obj);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值