Table of Contents
Question content
Solution
Home Java Is there a way to (de)serialize Java enums with YAML?

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

Feb 08, 2024 pm 10:15 PM

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!

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)