Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
How to parse part of .yaml file to object in SnakeYAML?

I have a yaml file, for example:

# this is the part I don't care about
config:
  key-1: val-1
other-config:
  lang: en
  year: 1906
# below is the only part I care about
interesting-setup:
  port: 1234
  validation: false
  parts:
    - on-start: backup
      on-stop: say-goodbye

Also I have a POJO class that is suitable for the interesting-setup part

public class InterestingSetup {
    int port;
    boolean validation;
    List<Map<String, String>> parts;
}

I want to load just the interesting-setup part (similarly as @ConfigurationProperties("interesting-setup") in Spring)

Currently I'm doing it like this:

Map<String, Object> yamlConfig = yaml.load(yamlFile);            # loading the whole file to Map with Object values
Object interestingObject = yamlConfig.get("interesting-setup");  # loading 'interesting-setup' part as an object
Map<String, Object> interestingMap = (Map<String, Object>);      # Casting object to Map<String, Object>
String yamlDumped = yaml.dump(interestingMap);                   # Serialization to String
InterestingSetup finalObject = yaml.load(yamlDumped);            # Getting final object from String

The crucial part is when I have an Object (Map<String, Object>) and want to cast it to my final class. To do that - I need to serialize it to String, so the process looks like this:

File -> Map<String, Object> -> Object -> Map<String, Object> -> String -> FinalClass and I'd like to avoid deserialization and again serialization of the same data.

So can I somehow use Yaml to map the Map<String, Object> to another class? I cannot see this in an API?

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda