Skip to content
New issue

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

[QUESTION] ObjectReader 怎么实现? #356

Closed
yangcanlin opened this issue May 26, 2022 · 7 comments
Closed

[QUESTION] ObjectReader 怎么实现? #356

yangcanlin opened this issue May 26, 2022 · 7 comments
Labels
bug Something isn't working question Further information is requested
Milestone

Comments

@yangcanlin
Copy link

你好,请问能不能提供一个 实现 ObjectReader 的例子,FastJson2 网上教程太难找了,全是 FastJson1 的教程,然后 com.alibaba.fastjson2.writer 包下的类是不是现在还不能使用,如 ObjectWriterImplLocalDateTime.class

我想解决的问题是,后端接收 LocalDateTime 属性,前端传值:“2022-05-26”,正常,但是传值:“2022-05-26 16:42:11” 就会报错,下面是我写的 配置类 , 学习的路上,能不能提供一个解决方案,琢磨一整天了。

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.reader.ObjectReader;
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.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;

@Configuration
@EnableWebMvc
public class MyWebMvcConfigurer implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonConfig config = new FastJsonConfig();
        
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        
        config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean, JSONReader.Feature.SupportAutoType);
        config.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.WriteNulls);
        
        JSON.register(LocalDateTime.class, MyLocalDateTimeWriter.instance);
        JSON.register(LocalDate.class, MyLocalDateWriter.instance);
    
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        converter.setFastJsonConfig(config);
        converter.setDefaultCharset(StandardCharsets.UTF_8);
        converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
        converters.add(0, converter);
    }
}

class MyLocalDateTimeWriter implements ObjectWriter {
    static MyLocalDateTimeWriter instance = new MyLocalDateTimeWriter();
    
    @Override
    public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
        if (object == null) {
            jsonWriter.writeNull();
            return;
        }
        LocalDateTime dateTime = (LocalDateTime) object;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = formatter.format(dateTime);
        // 优先使用name
        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;
        }
        LocalDateTime dateTime = (LocalDateTime) object;
        LocalDate date = dateTime.toLocalDate();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format = formatter.format(date);
        // 优先使用name
        jsonWriter.writeString(format);
    }
}
@yangcanlin yangcanlin added the question Further information is requested label May 26, 2022
@wenshao
Copy link
Member

wenshao commented May 26, 2022

收到,我周六看你的例子,并且补一个文档

@wenshao
Copy link
Member

wenshao commented May 29, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.5
2.0.5支持自动识别了,不用自己写ObjectReader/ObjectWriter,你升级试试看

@yangcanlin
Copy link
Author

https://github.com/alibaba/fastjson2/releases/tag/2.0.5 2.0.5支持自动识别了,不用自己写ObjectReader/ObjectWriter,你升级试试看

好的 我明天试一下 感谢大佬

@yangcanlin
Copy link
Author

你好,我刚刚试了,LocalDate 确实没问题了,但是 LocalTime 好像没有修复哦,你看看是我操作错误还是没有修复。

// 全局注册
JSON.register(LocalTime.class, JdbcSupport.createTimeReader("yyyy-MM-dd HH:mm:ss", Locale.CHINESE));

// 前端输入
{ "localTime": "2022-05-30 11:36:11" }

虽说很少用上 LocalTime 吧,但难免还是有用上的,期待,谢谢!

@wenshao wenshao added this to the 2.0.7 milestone Jun 6, 2022
@wenshao wenshao added the bug Something isn't working label Jun 6, 2022
@yangcanlin
Copy link
Author

注意!!! 2.0.5 修复了 LocalDate 的问题,2.0.6 又出现了 LocalDate 报错问题!!!

异常:Could not write JSON: FASTJSON-2.0.6 write JSON errorUnsupported field: HourOfDay; nested exception is com.alibaba.fastjson2.JSONException: FASTJSON-2.0.6 write JSON errorUnsupported field: HourOfDay

@wenshao
Copy link
Member

wenshao commented Jun 7, 2022

@wenshao
Copy link
Member

wenshao commented Jun 11, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.7
问题已经修复,请用新版本

@wenshao wenshao closed this as completed Jun 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants