用 snakeyaml 解析 beans 列表
Parse list of beans with snakeyaml
是否可以用snakeyaml
解析以下内容并获得List<Radio>
(其中Radio
是合适的java bean)?
-
id: chaine416
name: 'France Inter'
type: music
-
id: chaine417
name: 'France Culture'
type: music
-
id: chaine418
name: 'Couleur 3'
type: music
new Yaml().load(...);
returns 一个 List<HashMap>
,但我想要一个 List<Radio>
。
我知道的唯一方法是使用顶级对象来处理集合。
Yaml 文件:
---
stations:
-
id: chaine416
name: "France Inter"
type: music
-
id: chaine417
name: "France Culture"
type: music
-
id: chaine418
name: "Couleur 3"
type: music
我刚刚添加了“---”、新文档和属性 stations.
然后:
package snakeyaml;
import java.util.ArrayList;
public class Radios {
ArrayList<RadioStation> stations = new ArrayList<RadioStation>();
public ArrayList<RadioStation> getStations() {
return stations;
}
public void setStations(ArrayList<RadioStation> stations) {
this.stations = stations;
}
}
class电台:
package snakeyaml;
public class RadioStation {
String id;
String name;
String type;
public RadioStation(){
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "RadioStation{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", type='" + type + '\'' +
'}';
}
}
并读取 YAML 文件:
package snakeyaml;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Test {
public static void main(String[] args) {
Yaml yaml = new Yaml(new Constructor(Radios.class));
try {
Radios result = (Radios) yaml.load(new FileInputStream("/home/ofe/dev/projets/projets_non_byo/TachesInfoengine/src/snakeyaml/data.yaml"));
for (RadioStation radioStation : result.getStations()) {
System.out.println("radioStation = " + radioStation);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
实际上,Yaml 中有一个方法 loadAll returns Iterable 但是 yaml 文件应该在列表项之间有一个分隔符“---”。
该文件应如下所示:
id: chaine416
name: 'Fr
ance Inter'
type: music
---
id: chaine417
name: 'France Culture'
type: music
---
id: chaine418
name: 'Couleur 3'
type: music
(省略了 Radio DTO)代码应该是这样的:
public class Test {
public static void main(String[] args) {
Yaml yaml = new Yaml(new Constructor(Radio.class));
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("yourFile.yaml");
List<Radio> radios = StreamSupport.stream(yaml.loadAll(inputStream).spliterator(), false)
.filter(Radio.class::isInstance)
.map(Radio.class::cast)
.collect(Collectors.toList());
}
}
是否可以用snakeyaml
解析以下内容并获得List<Radio>
(其中Radio
是合适的java bean)?
-
id: chaine416
name: 'France Inter'
type: music
-
id: chaine417
name: 'France Culture'
type: music
-
id: chaine418
name: 'Couleur 3'
type: music
new Yaml().load(...);
returns 一个 List<HashMap>
,但我想要一个 List<Radio>
。
我知道的唯一方法是使用顶级对象来处理集合。
Yaml 文件:
---
stations:
-
id: chaine416
name: "France Inter"
type: music
-
id: chaine417
name: "France Culture"
type: music
-
id: chaine418
name: "Couleur 3"
type: music
我刚刚添加了“---”、新文档和属性 stations.
然后:
package snakeyaml;
import java.util.ArrayList;
public class Radios {
ArrayList<RadioStation> stations = new ArrayList<RadioStation>();
public ArrayList<RadioStation> getStations() {
return stations;
}
public void setStations(ArrayList<RadioStation> stations) {
this.stations = stations;
}
}
class电台:
package snakeyaml;
public class RadioStation {
String id;
String name;
String type;
public RadioStation(){
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "RadioStation{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", type='" + type + '\'' +
'}';
}
}
并读取 YAML 文件:
package snakeyaml;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Test {
public static void main(String[] args) {
Yaml yaml = new Yaml(new Constructor(Radios.class));
try {
Radios result = (Radios) yaml.load(new FileInputStream("/home/ofe/dev/projets/projets_non_byo/TachesInfoengine/src/snakeyaml/data.yaml"));
for (RadioStation radioStation : result.getStations()) {
System.out.println("radioStation = " + radioStation);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
实际上,Yaml 中有一个方法 loadAll returns Iterable 但是 yaml 文件应该在列表项之间有一个分隔符“---”。 该文件应如下所示:
id: chaine416
name: 'Fr
ance Inter'
type: music
---
id: chaine417
name: 'France Culture'
type: music
---
id: chaine418
name: 'Couleur 3'
type: music
(省略了 Radio DTO)代码应该是这样的:
public class Test {
public static void main(String[] args) {
Yaml yaml = new Yaml(new Constructor(Radio.class));
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("yourFile.yaml");
List<Radio> radios = StreamSupport.stream(yaml.loadAll(inputStream).spliterator(), false)
.filter(Radio.class::isInstance)
.map(Radio.class::cast)
.collect(Collectors.toList());
}
}