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

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]

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
}

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

}

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
}

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!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)