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

com.github.joekerouac.common.tools.util.JsonUtil Maven / Gradle / Ivy

The newest version!
// Generated by delombok at Fri Mar 14 11:41:38 CST 2025
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
 * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
 * to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package com.github.joekerouac.common.tools.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicReference;
import javax.validation.constraints.NotNull;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.joekerouac.common.tools.codec.exception.SerializeException;
import com.github.joekerouac.common.tools.codec.json.JacksonJsonCodec;
import com.github.joekerouac.common.tools.enums.ErrorCodeEnum;
import com.github.joekerouac.common.tools.exception.CommonException;
import com.github.joekerouac.common.tools.reflect.type.AbstractTypeReference;
import com.github.joekerouac.common.tools.string.StringUtils;

/**
 * JSON工具
 * 
 * @since 1.0.0
 * @author JoeKerouac
 * @date 2022-10-14 14:37:00
 */
public class JsonUtil {
    private static final AtomicReference mapperAtomicReference = new AtomicReference<>();
    private static final JacksonJsonCodec CODER = new JacksonJsonCodec(mapperAtomicReference::set);

    /**
     * 读取数据
     * 
     * @param data
     *            数据
     * @param type
     *            数据类型Class
     * @param 
     *            数据实际类型
     * @return 读取到的数据
     * @throws SerializeException
     *             序列化失败应该抛出SerializeException而不是其他异常
     */
    @Deprecated
    public static  T read(byte[] data, Class type) throws SerializeException {
        return CODER.read(data, type);
    }

    /**
     * 读取数据
     *
     * @param data
     *            数据
     * @param type
     *            数据类型Class
     * @param 
     *            数据实际类型
     * @return 读取到的数据
     * @throws SerializeException
     *             序列化失败应该抛出SerializeException而不是其他异常
     */
    public static  T read(String data, Class type) throws SerializeException {
        return CODER.read(data.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8, type);
    }

    /**
     * 读取数据
     *
     * @param data
     *            数据
     * @param charset
     *            数据对应的字符集,为空时默认使用utf8
     * @param type
     *            数据类型Class
     * @param 
     *            数据实际类型
     * @return 读取到的数据
     * @throws SerializeException
     *             序列化失败应该抛出SerializeException而不是其他异常
     */
    public static  T read(byte[] data, Charset charset, Class type) throws SerializeException {
        return CODER.read(data, charset, type);
    }

    /**
     * 读取数据
     * 
     * @param data
     *            数据
     * @param typeReference
     *            数据类型引用
     * @param 
     *            数据实际类型
     * @return 读取到的数据
     * @throws SerializeException
     *             序列化失败应该抛出SerializeException而不是其他异常
     */
    @Deprecated
    public static  T read(byte[] data, AbstractTypeReference typeReference) throws SerializeException {
        return CODER.read(data, typeReference);
    }

    /**
     * 读取数据
     *
     * @param data
     *            数据
     * @param charset
     *            数据的字符集,为空时默认使用utf8
     * @param typeReference
     *            数据类型引用
     * @param 
     *            数据实际类型
     * @return 读取到的数据
     * @throws SerializeException
     *             序列化失败应该抛出SerializeException而不是其他异常
     */
    public static  T read(byte[] data, Charset charset, AbstractTypeReference typeReference) throws SerializeException {
        return CODER.read(data, charset, typeReference);
    }

    /**
     * 读取数据
     *
     * @param data
     *            数据
     * @param typeReference
     *            数据类型引用
     * @param 
     *            数据实际类型
     * @return 读取到的数据
     * @throws SerializeException
     *             序列化失败应该抛出SerializeException而不是其他异常
     */
    public static  T read(String data, AbstractTypeReference typeReference) throws SerializeException {
        if (StringUtils.isBlank(data)) {
            return null;
        }
        return CODER.read(data.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8, typeReference);
    }

