[alibaba/fastjson]实体类中出现与typeKey相同的字段时,反序列化无法正确选择子类

2024-09-20 592 views
0

使用的版本是 1.2.73。使用了@JSONType.seeAlso和@JSONType.typeKey功能。

父类的@JSONType.typeKey设置为type,当Animal中出现type时,反序列化返回null,没有type字段则可以正常反序列化。(请参照测试代码)

看了以下两个文档的说明: https://github.com/alibaba/fastjson/wiki/JSONType_seeAlso_cn https://github.com/alibaba/fastjson/wiki/JSONType_typeKey_cn

没有发现字段不能与typeKey相同的说明。不知道这是不是个问题?

下面是使用的测试代码

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONType;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class FastJSONTest {

    @JSONType(seeAlso = {Dog.class, Cat.class}, typeKey = "type")
    public static abstract class Animal {
// 打开注释部分代码,无法正确反序列化
//        private String type;
//
//        public String getType() {
//            return type;
//        }
//
//        public void setType(String type) {
//            this.type = type;
//        }
    }

    @JSONType(typeName = "dog")
    public static class Dog extends Animal {
        private String dogName;

        public String getDogName() {
            return dogName;
        }

        public void setDogName(String dogName) {
            this.dogName = dogName;
        }

        @Override
        public String toString() {
            return "Dog{" +
                    "dogName='" + dogName + '\'' +
                    '}';
        }
    }

    @JSONType(typeName = "cat")
    public static class Cat extends Animal {
        private String catName;

        public String getCatName() {
            return catName;
        }

        public void setCatName(String catName) {
            this.catName = catName;
        }

        @Override
        public String toString() {
            return "Cat{" +
                    "catName='" + catName + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.dogName = "dog1001";

        String text = JSON.toJSONString(dog, SerializerFeature.WriteClassName);
        System.out.println(text);

        Dog dog2 = (Dog) JSON.parseObject(text, Animal.class);

        System.out.println(dog2);
    }

}

存在type字段的返回结果:

{"type":"dog","dogName":"dog1001"}
null

不存在type字段的结果:

{"type":"dog","dogName":"dog1001"}
Dog{dogName='dog1001'}

回答

7

@sumy7 Is there anyone had done something in this issue? My group are willing to solve it in a few months.

3
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONType;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;
import org.junit.Test;

public class Issue3479 {

    @JSONType(seeAlso = {Dog.class, Cat.class}, typeKey = "type")
    //open `@Data` in nextline, the 2nd of output will be `null`
    //@Data
    public static abstract class Animal {
        private String type;
    }

    @JSONType(typeName = "dog")
    @Data
    public static class Dog extends Animal {
        private String dogName;
    }

    @JSONType(typeName = "cat")
    @Data
    public static class Cat extends Animal {
        private String catName;
    }

    @Test
    public void test1() {
        Dog dog = new Dog();
        dog.dogName = "dog1001";

        String text = JSON.toJSONString(dog, SerializerFeature.WriteClassName);
        System.out.println(text);

        Dog dog2 = (Dog) JSON.parseObject(text, Animal.class);

        System.out.println(dog2);
    }
}

1.2.75 仍然有效

7

@HuaHuay

7

@sumy7 Is there anyone had done something in this issue? My group are willing to solve it in a few months.

@Certseeds Noop, got no word back. 没有,之后没有收到过什么反馈了。