当前位置: 首页 > 教程 > Fastjson >

Fastjson ParseProcess

精华
小牛编辑
116浏览
2023-03-14

ParseProcess是编程扩展定制反序列化的接口。Fastjson支持如下ParseProcess:

  • ExtraProcessor:用于处理多余的字段;
  • ExtraTypeProvider:用于处理多余字段时提供类型信息。

先准备好后面示例所需要的的实体类VO:

package cn.xnip.domain;

import java.util.HashMap;
import java.util.Map;

/**
 * 小牛知识库网 - https://www.xnip.cn
 */
public class VO {
    private int id;
    private Map<String, Object> attributes = new HashMap<String, Object>();

    public int getId() {
        return id;
    }

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

    public Map<String, Object> getAttributes() {
        return attributes;
    }

    @Override
    public String toString() {
        return "VO [id=" + id + ", attributes=" + attributes + "]";
    }
}

1 使用ExtraProcessor处理多余字段

1.1 编写测试类

package cn.xnip.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
import cn.xnip.domain.VO;

/**
 * 小牛知识库网 - https://www.xnip.cn
 */
public class MainApp {

    public static void main(String args[]){
        ExtraProcessor processor = new ExtraProcessor() {
            public void processExtra(Object object, String key, Object value) {

                System.out.println("---------------object = " + object);
                System.out.println("---------------key = " + key);
                System.out.println("---------------value = " + value);
                System.out.println();

                VO vo = (VO) object;
                vo.setId(789);// 修改一下id值
                vo.getAttributes().put(key, value);
            }
        };
        // 这里name和phone是多余的,在VO里没有
        VO vo = JSON.parseObject("{\"id\":123,\"name\":\"abc\",\"phone\":\"18603396954\"}", VO.class, processor);

        System.out.println("vo.getId() = " + vo.getId());
        System.out.println("vo.getAttributes().get(\"name\") = " + vo.getAttributes().get("name"));
        System.out.println("vo.getAttributes().get(\"phone\") = " + vo.getAttributes().get("phone"));
    }

}

1.2 运行结果

---------------object = VO [id=123, attributes={}]
---------------key = name
---------------value = abc

---------------object = VO [id=789, attributes={name=abc}]
---------------key = phone
---------------value = 18603396954

vo.getId() = 789
vo.getAttributes().get("name") = abc
vo.getAttributes().get("phone") = 18603396954

2 使用ExtraTypeProvider为多余的字段提供类型

2.1 编写测试类

package cn.xnip.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
import com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider;
import cn.xnip.domain.VO;

import java.lang.reflect.Type;

/**
 * 小牛知识库网 - https://www.xnip.cn
 */
public class MainApp {

    public static void main(String args[]){
        ExtraProcessor processor = new MyExtraProcessor();

        VO vo = JSON.parseObject("{\"id\":123,\"value\":\"123456\"}", VO.class, processor);

        System.out.println("vo.getId() = " + vo.getId());
        System.out.println("vo.getAttributes().get(\"value\") = " + vo.getAttributes().get("value"));
        System.out.println("vo.getAttributes().get(\"value\").getClass() = " + vo.getAttributes().get("value").getClass());
        // value本应该是字符串类型的,通过getExtraType的处理变成Integer类型了。
    }

}

class MyExtraProcessor implements ExtraProcessor, ExtraTypeProvider {
    public void processExtra(Object object, String key, Object value) {
        VO vo = (VO) object;
        vo.getAttributes().put(key, value);
    }

    public Type getExtraType(Object object, String key) {

        System.out.println("---------------object = " + object);
        System.out.println("---------------key = " + key);
        System.out.println();

        if ("value".equals(key)) {
            return int.class;
        }
        return null;
    }
};

2.2 运行结果 

---------------object = VO [id=123, attributes={}]
---------------key = value

vo.getId() = 123
vo.getAttributes().get("value") = 123456
vo.getAttributes().get("value").getClass() = class java.lang.Integer