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

top.doudou.core.builder.JsonBuilder Maven / Gradle / Ivy

There is a newer version: 1.3.2
Show newest version
package top.doudou.core.builder;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.List;

/**
 * @Description json的构建工具类
 * @Author 傻男人 <[email protected]>
 * @Date 2020-11-13 17:28
 * @Version V1.0
 */
public class JsonBuilder {

    /**
     * 忽略大写的json转换
     * 再类上加@JsonIgnoreProperties(ignoreUnknown = true)
     * 假如序列化为其他的属性@SerializedName("其他的属性字段值")
     * @return
     */
    public static Gson gson(){
        return new GsonBuilder()
                .enableComplexMapKeySerialization()
                //序列化空字段
                .serializeNulls()
                //将json转化为适合页面输出的json  可能影响json的序列化
                .setPrettyPrinting()
                //按原样传递HTML字符 不转义html字符
                .disableHtmlEscaping()
                .create();
    }

    /**
     * 不序列化null的字段
     * @return
     */
    public static Gson gsonNotSerializeNull(){
        return new GsonBuilder()
                .enableComplexMapKeySerialization()
                //将json转化为适合页面输出的json  可能影响json的序列化
                .setPrettyPrinting()
                //按原样传递HTML字符 不转义html字符
                .disableHtmlEscaping()
                .create();
    }

    /**
     * json转list
     * @return
     */
    public static List fromJsonToList(String dataJson,Class target){
        return gson().fromJson(dataJson, new TypeToken>(){}.getType());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy