fastjson 关于正式生产项目使用FastJSON toJSON 数据字典的实现

ddhy6vgd  于 2021-11-27  发布在  Java
关注(0)|答案(0)|浏览(259)

自定义jackson比较困难,刚出炉的自定义fastJSON,个人感觉还是稍微有点作用的,故写出来,众大神忽略.

第一次使用md 见谅

1、首先我们来定义数据字典

以性别来说  1-男  ,  2女  字典类型值 userSex

` private static Map<String, Map<String, String>> dicMap = new HashMap<String, Map<String, String>>();

static {
    Map<String, String> userSexDicMap = new HashMap<String, String>();
    userSexDicMap.put("1", "男");
    userSexDicMap.put("2", "女");
    dicMap.put("userSex", userSexDicMap);
}`

2、定义entity对象

`public class UserInfo {

private String userName;
@Dictionary(key = "userSex")
private String userSex;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date createDate;

public UserInfo(String userName, String userSex, Date createDate) {
    this.userName = userName;
    this.userSex = userSex;
    this.createDate = createDate;
}

}`

3、定义JSONContextValueFilter

`public class JSONContextValueFilter implements ContextValueFilter {

public Object process(BeanContext context, Object object, String name, Object value) {
    QfDictionary dicAnnation = context.getAnnation(QfDictionary.class);
    if (null != dicAnnation) {
        String key = dicAnnation.key();
        return dicMap.get(key).get((String) value);
    }

    DateTimeFormat formatAnnation = context.getAnnation(DateTimeFormat.class);
    if (null != formatAnnation) {
        return new SimpleDateFormat(formatAnnation.pattern()).format(value);
    }

    return value;
}

}`

4、结果对比

使用filter {"createDate":"2016-12-20","userName":"张三","userSex":"男"}
未使用filter {"createDate":1482200509090,"userName":"张三","userSex":"1"}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题