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
}
}
1条答案
按热度按时间unftdfkk1#
it look like a bug