FastJson工具类
博客专区 > Curry_M 的博客 > 博客详情
FastJson工具类
Curry_M 发表于8个月前
FastJson工具类
  • 发表于 8个月前
  • 阅读 58
  • 收藏 2
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

package com.pingan.common.util;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

/**
 * @JSONField(serialize=false) 在属性get方法上加serialize=false可设置属性不输出 name=""
 *                             可设置序列化后的属性名称 试用场景 实体对象往协议层数据转换
 * @author Fe
 * 
 */
public class FastJson {

    public static final Logger logger = Logger.getLogger(FastJson.class);

    public static SerializeConfig mapping = new SerializeConfig();

    public static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    static {
        mapping.put(Date.class, new SimpleDateFormatSerializer(DEFAULT_DATE_FORMAT));
        JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.DisableCircularReferenceDetect.getMask();
    }

    public static <T> String toJson(T t) {
        return JSON.toJSONString(t);
    }

    public static <T> String toJson(T t, SerializeFilter filter) {
        return JSON.toJSONString(t, filter);
    }

    public static <T> String toJsonCustom(T t) {
        return JSON.toJSONString(t, mapping, SerializerFeature.WriteMapNullValue);
    }

    public static <T> T fromJson(String json, Class<T> t) {
        return (T) JSON.parseObject(json, t);
    }

    public static JSONObject fromJson(String json) {
        return JSON.parseObject(json);
    }

    public static <T> List<T> jsonToList(String json, Class<T> t) {
        return (List<T>) JSON.parseArray(json, t);
    }

    public static JSONArray jsonToList(String json) {
        return JSON.parseArray(json);
    }

    /**
     * 
     * @param object
     *            从jsonobject 目录节点
     * @param strings
     *            层次关系
     * @return
     */
    public static Object xNodes(JSONObject jsonObj, String... dirTree) {
        JSONObject deepJsonObj = jsonObj;
        int last = dirTree.length - 1;
        for (int i = 0; i < dirTree.length; i++) {
            if (deepJsonObj == null) {
                return null;
            }
            String node = dirTree[i];
            if (i == last) {
                return deepJsonObj.get(node);
            }
            deepJsonObj = deepJsonObj.getJSONObject(node);
        }
        return null;
    }

    /**
     * 
     * @param object
     *            从jsonobject 目录节点,最后一层自动增加“Text_Value”
     * @param strings
     *            层次关系
     * @return
     */
    public static Object xNodesForTV(JSONObject jsonObj, String... dirTree) {
        JSONObject testJsonObj = (JSONObject) xNodes(jsonObj, dirTree);
        if (testJsonObj != null) {
            return testJsonObj.get("Text_Value");
        }
        return null;
    }

    public static void main(String args[]) throws UnsupportedEncodingException {
        String s = FastJson.toJson(new double[] { 4, 55 });
        logger.debug(s);
        logger.debug(Md5Util.md5s("11111"));
    }
}

class User {
    private int id;
    private String name;
    private Long status;
    private Date createTime;
    private List<Hobby> hobbyList;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @JSONField(serialize = false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @JSONField(name = "st")
    public Long getStatus() {
        return status;
    }

    public void setStatus(Long status) {
        this.status = status;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public List<Hobby> getHobbyList() {
        return hobbyList;
    }

    public void setHobbyList(List<Hobby> hobbyList) {
        this.hobbyList = hobbyList;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", status=" + status + ", createTime=" + createTime
                + ", hobbyList=" + hobbyList + "]";
    }

}

class Hobby {
    private int hobbyId;
    private String hobbyName;

    public Hobby() {
    }

    public int getHobbyId() {
        return hobbyId;
    }

    public void setHobbyId(int hobbyId) {
        this.hobbyId = hobbyId;
    }

    public String getHobbyName() {
        return hobbyName;
    }

    public void setHobbyName(String hobbyName) {
        this.hobbyName = hobbyName;
    }

}

 

标签: fastjson
共有 人打赏支持
粉丝 2
博文 38
码字总数 40117
×
Curry_M
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: