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] 使用JSON.parseArray 反序列化非数组json("{....}"), 不论传入Class是什么类型,均转换为List<String> 内部String元素为该json字符串 #3180

Open
BohrX opened this issue Nov 27, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@BohrX
Copy link

BohrX commented Nov 27, 2024

问题描述

使用JSON.parseArray 反序列化非数组json("{....}"), 不论传入Class是什么类型,均转换为List 内部String元素为改json字符串
涉及api:
com.alibaba.fastjson2.JSON#parseArray(java.lang.String, java.lang.Class)

环境信息

  • OS信息: Win11
  • JDK信息: 1.8
  • 版本信息:Fastjson2 2.0.53

重现步骤

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

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.assertj.core.api.Assertions;
import org.junit.Test;

public class JsonArrayParseTest {

    @Test
    public void name() {
        String json = "{\"scene\":\"abc\",\"expireTime\":2880}";
        List<?> ls = com.alibaba.fastjson2.JSON.parseArray(json, Bean.class);
        System.out.println(ls.stream().map(Object::getClass).collect(Collectors.toList()));
        System.out.println(ls.stream().map(Objects::toString).collect(Collectors.toList()));

        Assertions.assertThatThrownBy(() -> com.alibaba.fastjson.JSON.parseArray(json, Bean.class));
    }

    private static class Bean {

        private String scene;

        private int expireTime;

        public String getScene() {
            return scene;
        }

        public void setScene(String scene) {
            this.scene = scene;
        }

        public int getExpireTime() {
            return expireTime;
        }

        public void setExpireTime(int expireTime) {
            this.expireTime = expireTime;
        }
    }
}

期待的正确结果

如FastJson1那样报错。

@BohrX BohrX added the bug Something isn't working label Nov 27, 2024
@BohrX BohrX changed the title [BUG] 使用JSON.parseArray 反序列化非数组json("{....}"), 不论传入Class是什么类型,均转换为List<String> 内部String为该json字符串 [BUG] 使用JSON.parseArray 反序列化非数组json("{....}"), 不论传入Class是什么类型,均转换为List<String> 内部String元素为该json字符串 Nov 27, 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
Projects
None yet
Development

No branches or pull requests

1 participant