将YAML反序列化为JAVA

时间:2019-05-23 09:33:47

标签: java jackson yaml snakeyaml

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

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

1。首先,我创建了一个名为Connector的类,其中定义了所有变量,例如ProviderNodesmaxconnectionsMinconnections。 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();
            }
        }

1 个答案:

答案 0 :(得分:0)

在使用Jackson时,可以将ObjectMapperYAMLFactory一起使用。在后台,它将使用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);