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] JSONB 序列化和反序列化使用com.google.common.collect.Lists.transform 的list报错 #400

Closed
dyj2012 opened this issue Jun 2, 2022 · 3 comments
Labels
bug Something isn't working jsonb
Milestone

Comments

@dyj2012
Copy link

dyj2012 commented Jun 2, 2022

问题描述

JSONB反序列化失败

环境信息

请填写以下信息:

  • OS信息: [e.g.:CentOS 8.4.2105 4Core 3.10GHz 16 GB]
  • JDK信息: [e.g.:Openjdk 1.8.0_312]
  • 版本信息:[e.g.:Fastjson2 2.x.x]

重现步骤

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

  1. 使用 xxx.xxx 方法

  2. 输入 ... 数据

  3. 出现 `com.alibaba.fastjson2.JSONException: create instance error class com.google.common.collect.Lists$TransformingRandomAccessList

    at com.alibaba.fastjson2.reader.ObjectReaderImplList.readJSONBObject(ObjectReaderImplList.java:351)
    at com.alibaba.fastjson2.reader.ObjectReader_1.readJSONBObject(Unknown Source)
    at com.alibaba.fastjson2.JSONB.parseObject(JSONB.java:505)
    at com.yuanian.infrastructure.cache.CacheTest2.txTest(CacheTest2.java:35)
    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:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    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)
    Caused by: java.lang.InstantiationException: com.google.common.collect.Lists$TransformingRandomAccessList
    at java.lang.Class.newInstance(Class.java:427)
    at com.alibaba.fastjson2.reader.ObjectReaderImplList.readJSONBObject(ObjectReaderImplList.java:349)
    ... 27 more
    Caused by: java.lang.NoSuchMethodException: com.google.common.collect.Lists$TransformingRandomAccessList.()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 28 more

Process finished with exit code -1
` 错误

        CacheObject cacheObject = new CacheObject();
        cacheObject.setId("11");
        CacheObject cacheObject2 = new CacheObject();
        cacheObject2.setId("22");
        List<CacheObject> cacheObjects = Arrays.asList(cacheObject, cacheObject2);

        List<String> transform = com.google.common.collect.Lists.transform(cacheObjects, CacheObject::getId);

        CacheObject cacheObject3 = new CacheObject();
        cacheObject3.setListEmCache(Arrays.asList(EnumTest.EM1, EnumTest.EM2));
        cacheObject3.setStrlistCache(transform);

        byte[] bytes = JSONB.toBytes(cacheObject3, JSONWriter.Feature.WriteClassName);

        CacheObject jsonObject = (CacheObject) JSONB.parseObject(bytes, Object.class, JSONReader.Feature.SupportAutoType, JSONReader.Feature.SupportClassForName);

        System.out.println(jsonObject);
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.List;

public class CacheObject {

    private String id;

    private long version;
    private List<Object> listCache;
    private List<String> strlistCache;

    private List<EnumTest> listEmCache;

    public List<Object> getListCache() {
        return listCache;
    }

    public void setListCache(List<Object> listCache) {
        this.listCache = listCache;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the version
     */
    public long getVersion() {
        return version;
    }

    /**
     * @param version the version to set
     */
    public void setVersion(long version) {
        this.version = version;
    }


    @Override
    public String toString() {
        ToStringBuilder builder = new ToStringBuilder(this);
        builder.append("id", id);
        builder.append("version", version);
        return builder.toString();
    }

    public List<EnumTest> getListEmCache() {
        return listEmCache;
    }

    public void setListEmCache(List<EnumTest> listEmCache) {
        this.listEmCache = listEmCache;
    }

    public List<String> getStrlistCache() {
        return strlistCache;
    }

    public void setStrlistCache(List<String> strlistCache) {
        this.strlistCache = strlistCache;
    }
}

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。

相关日志输出

请复制并粘贴任何相关的日志输出。

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

@dyj2012 dyj2012 added the bug Something isn't working label Jun 2, 2022
@dyj2012
Copy link
Author

dyj2012 commented Jun 2, 2022

建议 FieldReaderListStrMethod 类中 的方法

listType == AbstractList.class 改为 AbstractList.class.isAssignableFrom(listType) 可以适配更多的List数组类型

@wenshao wenshao added this to the 2.0.6 milestone Jun 2, 2022
@wenshao wenshao added the jsonb label Jun 2, 2022
@wenshao
Copy link
Member

wenshao commented Jun 2, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.6-SNAPSHOT/

问题已经修复,请帮忙用2.0.6-SNAPSHOT版本验证,2.0.6正式版预计6月5日前发布

@wenshao
Copy link
Member

wenshao commented Jun 4, 2022

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

@wenshao wenshao closed this as completed Jun 4, 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 jsonb
Projects
None yet
Development

No branches or pull requests

2 participants