首页 > fastjson 或者 Gson 有没有对某个类自定义 json 的办法。

fastjson 或者 Gson 有没有对某个类自定义 json 的办法。

有一个特殊的类型,不希望按照 bean 方式json 化,能不能用自己的方法替换。


自己解决了:

    ObjectSerializer recordFilter = new ObjectSerializer() {
        @Override
        public void write(JSONSerializer jsonSerializer, Object o, Object o1, Type type) throws IOException {
            SerializeWriter out = jsonSerializer.getWriter();
            Record record = (Record) o;
            if( record != null){
                out.write(record.toJson());
            }else{
                out.writeNull();
            }
        }
    };

    public LocalConductor() {
        SerializeConfig.getGlobalInstance()
                .put(Record.class, recordFilter);

    }

接口可能有变化

使用 SerializeConfig 类的 put 方法,对指定类型的解析进行修改。


JSONSerializer


不是很明白, 可以贴个例子, 如果不用bean的话 可以用fastjson的get方法返回结果

【热门文章】
【热门文章】