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

[BUG] 对于LocalTime的支持有问题, 且JSONField无效 #591

Closed
RandyZ opened this issue Jul 28, 2022 · 5 comments
Closed

[BUG] 对于LocalTime的支持有问题, 且JSONField无效 #591

RandyZ opened this issue Jul 28, 2022 · 5 comments
Labels
bug Something isn't working
Milestone

Comments

@RandyZ
Copy link

RandyZ commented Jul 28, 2022

问题描述

反序列化JSON有问题,ObjectReaderImplLocalTime未正确读取到format

环境信息

请填写以下信息:

  • OS信息: win11
  • JDK信息:Openjdk 1.8.0_312]
  • 版本信息:Fastjson2 2.0.10

重现步骤

如何操作可以重现该问题:

@Test
    void test2() {
        String testStr = "{\"time\": \"09:00\", \"nest\": {\"time\": \"09:00\"}, \"nests\": [{\"time\": \"09:00\"}]}";

        TestAA aa = JSONObject.parseObject(testStr, TestAA.class);
    }

    @Data
    public class TestAA {
        private LocalTime time;
        private TestBB nest;
//        private List<TestBB> nests;
    }

//    @Data
    public class TestBB {
        @JSONField(name = "time", format = "HH:mm")
        private LocalTime time;

        public LocalTime getTime() {
            return time;
        }

        public void setTime(LocalTime time) {
            this.time = time;
        }
    //        private List<TestCC> nests;
    }

期待的正确结果

fix

相关日志输出

com.alibaba.fastjson2.JSONException: not support len : 5

	at com.alibaba.fastjson2.JSONReader.readLocalTime(JSONReader.java:1014)
	at com.alibaba.fastjson2.reader.ObjectReaderImplLocalTime.readObject(ObjectReaderImplLocalTime.java:52)
	at com.alibaba.fastjson2.reader.FieldReaderObjectMethod.readFieldValue(FieldReaderObjectMethod.java:140)
	at com.alibaba.fastjson2.reader.ObjectReaderNoneDefaultConstrutor.readObject(ObjectReaderNoneDefaultConstrutor.java:269)
	at com.alibaba.fastjson2.JSON.parseObject(JSON.java:453)
	at com.weiming.ddi.utils.FastJson2Test.test2(FastJson2Test.java:35)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
	at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
...
@RandyZ RandyZ added the bug Something isn't working label Jul 28, 2022
@RandyZ
Copy link
Author

RandyZ commented Jul 28, 2022

感觉ReaderImpl的实现有点粗糙,这个直接用了默认实现,且没看到源码中什么hook可以注入自己的实现

if (type == LocalTime.class) {
    return ObjectReaderImplLocalTime.INSTANCE;
}

static final ObjectReaderImplLocalTime INSTANCE = new ObjectReaderImplLocalTime(null, null);

@RandyZ
Copy link
Author

RandyZ commented Jul 28, 2022

临时方案,复制ObjectReaderImplLocalTime,传入相应格式。
请阿里的同行帮忙看看还有什么其他方式能传入自己的时间格式吗?我看使用的地方基本都是default访问。能否开放一个接口或者注解,方便开发者注入这些格式?

try (JSONReader reader = JSONReader.of(testStr)) {
    JSONReader.Context context = reader.getContext();

    boolean fieldBased = (context.getFeatures() & JSONReader.Feature.FieldBased.mask) != 0;
    context.getProvider().registerIfAbsent(LocalTime.class, new ObjectReaderImplLocalTime("HH:mm", null));
    ObjectReader<TestAA> objectReader = context.getProvider().getObjectReader(TestAA.class, fieldBased);

    TestAA object = objectReader.readObject(reader, null, null, 0);
}

@wenshao wenshao added this to the 2.0.11 milestone Jul 28, 2022
@wenshao
Copy link
Member

wenshao commented Jul 30, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.11-SNAPSHOT/
问题已修复,请用2.0.11-SNAPSHOT版本帮忙验证,2.0.11版本预计在8月7日发布

@wenshao
Copy link
Member

wenshao commented Aug 7, 2022

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

@wenshao wenshao closed this as completed Aug 7, 2022
@RandyZ
Copy link
Author

RandyZ commented Aug 10, 2022

已验证,thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants