我在 配置类编写了 自定义的序列化 规则,但是为什么没有使用自定义的规则,还是使用了默认的规则。
JSON.register(LocalDate.class, MyLocalDateWriter.instance);
经下断点调试: 并没有运行 MyLocalDateWriter.instance 中的 write 方法,而是运行了自带的 com.alibaba.fastjson2.writer.ObjectWriterImplLocalDate 中的 write 方法,导致了 输出结果达不到期望效果。
期望效果:2022-06-13 实际效果:2022-06-13 00:00:00
是不是我的写法有问题,请多指教,写法参考 实现ObjectWriter和ObjectReader实现定制序列化和反序列化 https://alibaba.github.io/fastjson2/register_custom_reader_writer_cn
package re.lpy.supermall.user.config;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONReader; import com.alibaba.fastjson2.JSONWriter; import com.alibaba.fastjson2.codec.DateTimeCodec; import com.alibaba.fastjson2.support.config.FastJsonConfig; import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter; import com.alibaba.fastjson2.writer.ObjectWriter; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.Collections; import java.util.List;
@Configuration @EnableWebMvc public class FastJson2Configurer implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//自定义配置...
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
config.setReaderFeatures(JSONReader.Feature.FieldBased,
JSONReader.Feature.SupportArrayToBean,
JSONReader.Feature.InitStringFieldAsEmpty,
JSONReader.Feature.SupportAutoType,
JSONReader.Feature.SupportClassForName,
JSONReader.Feature.IgnoreSetNullValue);
// JSONWriter.Feature.WriteNulls, 输出空值
config.setWriterFeatures(
JSONWriter.Feature.BrowserCompatible,
JSONWriter.Feature.NullAsDefaultValue,
JSONWriter.Feature.PrettyFormat,
JSONWriter.Feature.WriteBigDecimalAsPlain);
converter.setFastJsonConfig(config);
converter.setDefaultCharset(StandardCharsets.UTF_8);
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
// 移除jackson
converters.removeIf(converters::contains);
converters.add(0, converter);
JSON.register(LocalDateTime.class, MyLocalDateTimeWriter.instance); // 1
JSON.register(LocalDate.class, MyLocalDateWriter.instance); // 1
JSON.register(LocalTime.class, MyLocalTimeWriter.instance); // 1
}
}
class MyLocalDateTimeWriter extends DateTimeCodec implements ObjectWriter { static MyLocalDateTimeWriter instance = new MyLocalDateTimeWriter(null);
public MyLocalDateTimeWriter(String format) {
super(format);
}
@Override
public void write(JSONWriter jsonWriter, Object o, Object o1, Type type, long l) {
if (o == null) {
jsonWriter.writeNull();
return;
}
String typeName = type.getTypeName();
LocalDateTime dateTime = (LocalDateTime) o;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = formatter.format(dateTime);
jsonWriter.writeString(format);
}
}
class MyLocalDateWriter implements ObjectWriter { static MyLocalDateWriter instance = new MyLocalDateWriter();
@Override
public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
if (object == null) {
jsonWriter.writeNull();
return;
}
LocalDate date = (LocalDate) object;
// LocalDate date = dateTime.toLocalDate();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String format = formatter.format(date);
// 优先使用name
jsonWriter.writeString(format);
}
}
class MyLocalTimeWriter implements ObjectWriter { static MyLocalTimeWriter instance = new MyLocalTimeWriter();
@Override
public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
if (object == null) {
jsonWriter.writeNull();
return;
}
LocalTime time = (LocalTime) object;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String format = formatter.format(time);
// 优先使用name
jsonWriter.writeString(format);
}
}
fastjson 2.0.7 最新版 ,2.0.5 是可以这样的,2.0.6未知 openjdk1.8.333
原提问者GitHub用户yangcanlin
请用2.0.8-SNAPSHOT验证,可以如下这样的写法:
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.8-SNAPSHOT/
public class FormatTest { @Test public void test() { LocalDate date = LocalDate.of(2017, 12, 13); LocalDateTime dateTime = LocalDateTime.of(2017, 1, 2, 12, 13, 14);
Bean bean = new Bean();
bean.date = date;
bean.dateTime = dateTime;
JSON.mixIn(LocalDateTime.class, LocalDateTimeMixin.class);
assertEquals("{\"date\":\"2017-12-13\",\"dateTime\":\"2017-01-02 12:13:14\"}", JSON.toJSONString(bean));
// clear cache
JSON.mixIn(LocalDateTime.class, null);
JSON.mixIn(Bean.class, null);
assertEquals("{\"date\":\"2017-12-13\",\"dateTime\":\"2017-01-02T12:13:14\"}", JSON.toJSONString(bean));
JSON.mixIn(LocalDateTime.class, null);
JSON.mixIn(Bean.class, null);
JSON.mixIn(LocalDate.class, LocalDateMixin.class);
assertEquals("\"2017-12-13 00:00:00\"", JSON.toJSONString(date));
JSON.mixIn(LocalDate.class, null);
assertEquals("\"2017-12-13\"", JSON.toJSONString(date));
}
@JSONType(format = "yyyy-MM-dd HH:mm:ss")
public static class LocalDateMixin {
}
@JSONType(format = "yyyy-MM-dd HH:mm:ss")
public static class LocalDateTimeMixin {
}
public static class Bean {
public LocalDateTime dateTime;
public LocalDate date;
}
}
原回答者GitHub用户wenshao
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。