All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.zhengframework.configuration.parser.YamlConfigurationParser Maven / Gradle / Ivy

There is a newer version: 1.8.0
Show newest version
package com.github.zhengframework.configuration.parser;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.dataformat.javaprop.JavaPropsMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.github.zhengframework.configuration.ex.ConfigurationSourceException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class YamlConfigurationParser implements ConfigurationParser, FileConfigurationParser {

  private JavaPropsMapper propsMapper = new JavaPropsMapper();
  private YAMLMapper yamlMapper = new YAMLMapper();

  @Override
  public Map parse(InputStream inputStream) {
    try {
      JsonNode jsonNode = yamlMapper.readTree(inputStream);
      return propsMapper.writeValueAsMap(jsonNode);
    } catch (IOException e) {
      throw new ConfigurationSourceException("fail load configuration from inputStream", e);
    }
  }

  @Override
  public String[] supportFileTypes() {
    return new String[]{".yml", ".yaml"};
  }

  @Override
  public Map parse(String fileName, InputStream inputStream) {
    checkSupportFileTypes(fileName);
    try {
      JsonNode jsonNode = yamlMapper.readTree(inputStream);
      return propsMapper.writeValueAsMap(jsonNode);
    } catch (IOException e) {
      throw new ConfigurationSourceException("fail load configuration from file: " + fileName, e);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy