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]2.0.41版本,使用的json字符串中的Integer parseObject为Long时报java.lang.Integer cannot be cast to java.lang.Long #1942

Closed
benjava opened this issue Oct 18, 2023 · 10 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@benjava
Copy link

benjava commented Oct 18, 2023

问题描述

web前端修改实体时,id为long型,提交后报java.lang.Integer cannot be cast to java.lang.Long错误

环境信息

请填写以下信息:

  • OS信息: Win10
  • JDK信息: jdk1.8.0_231
  • 版本信息:Fastjson2 2.0.41

重现步骤

示例代码段:
String json = "{"id":1,"name":"joe"}";
JSONObject obj = JSON.parseObject(json);
SerializeConfig config = new SerializeConfig();
config.put(Integer.class, DecimalSerializer.instance);

// ParserConfig parserConfig = new ParserConfig();
// parserConfig.putDeserializer(Integer.class, DecimalSerializer.instance);
SysDic dic = JSON.parseObject(json, SysDic.class);
dic = obj.toJavaObject(SysDic.class);
System.out.println();

输出日志:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

at com.stec.masterdata.entity.common.MasterTreeEntity.setId(MasterTreeEntity.java:19)
at com.alibaba.fastjson2.reader.ORG_1_11_SysDic.readObject(Unknown Source)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:510)
at com.stec.meta.web.test.JSONTest.idRevert(JSONTest.java:67)
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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

Disconnected from the target VM, address: '127.0.0.1:51458', transport: 'socket'

Process finished with exit code -1

@benjava benjava added the bug Something isn't working label Oct 18, 2023
@yanxutao89
Copy link
Collaborator

麻烦提供下完整的测试用例

@benjava
Copy link
Author

benjava commented Oct 18, 2023

SysDic.java

import java.io.Serializable;

@Data
public class SysDic implements Serializable{

private Long id;

private String name;
}

JSONTest.java

public class JSONTest {

    @Test
    public void idRevert(){
        String json = "{\"id\":1,\"name\":\"joe\"}";
        JSONObject obj = JSON.parseObject(json);
        SerializeConfig config = new SerializeConfig();
        config.put(Integer.class, DecimalSerializer.instance);

//        ParserConfig parserConfig = new ParserConfig();
//        parserConfig.putDeserializer(Integer.class, DecimalSerializer.instance);
        SysDic dic = JSON.parseObject(json, SysDic.class);
        dic = obj.toJavaObject(SysDic.class);
        System.out.println();

    }
}

异常结果如帖;
我使用的是fastjson2.0.41的fastjson1适配版!!

@benjava benjava closed this as completed Oct 20, 2023
@benjava benjava reopened this Oct 20, 2023
@benjava
Copy link
Author

benjava commented Oct 20, 2023

再三测试,会随机(一半以上几率)出现,回退至2.0.40版本后问题消失;

@wenshao wenshao added this to the 2.0.42 milestone Oct 20, 2023
@wenshao
Copy link
Member

wenshao commented Oct 20, 2023

DecimalSerializer的代码提供下

@benjava
Copy link
Author

benjava commented Oct 26, 2023

public class DecimalSerializer implements ObjectSerializer {

    public final static SwaggerJsonSerializer instance = new SwaggerJsonSerializer();

    private static final DecimalFormat format = new DecimalFormat("#.################");

    public void write(JSONSerializer serializer,
                      Object object, Object fieldName, Type fieldType, int features) throws IOException {
        SerializeWriter out = serializer.getWriter();
        String value;
        if(object instanceof Long || object instanceof Float) {
            value = format.format(object);
        }
        else {
            value = object.toString();
        }
        out.write(value);
    }
}

wenshao added a commit that referenced this issue Oct 28, 2023
@wenshao
Copy link
Member

wenshao commented Oct 30, 2023

问题无法重现

@benjava
Copy link
Author

benjava commented Nov 1, 2023

我再多研究下

@wenshao wenshao modified the milestones: 2.0.42, 2.0.43 Nov 5, 2023
@wenshao wenshao modified the milestones: 2.0.43, 2.0.44 Dec 3, 2023
@benjava
Copy link
Author

benjava commented Dec 20, 2023

已经找到原因:系泛型问题,对应的id字段是作为泛型在基类(接口)中进行的定义(getter、setter)方法,实现类中使用了Long进行了继承,并创建了对应Long型id字段,但未显示的创建getter和setter方法,而是使用lombok注解@DaTa来生成的,这种情况下在fastjson的2.0.40之后的版本会出现,在实现层显示创建getter和setter方法即可。

@benjava
Copy link
Author

benjava commented Dec 20, 2023

@wenshao 建议针对上述描述,可以检查下是否有这个问题,如果确系lombok或泛型的问题,我就关闭问题,但在2.0.40版本确是好的。

@wenshao wenshao modified the milestones: 2.0.44, 2.0.45 Dec 24, 2023
@wenshao wenshao modified the milestones: 2.0.45, 2.0.46 Jan 7, 2024
@wenshao wenshao added the fixed label Jan 7, 2024
@wenshao
Copy link
Member

wenshao commented Jan 7, 2024

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

@wenshao wenshao closed this as completed Jan 7, 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

3 participants