简体   繁体   English

使用 Jackson 在 Java 中读取 yaml 的一部分

[英]Read part of yaml in Java using Jackson

if I have the following yaml (which I found online) representing a java Order class, order.yaml :如果我有以下代表 java Order类的 yaml(我在网上找到的), order.yaml

orderNo: A001
customerName: Customer, Joe
orderLines:
  - item: No. 9 Sprockets
    quantity: 12
    unitPrice: 1.23
  - item: Widget (10mm)
    quantity: 4
    unitPrice: 3.45

I was able to use我能够使用

ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
Order order = objectMapper.readValue(new File(<path_to_order>), Order.class);

But this means that I need to define orderNo and orderLines in advance... If I have a giant yaml with a bunch of nested properties this can get really annoying.但这意味着我需要提前定义orderNoorderLines ...如果我有一个带有一堆嵌套属性的巨大 yaml,这可能会非常烦人。 What if I want a class than can read one property or a class that can read another property and "ignore" other ones?如果我想要一个可以读取一个属性的类或一个可以读取另一个属性并“忽略”其他属性的类怎么办? Is that even possible?这甚至可能吗? That way I could just specify which java object I want without necessarily having to recursively define every property of the yaml.这样我就可以指定我想要的 java 对象,而不必递归定义 yaml 的每个属性。 Thank you!谢谢!

The Map approach will lose you the type safety. Map 方法会让你失去类型安全。 There's no need to define every single property.无需定义每个属性。 You can use the Json annotations just fine with YAML too, it's just a historical leftover that it is called Json.你也可以在 YAML 中使用 Json 注释,它只是一个历史遗留物,它被称为 Json。 What you are looking for is @JsonIgnoreProperties(ignoreUnknown = true) .您正在寻找的是@JsonIgnoreProperties(ignoreUnknown = true)

If you don't like to specify the Annotation for every class, use objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);如果您不喜欢为每个类指定 Annotation,请使用objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); . .

public static void main(String[] args) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
    Order order = objectMapper.readValue(new File("foo.yml"), Order.class);
    System.out.println(order.getOrderLines().get(0).getItem());
}


@JsonIgnoreProperties(ignoreUnknown = true)
static class Order {
    private String orderNo;
    private List<OrderLine> orderLines;

    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public List<OrderLine> getOrderLines() {
        return orderLines;
    }

    public void setOrderLines(List<OrderLine> orderLines) {
        this.orderLines = orderLines;
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    static class OrderLine {
        private String item;

        public String getItem() {
            return item;
        }

        public void setItem(String item) {
            this.item = item;
        }
    }
}

You can read the json in a Map and then retrieve whatever you want from there您可以在 Map 中读取 json,然后从那里检索您想要的任何内容

ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
Map<String,Object> jsonMap = objectMapper.readValue(new File(<path_to_order>), Map.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM