Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jackson/YAML:将键值解析为类_Java_Jackson_Yaml_Jackson2 - Fatal编程技术网

Java Jackson/YAML:将键值解析为类

Java Jackson/YAML:将键值解析为类,java,jackson,yaml,jackson2,Java,Jackson,Yaml,Jackson2,在输入yaml文件时,我有以下内容: services: postgresql: image: "postgres:10" redis: image: "redis:4" public class DockerCompose { public static class Service { private String name; private String image; } private List<Se

在输入yaml文件时,我有以下内容:

services:
  postgresql:
    image: "postgres:10"
  redis:
    image: "redis:4"
public class DockerCompose {
    public static class Service {
        private String name;
        private String image;
    }

    private List<Service> services;
}
很容易将
服务
字段解析为
映射


这可能吗?

最简单的选项是添加自定义setter进行反序列化,添加getter进行序列化

public static class DockerCompose {

    public static class Service {
        @JsonIgnore // for serialization
        private String name;
        private String image;
    }

    @JsonProperty("services") // for derserialization
    public void setServicesMap(Map<String, Service> servicesMap){
        for (Map.Entry<String, Service> entry  :servicesMap.entrySet()) {
            entry.getValue().name = entry.getKey();
            services.add(entry.getValue());
        }
    }

    @JsonProperty("services") // for serialization
    public Map<String, Service> getServicesMap() {
        Map<String, Service> map = new HashMap<>();
        for(Service s: services)
            map.put(s.getName(), s);
        return map;
    }

    @JsonIgnore // for serialization
    private List<Service> services = new ArrayList<>();
}
公共静态类DockerCompose{
公共静态类服务{
@JsonIgnore//用于序列化
私有字符串名称;
私有字符串图像;
}
@JsonProperty(“服务”)//用于反序列化
公共void设置服务映射(映射服务映射){
对于(Map.Entry:servicesMap.entrySet()){
entry.getValue().name=entry.getKey();
services.add(entry.getValue());
}
}
@JsonProperty(“服务”)//用于序列化
公共地图getServicesMap(){
Map Map=newhashmap();
服务(s:服务)
put(s.getName(),s);
返回图;
}
@JsonIgnore//用于序列化
private List services=new ArrayList();
}

最简单的选项是添加用于反序列化的自定义setter和用于序列化的getter

public static class DockerCompose {

    public static class Service {
        @JsonIgnore // for serialization
        private String name;
        private String image;
    }

    @JsonProperty("services") // for derserialization
    public void setServicesMap(Map<String, Service> servicesMap){
        for (Map.Entry<String, Service> entry  :servicesMap.entrySet()) {
            entry.getValue().name = entry.getKey();
            services.add(entry.getValue());
        }
    }

    @JsonProperty("services") // for serialization
    public Map<String, Service> getServicesMap() {
        Map<String, Service> map = new HashMap<>();
        for(Service s: services)
            map.put(s.getName(), s);
        return map;
    }

    @JsonIgnore // for serialization
    private List<Service> services = new ArrayList<>();
}
公共静态类DockerCompose{
公共静态类服务{
@JsonIgnore//用于序列化
私有字符串名称;
私有字符串图像;
}
@JsonProperty(“服务”)//用于反序列化
公共void设置服务映射(映射服务映射){
对于(Map.Entry:servicesMap.entrySet()){
entry.getValue().name=entry.getKey();
services.add(entry.getValue());
}
}
@JsonProperty(“服务”)//用于序列化
公共地图getServicesMap(){
Map Map=newhashmap();
服务(s:服务)
put(s.getName(),s);
返回图;
}
@JsonIgnore//用于序列化
private List services=new ArrayList();
}