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

ObjectReaderImplMap.of方法生成ReadObject时,希望mapType可以支持非public修饰的类 #557

Closed
FanJiaRui opened this issue Jul 13, 2022 · 6 comments
Labels
bug Something isn't working enhancement New feature or request
Milestone

Comments

@FanJiaRui
Copy link

请描述您的需求或者改进建议

ObjectReaderImplMap.of方法生成ReadObject时,希望mapType可以支持非public修饰的类
例如:

private static class MyMap implements Map{ //..... }

ObjectReaderImplMap.of(new TypeReference<Map<String, Object>>() {}.getType(), MyMap.class, 0);
现在这么用会在运行到实例化Map时报错,问题出在ObjectReaderImplMapTyped.createInstance方法中

请描述你建议的实现方案

建议将ObjectReaderImplMapTyped.createInstance方法中instanceType.newInstance();实现修改为
try { Constructor<?> constructor = instanceType.getConstructor(); constructor.setAccessible(true); constructor.newInstance(); } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { throw new JSONException("create map error"); }

@FanJiaRui FanJiaRui added the enhancement New feature or request label Jul 13, 2022
@wenshao
Copy link
Member

wenshao commented Jul 14, 2022

能够提供重现问题的TestCase么?

@FanJiaRui
Copy link
Author

FanJiaRui commented Jul 14, 2022

能够提供重现问题的TestCase么?
你好,代码如下

package unit.test1;

import com.alibaba.fastjson2.JSONFactory;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.alibaba.fastjson2.reader.ObjectReader;
import com.alibaba.fastjson2.reader.ObjectReaderImplMap;

import java.util.HashMap;
import java.util.Map;

public class TestUtils {

    static {
        ObjectReader objectReader1 = ObjectReaderImplMap.of(new TypeReference<Map<String, Object>>() {
        }.getType(), TestPublicMap.class, 0);
        JSONFactory.getDefaultObjectReaderProvider().register(TestPublicMap.class, objectReader1);

        ObjectReader objectReader2 = ObjectReaderImplMap.of(new TypeReference<Map<String, Object>>() {
        }.getType(), TestPrivateMap.class, 0);
        JSONFactory.getDefaultObjectReaderProvider().register(TestPrivateMap.class, objectReader2);
    }

    public static void main(String[] args) {
        Map<String, Object> a = JSONObject.parseObject("{'a':'b'}").to(TestPublicMap.class);
        System.out.println(a);

        Map<String, Object> b = JSONObject.parseObject("{'a':'b'}").to(TestPrivateMap.class);
        System.out.println(a);
    }

    public static class TestPublicMap extends HashMap<String, Object>  {

    }

    private static class TestPrivateMap extends HashMap<String, Object>  {

    }

}

@FanJiaRui
Copy link
Author

我用的是Oracel JDK 1.8.0_201 和fastjson 2.0.9,补充一下运行结果相关截图
099612FC-710B-40e3-87B6-28DF6506505A
9BF313C8-C406-4432-A53D-C37CC5535FAF

@wenshao
Copy link
Member

wenshao commented Jul 14, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.10-SNAPSHOT/
问题已经修复,请用2.0.10-SNAPSHOT版本验证,2.0.10正式版预计在7月24日前发布

@wenshao wenshao added this to the 2.0.10 milestone Jul 14, 2022
@wenshao wenshao added the bug Something isn't working label Jul 14, 2022
@FanJiaRui
Copy link
Author

感谢支持,已经验证无问题,期待新版本的发布

@wenshao
Copy link
Member

wenshao commented Jul 23, 2022

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

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

No branches or pull requests

2 participants