3
我希望加載此文件(test.yml):如何使用SnakeYAML將yaml直接載入地圖<String, Car>?
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
對象和TypeDescription
我的內部類型的對象Yaml
,但我不知道該怎麼做。 這樣的事情,但我不知道要放什麼東西的根元素,這是地圖:
TypeDescription mapDesc = new TypeDescription(Map.class);
mapDesc.putMapPropertyType("???", String.class, Car.class);
Constructor constructor = new Constructor(mapDesc);
汽車類看起來是這樣的:
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;
}
}
而且規格類看起來像這樣:
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;
}
}
[使用snakeYaml在根上解析一個帶有映射的YAML文檔]的可能的副本(http://stackoverflow.com/questions/28551081/parsing-a-yaml-document-with-a-map-at-the -root-使用-snakeyaml) – Josef 2016-11-29 11:43:16