我正在尝试序列化和反序列化一个Java对象,该对象包含到YAML表示的枚举。我想用杰克逊(
com.fasterxml.jackson
)包来完成此操作。
我一直得到这个例外:
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `com.beastcode.devops.prometheusbroker.domain.Metric$Type` from String "counter": not one of the values accepted for Enum class: [GAUGE, COUNTER]
我想我需要某种适配器或转换器,但不知道YAML有什么可用的(如果有的话)。
供参考:
Metric.java:
public class Metric {
public enum Type {
COUNTER, GAUGE
}
private String name;
private String description;
private Type type;
private List<String> labels;
private List<MetricData> data;
// getters/setters removed
}
data.yaml:
---
name: gitlab_pipeline_success_total
description: "blah blah blah"
type: counter
labels:
- project
- somethingElse
data:
Parser.java:
public class Parser {
private ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
public void parse() throws StreamReadException, DatabindException, IOException {
Metric m = mapper.readValue(new File("data.yaml"), Metric.class);
}
}