All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.yulichang.springboot.web.log.toolkit.JsonUtils Maven / Gradle / Ivy

The newest version!
package com.github.yulichang.springboot.web.log.toolkit;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.yulichang.springboot.web.log.exception.SpringbootWebLogException;

import java.util.Map;

/**
 * jackson 工具类
 *
 * @author yulichang
 */
public class JsonUtils {

    /**
     * 转json并且格式化
     */
    public static String toJsonString(Object obj) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new SpringbootWebLogException(e.getMessage());
        }
    }

    /**
     * json格式化
     */
    public static String toJsonString(String str) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return toJsonString(mapper.readValue(str, Map.class));
        } catch (JsonProcessingException e) {
            throw new SpringbootWebLogException(e.getMessage());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy