java--json工具

对象转化为string类型时,时间类型保持为字符串类型,而不是转化为时间戳(数字)

JsonUtils .java

package com.hfwy.energyelectrolysisauxiliary.utils;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

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

/**
 * JSON 工具类
 *
 * @author 芋道源码
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class JsonUtils {

	private static final ObjectMapper OBJECT_MAPPER = SpringUtils.getBean(ObjectMapper.class);

	public static ObjectMapper getObjectMapper() {
		return OBJECT_MAPPER;
	}

	public static String toJsonString(Object object) {
		if (ObjectUtil.isNull(object)) {
			return null;
		}
		try {
			return OBJECT_MAPPER.writeValueAsString(object);
		}
		catch (JsonProcessingException e) {
			throw new RuntimeException(e);
		}
	}

	public static <T> T parseObject(String text, Class<T> clazz) {
		if (StringUtils.isEmpty(text)) {
			return null;
		}
		try {
			return OBJECT_MAPPER.readValue(text, clazz);
		}
		catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static <T> T parseObject(byte[] bytes, Class<T> clazz) {
		if (ArrayUtil.isEmpty(bytes)) {
			return null;
		}
		try {
			return OBJECT_MAPPER.readValue(bytes, clazz);
		}
		catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static <T> T parseObject(String text, TypeReference<T> typeReference) {
		if (StringUtils.isBlank(text)) {
			return null;
		}
		try {
			return OBJECT_MAPPER.readValue(text, typeReference);
		}
		catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static Dict parseMap(String text) {
		if (StringUtils.isBlank(text)) {
			return null;
		}
		try {
			return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructType(Dict.class));
		}
		catch (MismatchedInputException e) {
			// 类型不匹配说明不是json
			return null;
		}
		catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static List<Dict> parseArrayMap(String text) {
		if (StringUtils.isBlank(text)) {
			return null;
		}
		try {
			return OBJECT_MAPPER.readValue(text,
					OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, Dict.class));
		}
		catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static <T> List<T> parseArray(String text, Class<T> clazz) {
		if (StringUtils.isEmpty(text)) {
			return new ArrayList<>();
		}
		try {
			return OBJECT_MAPPER.readValue(text,
					OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
		}
		catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

}

pom.xml

  <!--hutool工具-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.15</version>
        </dependency>
          <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值