    /**
     * 读取数据
     *
     * @param inputStream
     *            数据输入流
     * @param type
     *            数据类型
     * @param 
     *            数据实际类型
     * @return 读取到的数据
     * @throws SerializeException
     *             序列化失败应该抛出SerializeException而不是其他异常
     */
    public static  T read(InputStream inputStream, @NotNull Class type) throws SerializeException {
        return CODER.read(inputStream, type);
    }

    /**
     * 读取数据
     *
     * @param inputStream
     *            数据输入流
     * @param typeReference
     *            数据类型引用
     * @param 
     *            数据实际类型
     * @return 读取到的数据
     * @throws SerializeException
     *             序列化失败应该抛出SerializeException而不是其他异常
     */
    public static  T read(InputStream inputStream, @NotNull AbstractTypeReference typeReference) throws SerializeException {
        return CODER.read(inputStream, typeReference);
    }

    /**
     * 读取数据
     *
     * @param inputStream
     *            数据输入流
     * @param charset
     *            数据对应的数据集,传null时默认使用utf8
     * @param type
     *            数据类型
     * @param 
     *            数据实际类型
     * @return 读取到的数据
     * @throws SerializeException
     *             序列化失败应该抛出SerializeException而不是其他异常
     */
    public static  T read(InputStream inputStream, Charset charset, @NotNull Class type) throws SerializeException {
        return CODER.read(inputStream, charset, type);
    }

    /**
     * 读取数据
     *
     * @param inputStream
     *            数据输入流
     * @param charset
     *            数据对应的数据集,传null时默认使用utf8
     * @param typeReference
     *            数据类型引用
     * @param 
     *            数据实际类型
     * @return 读取到的数据
     * @throws SerializeException
     *             序列化失败应该抛出SerializeException而不是其他异常
     */
    public static  T read(InputStream inputStream, Charset charset, @NotNull AbstractTypeReference typeReference) throws SerializeException {
        return CODER.read(inputStream, charset, typeReference);
    }

    /**
     * 写出数据
     * 
     * @param data
     *            数据
     * @return 序列化后的数据,UTF8编码
     */
    @Deprecated
    public static byte[] write(Object data) {
        return CODER.write(data);
    }

    /**
     * 写出数据
     *
     * @param data
     *            数据
     * @param resultCharset
     *            结果字符集
     * @return 序列化后的数据
     */
    public static byte[] write(Object data, Charset resultCharset) {
        return CODER.write(data, resultCharset);
    }

    /**
     * 写出数据到输出流
     *
     * @param data
     *            数据
     * @param outputStream
     *            写出流
     */
    public static void write(Object data, OutputStream outputStream) {
        CODER.write(data, outputStream);
    }

    /**
     * 写出数据到输出流
     *
     * @param data
     *            数据
     * @param resultCharset
     *            结果字符集,如果为空则使用utf8字符集
     * @param outputStream
     *            写出流
     */
    public static void write(Object data, Charset resultCharset, OutputStream outputStream) {
        CODER.write(data, resultCharset, outputStream);
    }

    /**
     * 写出数据
     *
     * @param data
     *            数据
     * @return 序列化后的数据
     */
    public static String writeAsString(Object data) {
        return new String(CODER.write(data, StandardCharsets.UTF_8), StandardCharsets.UTF_8);
    }

    /**
     * 格式化的形式写出数据,数据是美化的
     * 
     * @param data
     *            数据
     * @return 美化后的输出,UTF8编码
     */
    public static byte[] writeWithFormat(Object data) {
        try {
            // 默认换行是取的系统参数line.separator的值,Windows下是\r\n,我们这里写死所有系统都是\n
            return mapperAtomicReference.get().writer(new DefaultPrettyPrinter().withObjectIndenter(new DefaultIndenter("  ", "\n"))).writeValueAsBytes(data);
        } catch (JsonProcessingException e) {
            throw new CommonException(ErrorCodeEnum.UNKNOWN_EXCEPTION, StringUtils.format("数据[{}]json格式化输出异常", data), e);
        }
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    private JsonUtil() {
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy