我想加载这个文件(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());
它被加载到Map的Map中,里面的所有内容都是字符串。
我想我需要提供Constructor
内部类型的TypeDescription
个对象和Yaml
个对象TypeDescription mapDesc = new TypeDescription(Map.class);
mapDesc.putMapPropertyType("???", String.class, Car.class);
Constructor constructor = new Constructor(mapDesc);
,但我不知道该怎么做。
像这样的东西,但是我不确定要为根元素放什么,这就是Map:
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;
}
}
Car类看起来像这样:
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;
}
}
Specs类看起来像这样:
{{1}}