首页 > 其他分享 >JacksonUtil工具类

JacksonUtil工具类

时间:2023-02-07 14:33:50浏览次数:66  
标签:node mapper return leaf 工具 null JacksonUtil ObjectMapper

package org.linlinjava.litemall.core.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class JacksonUtil {

    private static final Log logger = LogFactory.getLog(JacksonUtil.class);

    public static String parseString(String body, String field) {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node;
        try {
            node = mapper.readTree(body);
            JsonNode leaf = node.get(field);
            if (leaf != null) {
                return leaf.asText();
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }


    public static List<String> parseStringList(String body, String field) {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node;
        try {
            node = mapper.readTree(body);
            JsonNode leaf = node.get(field);

            if (leaf != null)
                return mapper.convertValue(leaf, new TypeReference<List<String>>() {
                });
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static Integer parseInteger(String body, String field) {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node;
        try {
            node = mapper.readTree(body);
            JsonNode leaf = node.get(field);
            if (leaf != null)
                return leaf.asInt();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static List<Integer> parseIntegerList(String body, String field) {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node;
        try {
            node = mapper.readTree(body);
            JsonNode leaf = node.get(field);

            if (leaf != null)
                return mapper.convertValue(leaf, new TypeReference<List<Integer>>() {
                });
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }


    public static Boolean parseBoolean(String body, String field) {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node;
        try {
            node = mapper.readTree(body);
            JsonNode leaf = node.get(field);
            if (leaf != null)
                return leaf.asBoolean();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static Short parseShort(String body, String field) {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node;
        try {
            node = mapper.readTree(body);
            JsonNode leaf = node.get(field);
            if (leaf != null) {
                Integer value = leaf.asInt();
                return value.shortValue();
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static Byte parseByte(String body, String field) {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node;
        try {
            node = mapper.readTree(body);
            JsonNode leaf = node.get(field);
            if (leaf != null) {
                Integer value = leaf.asInt();
                return value.byteValue();
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static <T> T parseObject(String body, String field, Class<T> clazz) {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node;
        try {
            node = mapper.readTree(body);
            node = node.get(field);
            return mapper.treeToValue(node, clazz);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static Object toNode(String json) {
        if (json == null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        try {

            return mapper.readTree(json);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }

        return null;
    }

    public static Map<String, String> toMap(String data) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.readValue(data, new TypeReference<Map<String, String>>() {
            });
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static String toJson(Object data) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
package org.linlinjava.litemall.core.util;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import java.io.IOException;import java.util.List;import java.util.Map;public class JacksonUtil {    private static final Log logger = LogFactory.getLog(JacksonUtil.class);    public static String parseString(String body, String field) {        ObjectMapper mapper = new ObjectMapper();        JsonNode node;        try {            node = mapper.readTree(body);            JsonNode leaf = node.get(field);            if (leaf != null) {                return leaf.asText();            }        } catch (IOException e) {            logger.error(e.getMessage(), e);        }        return null;    }    public static List<String> parseStringList(String body, String field) {        ObjectMapper mapper = new ObjectMapper();        JsonNode node;        try {            node = mapper.readTree(body);            JsonNode leaf = node.get(field);            if (leaf != null)                return mapper.convertValue(leaf, new TypeReference<List<String>>() {                });        } catch (IOException e) {            logger.error(e.getMessage(), e);        }        return null;    }    public static Integer parseInteger(String body, String field) {        ObjectMapper mapper = new ObjectMapper();        JsonNode node;        try {            node = mapper.readTree(body);            JsonNode leaf = node.get(field);            if (leaf != null)                return leaf.asInt();        } catch (IOException e) {            logger.error(e.getMessage(), e);        }        return null;    }    public static List<Integer> parseIntegerList(String body, String field) {        ObjectMapper mapper = new ObjectMapper();        JsonNode node;        try {            node = mapper.readTree(body);            JsonNode leaf = node.get(field);            if (leaf != null)                return mapper.convertValue(leaf, new TypeReference<List<Integer>>() {                });        } catch (IOException e) {            logger.error(e.getMessage(), e);        }        return null;    }    public static Boolean parseBoolean(String body, String field) {        ObjectMapper mapper = new ObjectMapper();        JsonNode node;        try {            node = mapper.readTree(body);            JsonNode leaf = node.get(field);            if (leaf != null)                return leaf.asBoolean();        } catch (IOException e) {            logger.error(e.getMessage(), e);        }        return null;    }    public static Short parseShort(String body, String field) {        ObjectMapper mapper = new ObjectMapper();        JsonNode node;        try {            node = mapper.readTree(body);            JsonNode leaf = node.get(field);            if (leaf != null) {                Integer value = leaf.asInt();                return value.shortValue();            }        } catch (IOException e) {            logger.error(e.getMessage(), e);        }        return null;    }    public static Byte parseByte(String body, String field) {        ObjectMapper mapper = new ObjectMapper();        JsonNode node;        try {            node = mapper.readTree(body);            JsonNode leaf = node.get(field);            if (leaf != null) {                Integer value = leaf.asInt();                return value.byteValue();            }        } catch (IOException e) {            logger.error(e.getMessage(), e);        }        return null;    }    public static <T> T parseObject(String body, String field, Class<T> clazz) {        ObjectMapper mapper = new ObjectMapper();        JsonNode node;        try {            node = mapper.readTree(body);            node = node.get(field);            return mapper.treeToValue(node, clazz);        } catch (IOException e) {            logger.error(e.getMessage(), e);        }        return null;    }    public static Object toNode(String json) {        if (json == null) {            return null;        }        ObjectMapper mapper = new ObjectMapper();        try {            return mapper.readTree(json);        } catch (IOException e) {            logger.error(e.getMessage(), e);        }        return null;    }    public static Map<String, String> toMap(String data) {        ObjectMapper objectMapper = new ObjectMapper();        try {            return objectMapper.readValue(data, new TypeReference<Map<String, String>>() {            });        } catch (IOException e) {            logger.error(e.getMessage(), e);        }        return null;    }    public static String toJson(Object data) {        ObjectMapper objectMapper = new ObjectMapper();        try {            return objectMapper.writeValueAsString(data);        } catch (JsonProcessingException e) {            e.printStackTrace();        }        return null;    }}

翻译

搜索

复制

标签:node,mapper,return,leaf,工具,null,JacksonUtil,ObjectMapper
From: https://www.cnblogs.com/chunwu822/p/17098298.html

相关文章

  • 【Unity 框架】 QFramework v1.0 使用指南 工具篇: 16. LiveCodingKit 写代码不用停止
    我们在用Unity开发的时候,每次编写或修改一点代码就需要进行停止运行->编写代码->等待编译->运行游戏。而在很多情况下这个过程是一个比较耗神的过程,因为开发者需要等待......
  • 灵活又简便,效率提升快,来了解下自定义表单工具!
    选择低代码开发平台,需要看准服务商、产品、服务保障等条件。只有认准专业的开发平台服务商,才能拥有一整套完善的低代码平台解决方案,才能帮助企业最大限度提升办公协作效率,......
  • JNA 加载动态链接库工具
    原理:简而言之就是把jar包中的动态库解压写到系统临时文件目录中去,然后在动态库的目录结构的要求进行加载获取系统临时文件目录:System.out.println(System.getProperty("ja......
  • EasyExcel工具使用
    pom文件:<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.2.7</version></dependency> packagecom.unicom.c......
  • 主打综合实力的静态代码试工具Klocwork 2022.4 版更新解析
    Klocwork2022.4中的新增功能 对于2022年的最终版本,Klocwork2022.4提供了针对C、C++、C#、Java的更新和改进,并增强了对Android13的支持。此外,此版本还包括对......
  • 4款常用的Linux手机远程工具!
    作为一名专业的Linux运维工程师,当我们进行服务器维护时,通常都是采用远程连接的方式进行操作及控制;而在多数情况下,很多人都是通过电脑进行远程操作的,但某种情况下,可能身......
  • Android sqlite3工具的使用
    sqlite3<数据库名称>进入数据库操作模式eg:sqlite3contacts.db使用这条命名前,先进入到该数据库的位置(需要用adbshell),执行 sqlite3contacts.db.tables查看所有的......
  • SpringBoot数据分页工具类
    SpringBoot数据分页工具类/***数据分页工具*/publicclassPageUtil{publicstaticPagepageHelp(@NotNullIntegerpageNum,@NotNullIntegerpageSize,@No......
  • NLP基础-准确分词(使用工具分词)
    关于NLP相关包安装配置,可以参考:​NLP工具包安装配置​​关于分词的原理可以参考:自然语言处理NLP-隐马尔科夫)1.加载字典来保证词可以分准对一些专业的名词来说,使用原有的词......
  • ansible自动化运维工具
    一、Ansible1、Ansible是一个基于Python开发的配置管理和应用部署工具,现在也在自动化管理领域大放异彩。它融合了众多老牌运维工具的优点,Pubbet和Saltstack能实现的功能,An......