We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
我清楚如果不涉及到跨时区,用LocalDateTime会更容易,但是现有系统设计时就用了ZonedDateTime
public class VO{ @JSONField(format="yyyy-MM-dd HH:mm:ss") private ZonedDateTime modifyTime=ZonedDateTime .now(); //...getter/setter } System.err.println(JSON.toJSONString(new VO())); //{"modifyTime": "2017-01-04 13:21:39"} 输出的是东八区,也就是按照@JSONField输出本地时区的日期时间
但是反序列化就不行了
JSON.parseObject("{'modifyTime': '2017-01-04 13:21:39'}", VO.class); Exception in thread "main" com.alibaba.fastjson.JSONException: Text '2017-01-04 13:21:39' could not be parsed at index 10 at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:643) at com.alibaba.fastjson.JSON.parseObject(JSON.java:339) at com.alibaba.fastjson.JSON.parseObject(JSON.java:243) at com.alibaba.fastjson.JSON.parseObject(JSON.java:456) at demo.main(demo.java:179) Caused by: java.time.format.DateTimeParseException: Text '2017-01-04 13:21:39' could not be parsed at index 10 at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) at java.time.ZonedDateTime.parse(ZonedDateTime.java:597) at java.time.ZonedDateTime.parse(ZonedDateTime.java:582) at com.alibaba.fastjson.parser.deserializer.Jdk8DateCodec.deserialze(Jdk8DateCodec.java:95) at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:66) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:722) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:568) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:187) at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:183) at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:639) ... 4 more
JSON.parseObject("{'modifyTime': '2017-01-04 13:21:39'}", Appointment.class);
https://github.com/alibaba/fastjson/blob/master/src/main/java/com/alibaba/fastjson/parser/deserializer/Jdk8DateCodec.java#L64-L97
在Jdk8DateCodec中只对LocalDateTime进行了特殊处理,ZonedDateTime仅仅是ZonedDateTime.parse(text);
需要传入 类似 2017-01-04T13:21:39.005+08:00[Asia/Shanghai] 格式的才行,否则统统异常
2017-01-04T13:21:39.005+08:00[Asia/Shanghai]
jackson可以注册一个全局的类型,进行处理
@Configuration public class JacksonConfiguration { public static final DateTimeFormatter ISO_FIXED_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/Shanghai")); @Bean public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() { ObjectMapper objectMapper=new ObjectMapper(); objectMapper.registerModule(new JavaTimeModule()); return jackson2ObjectMapperBuilder -> jackson2ObjectMapperBuilder.serializers(new ZonedDateTimeSerializer(ISO_FIXED_FORMAT)); } }
在FastJson中有 JSON.parseObject(input, clazz, config, features)方法进行处理,但是,
ParserConfig config=new ParserConfig(); config.getDerializers().put(ZonedDateTime.class, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/Shanghai")));
会报错,因为,value是ObjectDeserializer类型的
The text was updated successfully, but these errors were encountered:
@wenshao 在网上只找到了一个FastJson的ZonedDateTime相关代码片段 http://apiwave.com/java/snippets/addition/java.time.OffsetDateTime
Sorry, something went wrong.
bug fixed for ZonedDateTime decode. issue #980
b601781
谢啦,温少 : )
bug fixed for ZonedDateTime decode. issue alibaba#980
6a787a7
No branches or pull requests
我清楚如果不涉及到跨时区,用LocalDateTime会更容易,但是现有系统设计时就用了ZonedDateTime
但是反序列化就不行了
JSON.parseObject("{'modifyTime': '2017-01-04 13:21:39'}", Appointment.class);
https://github.com/alibaba/fastjson/blob/master/src/main/java/com/alibaba/fastjson/parser/deserializer/Jdk8DateCodec.java#L64-L97
在Jdk8DateCodec中只对LocalDateTime进行了特殊处理,ZonedDateTime仅仅是ZonedDateTime.parse(text);
需要传入 类似
2017-01-04T13:21:39.005+08:00[Asia/Shanghai]
格式的才行,否则统统异常jackson可以注册一个全局的类型,进行处理
在FastJson中有 JSON.parseObject(input, clazz, config, features)方法进行处理,但是,
会报错,因为,value是ObjectDeserializer类型的
The text was updated successfully, but these errors were encountered: