NutzCN Logo
问答 升级63之后json解析无法读取泛型
发布于 18天前 作者 Kerbores 103 次浏览 复制 上一个帖子 下一个帖子
标签:
 public static class A<T> {

        T data;

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

    }

    public static class B {
        int id;
        String name;
        Date birth;
        /**
         * @param id
         * @param name
         * @param birth
         */
        public B(int id, String name, Date birth) {
            super();
            this.id = id;
            this.name = name;
            this.birth = birth;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Date getBirth() {
            return birth;
        }
        public void setBirth(Date birth) {
            this.birth = birth;
        }
        
        
    }

    @Test
    public void test() {
        A a = new A();
        a.setData(new B(1, "test", Times.now()));
        String s = Json.toJson(a);
        A<B> a1 = Json.fromJson(A.class, s);
        assertEquals(B.class, a1.getData().getClass());
    }

执行报错

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to org.nutz.json.GenericTypeTest$B
	at org.nutz.json.GenericTypeTest.test(GenericTypeTest.java:73)
	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:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)


13 回复

以前可以的?

yes 62的时候是可以的,看了源码也没有动这个部分

我觉得fromJso那段不行吧,B的信息怎能推导到

我试一下1.r.62,一样的错误的

很奇怪的是换62就支持

你看错了吧, 我试了试, 即使是1.r.62, 也需要这样写

    @Test
    public void test() {
        A a = new A();
        a.setData(new B(1, "test", Times.now()));
        String s = Json.toJson(a);
        A<B> a1 = (A<B>) Json.fromJson(new NutType(A.class, B.class), s);
        assertEquals(B.class, a1.getData().getClass());
    }

直接传A.class, 泛型B怎么可能拿得到... 怎么知道不是C不是D呢...

找到了62的NutzJsonMessageConverter实现和63的不一样

对原有的代码的修改:

首先, B添加无参数构造方法
然后, 添加test_2用例

test不能pass, test_2可以

我已经提交https://github.com/nutzam/nutzmore/commit/a22eaafc0b500363ac79dd530206ab06549f6d3c,但是还没有测试,看了下集成的类不一样,有一个是可以拿到type的只要拿到type就好说

哦, 原来是nutzmore的问题... 我说呢...

添加回复
请先登陆
回到顶部