Home > Java > Is there a way to (de)serialize Java enums with YAML?

Is there a way to (de)serialize Java enums with YAML?

WBOY
Release: 2024-02-08 22:15:18
forward
972 people have browsed it

php editor Strawberry answered: Yes, there is a way to serialize and deserialize Java enumerations and YAML. Java enumerations can be serialized and deserialized using Jackson, Gson, or other JSON libraries. YAML is a human-friendly data format that can also be used to serialize and deserialize Java enums via Jackson, SnakeYAML, or other YAML libraries. Using these libraries, you can easily convert Java enumerations to YAML for data transmission and storage.

Question content

I'm trying to serialize and deserialize a Java object containing an enum to/from a YAML representation. I'm trying to use the Jackson (com.fasterxml.jackson) package to do this.

I keep getting this exception:

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]
Copy after login

I'm assuming I need some kind of adapter or converter, but don't know what's available for YAML (if any).

for reference:

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
}
Copy after login

data.yaml:

---
name: gitlab_pipeline_success_total
description: "blah blah blah"
type: counter
labels:
  - project
  - somethingElse
data:
Copy after login

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);
  }

}
Copy after login

Solution

By default, Jackson uses the valueOf method of the enumeration. case sensitive.

You can easily change this by adding a static method annotated with @JsonCreator:

public class Metric {

    public enum Type {
        COUNTER, GAUGE;

        @JsonCreator
        public static Type getType(String value) {
            return valueOf(value.toUpperCase());
        }

        // optional, for serialization
        @JsonValue
        public String value() {
            return name().toLowerCase();
        }
    }

    // rest omitted
}
Copy after login

The above is the detailed content of Is there a way to (de)serialize Java enums with YAML?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template