javaspring-bootyamlapache-commons

Parse list of object from external yaml file


I have an external YAML file that contains only a list of objects. I need to read this file every time the loadVerificatedEmailsConfiguration method is called. I'm trying to solve this problem using the Apache Commons Configuration2 library. Unfortunately, I'm only able to load individual attributes from the file, such as verificated-emails.email, but not individual objects as a whole. I need this because some attributes are optional.

YAML file:

verificated-emails:
  - { email: xx.mila@xxx.cz, firstname: Dummy, surname: xxxx }
  - { email: bob.xx@xxx.cz, firstname: Bob, surname: xxx }
  - { email: petr.dusan@xxx.cz, surname: Dusan }
  - { email: tomas.xxx@xxx.cz, firstname: tomas, surname: xxx }

loadVerificatedEmailsConfiguration method

 public List<VerificatedEmailsConfiguration> loadVerificatedEmailsConfiguration() throws ConfigurationException, IOException, org.apache.commons.configuration2.ex.ConfigurationException {
            log.info("start loadVerificatedEmailsConfiguration");
            YAMLConfiguration yamlConfiguration = new YAMLConfiguration();
            if(VERIFICATED_EMAIL_CONFIG == null) {
                throw new ConfigurationException("FilePath is null");
            }
            List<VerificatedEmailsConfiguration> verificatedEmailsConfigurationList = new ArrayList<>();
            InputStream inputStream = new FileInputStream(VERIFICATED_EMAIL_CONFIG);
            yamlConfiguration.read(inputStream);
            var verifyEmails = yamlConfiguration.getProperty("verificated-emails");
            log.info("verifyEmails: {}", verifyEmails);
    
            return verificatedEmailsConfigurationList;
        }

VerificatedEmailsConfiguration model

@Data
public class VerificatedEmailsConfiguration {
    private String email;
    private String firstname;
    private String surname;
}

Logs

2024-05-10T13:23:59.145+02:00  INFO 17340 --- [mail] [           main] c.e.m.c.ExternalConfiguration            : start loadVerificatedEmailsConfiguration
2024-05-10T13:23:59.192+02:00  INFO 17340 --- [mail] [           main] c.e.m.c.ExternalConfiguration            : verifyEmails: null

Solution

  • Use configurationsAt method which returns a list of List<HierarchicalConfiguration<ImmutableNode>> you can then iterate over that list to get the entries.

    public List<VerificatedEmailsConfiguration> loadVerificatedEmailsConfiguration() throws ConfigurationException, IOException {
    
        YAMLConfiguration yamlConfiguration = new YAMLConfiguration();
        List<VerificatedEmailsConfiguration> verificatedEmailsConfigurationList = new ArrayList<>();
        InputStream inputStream = new FileInputStream(VERIFICATED_EMAIL_CONFIG);
        yamlConfiguration.read(inputStream);
        final List<HierarchicalConfiguration<ImmutableNode>> verifiedEmails = yamlConfiguration.configurationsAt("verificated-emails");
    
        for(var entry : verifiedEmails) {
            VerificatedEmailsConfiguration configuration = new VerificatedEmailsConfiguration();
            configuration.email = entry.getString("email");
            configuration.firstname = entry.getString("firstname");
            configuration.surname = entry.getString("surname");
            verificatedEmailsConfigurationList.add(configuration);
        }
        return verificatedEmailsConfigurationList;
    }
    

    p.s I'm not a native english speaker but I think they say verified instead of verificated