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

fastjson2 怎么统一指定timestamp 转换格式 #2408

Closed
Johnson-Jia opened this issue Apr 8, 2024 · 4 comments
Closed

fastjson2 怎么统一指定timestamp 转换格式 #2408

Johnson-Jia opened this issue Apr 8, 2024 · 4 comments
Assignees
Labels
bug Something isn't working fixed
Milestone

Comments

@Johnson-Jia
Copy link

问题描述

jdk 8
Dubbo

目前 A 系统使用的 java.util.Date 对象接收数据库里面的数据,拿到后发现是 java.sql.Timestamp
B 系统接口的入参为 java.util.Date ,当A 系统调用 B 系统的时候 出现无法转换问题。

Dubbo 中使用 fastjson2 能否统一 兼容支持 Timestamp 转 Date ?

@Johnson-Jia Johnson-Jia added the bug Something isn't working label Apr 8, 2024
@Johnson-Jia
Copy link
Author

Johnson-Jia commented Apr 8, 2024

例如:

  Timestamp timestamp = new Timestamp(new Date().getTime());
        byte[] bytes = JSONB.toBytes(timestamp, JSONWriter.Feature.WriteClassName, JSONWriter.Feature.FieldBased,
            JSONWriter.Feature.ErrorOnNoneSerializable, JSONWriter.Feature.ReferenceDetection,
            JSONWriter.Feature.WriteNulls, JSONWriter.Feature.NotWriteDefaultValue,
            JSONWriter.Feature.NotWriteHashMapArrayListClassName, JSONWriter.Feature.WriteNameAsSymbol);

        Date result = JSONB.parseObject(bytes, Date.class, JSONReader.Feature.UseDefaultConstructorAsPossible,
            JSONReader.Feature.ErrorOnNoneSerializable, JSONReader.Feature.IgnoreAutoTypeNotMatch,
            JSONReader.Feature.UseNativeObject, JSONReader.Feature.FieldBased);
        System.out.println(result);

Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:64)
Caused by: java.lang.UnsupportedOperationException
at com.alibaba.fastjson2.JSONReaderJSONB.getStringLength(JSONReaderJSONB.java:5159)
at com.alibaba.fastjson2.JSONReader.readMillisFromString(JSONReader.java:1628)
at com.alibaba.fastjson2.reader.ObjectReaderImplDate.readDate(ObjectReaderImplDate.java:175)
at com.alibaba.fastjson2.reader.ObjectReaderImplDate.readJSONBObject(ObjectReaderImplDate.java:54)
at com.alibaba.fastjson2.JSONB.parseObject(JSONB.java:748)

@wenshao wenshao added this to the 2.0.49 milestone Apr 8, 2024
@wenshao wenshao self-assigned this Apr 8, 2024
@wenshao wenshao added the fixed label Apr 9, 2024
@wenshao
Copy link
Member

wenshao commented Apr 9, 2024

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

@Johnson-Jia
Copy link
Author

此问题本地验证已通过。

@wenshao
Copy link
Member

wenshao commented Apr 14, 2024

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

No branches or pull requests

2 participants