是否可以将引用类型动态传递给需要引用类型的对象实例?

时间:2019-02-28 15:20:20

标签: java jackson serializer

例如:

public void builCarManager(Car car) {
   Manager<car.getClass()> carManager = new Manager<>();
}

有人建议解决这个问题?

编辑

我的主要问题在于:

public class CustomSerializer<T> extends StdSerializer<T> {

    @Override
    public void serialize(T car, JsonGenerator gen, SerializerProvider sp) {
        // I get car.getClass() fine but I need add it
        Manager<T> carManager = new Manager<>();
        // Manager<car.getClass()> carManager = new Manager<>();
    }
}

在这里,我无法通过CustomSerializer<Lamborgini>.class

@JsonSerialize(using = CustomSerializer.class)
private Lamborgini car; 

3 个答案:

答案 0 :(得分:1)

处理此问题的正确方法是让两个类(例如CarDesk)实现一个公共接口(例如Item)并将该接口与{{1} }类。

Manager

输出

public class Instance {
    private static interface Item {
        String getName();
    }

    private static class Car implements Item {
        @Override
        public String getName() {
            return "Car";
        }
    }

    private static class Desk implements Item {
        @Override
        public String getName() {
            return "Desk";
        }
    }

    private static class Manager<T extends Item> {
        private T item;

        public Manager(T item) {
            this.item = item;
        }

        @Override
        public String toString() {
            return String.format("Manager[item=%s]", item.getName());
        }
    }

    public static <E extends Item> Manager<E> buildItemManager(E item) {
        return new Manager<E>(item);
     }

    public static void main(String[] args) {
        Item car  = new Car(), desk = new Desk();

        Manager<Item> manager1 = buildItemManager(car);
        System.out.println(manager1);

        Manager<Item> manager2 = buildItemManager(desk);
        System.out.println(manager2);
    }
}

另一种方法是...传入代表Manager[item=Car] Manager[item=Desk] 的类来实例化Manager

Item

答案 1 :(得分:1)

假设您的模型低于POJO

class Car {
}

class Lamborghini extends Car {
}

class Ferrari extends Car {
}

您的Manager类如下所示:

class Manager<T extends Car> {

    private Class<T> clazz;

    public Manager(Class<T> clazz) {
        this.clazz = clazz;
    }

    public String generateCustomCarName() {
        if (Lamborghini.class == clazz) {
            return "Lambooooo!";
        } else if (Ferrari.class == clazz) {
            return "Wild horse Ferrari!";
        }

        return "Not for everyone!";
    }

    @Override
    public String toString() {
        return String.format("Manager[clazz=%s]", clazz.getName());
    }
}

现在,让我们创建一个Store,其中有一个Ferrari和一个Lamborghini

class Store {

    private Car ferrari = new Ferrari();
    private Car lamborghini = new Lamborghini();

    public Car getFerrari() {
        return ferrari;
    }

    public void setFerrari(Car ferrari) {
        this.ferrari = ferrari;
    }

    public Car getLamborghini() {
        return lamborghini;
    }

    public void setLamborghini(Car lamborghini) {
        this.lamborghini = lamborghini;
    }
}

现在,我们需要在运行时提供class信息。由于Java的{​​{3}},我们需要提供class

class CustomCarSerializer<T extends Car> extends StdSerializer<T> {

    public CustomCarSerializer(Class<T> clazz) {
        super(clazz);
    }

    @Override
    public void serialize(T value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException {
        Manager<T> manager = new Manager<>(_handledType);
        jgen.writeString(manager.generateCustomCarName());
    }
}

请注意,StdSerializer也具有需要class的构造函数,稍后我们可以使用_handledType来阅读它。现在,我们需要使用SimpleModule使用序列化程序配置实现:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import java.io.IOException;

public class Cars {

    public static void main(String[] args) throws Exception {
        SimpleModule carsModule = new SimpleModule("CarsModule");
        carsModule.addSerializer(Lamborghini.class, new CustomCarSerializer<>(Lamborghini.class));
        carsModule.addSerializer(Ferrari.class, new CustomCarSerializer<>(Ferrari.class));

        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(carsModule);
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        System.out.println(mapper.writeValueAsString(new Store()));
    }
}

上面的代码显示:

{
  "ferrari" : "Wild horse Ferrari!",
  "lamborghini" : "Lambooooo!"
}

答案 2 :(得分:0)

是的,您可以使用泛型。

  public <T> void builCarManager(T car) {
    Manager<T> carManager = new Manager<>();
 }

现在您可以传递任何类型的对象。创建的经理将是同一类型。