6

我想加载这个文件(test.yml):

Car1:
    countriesSold: [Spain, France, Italy]
    model: Seat
    specs: {color: black, height: 29, with: 29}
Car2:
    countriesSold: [Germany, France]
    model: BMW
    specs: {color: white, height: 11, with: 33}

到一个Map<String, Car>.

我在 SnaleYAML 中看到的所有示例总是将 Map 对象放入另一个对象中。

当我尝试像这样加载我的文件时

Map<String, Car> load = 
(Map<String, Car>) yaml.load(new ClassPathResource("test.yml").getInputStream());

它被加载到地图的地图中,里面的一切都是地图的字符串。

我想我需要提供我的内部类型的Constructor对象和对象,但我不知道该怎么做。像这样的东西,但我不确定要为根元素放置什么,即 Map:TypeDescriptionYaml

TypeDescription mapDesc = new TypeDescription(Map.class);
mapDesc.putMapPropertyType("???", String.class, Car.class);
Constructor constructor = new Constructor(mapDesc);

Car 类如下所示:

public class Car {

    private String model;
    private Specs specs;
    private List<String> countriesSold = new ArrayList<>();

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public Specs getSpecs() {
        return specs;
    }

    public void setSpecs(Specs specs) {
        this.specs = specs;
    }

    public List<String> getCountriesSold() {
        return countriesSold;
    }

    public void setCountriesSold(List<String> countriesSold) {
        this.countriesSold = countriesSold;
    }
}

Specs 类如下所示:

public class Specs {

    private int height;
    private int with;

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWith() {
        return with;
    }

    public void setWith(int with) {
        this.with = with;
    }
}
4

0 回答 0