fastjson Bug when PrettyFormat=false

ztyzrc3y  于 2021-11-27  发布在  Java
关注(0)|答案(1)|浏览(324)

Model:

public class Model {
    long julianDate;
    public Model() {  }
    public long getJulianDate() {
        return julianDate;
    }
    public void setJulianDate(long julianDate) {
        this.julianDate = julianDate;
    }
}

Serializer:

public class LongSerializer implements ObjectSerializer {

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        SerializeWriter out   = serializer.out;
        long            value = (long) object;
        if (value == Long.MAX_VALUE) {
            out.writeString("never");
        } else {
            out.writeString(new SimpleDateFormat("yyyy-MM-dd").format(new Date(value)));
        }
    }
}

Test:

public class Test {
    public static void main(String[] args) {
        Model model = new Model();
        model.julianDate = new Date().getTime();
        SerializeConfig.getGlobalInstance().put(Long.class, new LongSerializer());
        System.out.println(JSON.toJSONString(model, true));
           // {
	   //       "julianDate":"2017-10-10"
           // }
        System.out.println(JSON.toJSONString(model, false));
           //  {"julianDate":1507670658523}   <-- LongSerializer IGNORED
    }
}

相关问题