gpt4 book ai didi

java - 将 YAML 反序列化为 JAVA

转载 作者:行者123 更新时间:2023-12-02 05:19:45 26 4
gpt4 key购买 nike

我有一个包含以下数据的 YAML 文件,我想将其反序列化为 java 对象。

connector:
provider: ABCD
nodes:
- 1.1.2.1
- 1.2.2.8
maxconnection: 1
minconnection: 1

1.首先,我创建了一个名为 Connector 的类,其中定义了所有变量,如 Provider , Nodes , maxconnections , Minconnections 。2.然后我创建了一个名为group的类并调用Connector类。3.然后在main函数中尝试加载yaml文件并调用它。

但是我遇到了空指针异常错误

#Connector Class
public static class connector {

private String provider;

private String[] nodes;

private int max_connections;

private int min_connections;

public String getprovider() {
return provider;
}
public void setprovider(String provider) {
this.provider = provider;
}
public String[] getnodes() {
return nodes;
}
public void setnodes( String[] nodes) {
this.nodes = nodes;
}
public int getmax_connections() {
return max_connections;
}
public void setmax_connections(int max_connections) {
this.max_connections = max_connections;
}
public int getmin_connections() {
return min_connections;
}
public void setmin_connections(int min_connections) {
this.min_connections = min_connections;
}

@Override
public String toString() {
return "connector: {provider: " + this.provider + ", nodes: " + this.nodes + ",max_connections: " + this.max_connections +",min_connections: " + this.min_connections +"}";
}


#Group class

public static class Group {
private ArrayList<connector> connector;

public ArrayList<connector> getconnector() {
return connector;
}

public void setconnector(ArrayList<connector> connector) {
this.connector = connector;
}
}

#main Class

public static void main(String[] args) {

final URL resource = YamlConf.class.getResource("demo.yaml");
final Constructor peopleContructor = new Constructor(Group.class);
final TypeDescription peopleDescription = new TypeDescription(connector.class);
peopleDescription.putMapPropertyType("connector", connector.class, Object.class);
peopleContructor.addTypeDescription(peopleDescription);

final Yaml yaml = new Yaml(peopleContructor);
try {
final Group group = (Group) yaml.load(resource.openStream());
for (final connector connector : group.getconnector()) {
System.out.println(connector);
}

} catch (IOException e) {
e.printStackTrace();
}
}

最佳答案

当您使用 Jackson 时,您可以使用 ObjectMapper YAMLFactory 。在幕后,它将使用 SnakeYAML 图书馆。所需的依赖项,请参阅 jackson-dataformats-text 项目文档。

您可以按如下方式定义您的类:

@Getter
@Setter
@NoArgsConstructor
public class Content {

private Connector connector;
}
@Getter
@Setter
@NoArgsConstructor
public class Connector {

private String provider;

private List<String> nodes;

@JsonProperty("maxconnection")
private Integer maxConnections;

@JsonProperty("minconnection")
private Integer minConnections;
}

@Getter , @Setter @NoArgsConstructor 注释来自Lombok项目。它们将为您生成 getter、setter 和默认构造函数。如果您不想使用 Lombok,只需实现这些方法即可。

然后读取您的 YAML 文件:

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Content content = mapper.readValue(yamlFile, Content.class);
<小时/>

根据您的需求,您可以考虑 Map<String, Object>保存值:

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
TypeReference<Map<String, Object>> type = new TypeReference<Map<String, Object>>() {};
Map<String, Object> content = mapper.readValue(yamlFile, type);

关于java - 将 YAML 反序列化为 JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56272225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com