use of com.fasterxml.jackson.dataformat.yaml.YAMLParser in project polaris-java by polarismesh.
the class ConfigAPIFactory method loadConfig.
/**
* 通过配置文件加载配置对象
*
* @param configStream 配置文件流
* @return 配置对象
* @throws PolarisException 文件加载异常
*/
public static Configuration loadConfig(InputStream configStream) throws PolarisException {
YAMLFactory yamlFactory = new YAMLFactory();
ObjectMapper mapper = new ObjectMapper();
try {
YAMLParser yamlParser = yamlFactory.createParser(configStream);
final JsonNode node = mapper.readTree(yamlParser);
TreeTraversingParser treeTraversingParser = new TreeTraversingParser(node);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper.readValue(treeTraversingParser, ConfigurationImpl.class);
} catch (Exception e) {
throw new PolarisException(ErrorCode.INVALID_CONFIG, "fail to load config from stream", e);
}
}
use of com.fasterxml.jackson.dataformat.yaml.YAMLParser in project atlas by osmlab.
the class StandardConfiguration method readConfigurationMapFromYAML.
@SuppressWarnings("unchecked")
private Optional<Map<String, Object>> readConfigurationMapFromYAML(final byte[] readBytes) {
final ByteArrayInputStream read = new ByteArrayInputStream(readBytes);
logger.info("Attempting to load configuration as YAML.");
try {
final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
final SimpleModule simpleModule = new SimpleModule();
simpleModule.addDeserializer(Map.class, new ConfigurationDeserializer());
objectMapper.registerModule(simpleModule);
final YAMLParser parser = new YAMLFactory().createParser(read);
final Map<String, Object> readConfig = objectMapper.readValue(parser, Map.class);
logger.info("Success! Loaded YAML configuration.");
return Optional.of(readConfig);
} catch (final Exception yamlReadException) {
logger.warn("Unable to parse config file as YAML");
return Optional.empty();
} finally {
IOUtils.closeQuietly(read);
}
}
use of com.fasterxml.jackson.dataformat.yaml.YAMLParser in project strimzi by strimzi.
the class TestUtils method configMapFromYaml.
public static ConfigMap configMapFromYaml(String yamlPath, String name) {
try {
YAMLFactory yaml = new YAMLFactory();
ObjectMapper mapper = new ObjectMapper(yaml);
YAMLParser yamlParser = yaml.createParser(new File(yamlPath));
List<ConfigMap> list = mapper.readValues(yamlParser, new TypeReference<ConfigMap>() {
}).readAll();
Optional<ConfigMap> cmOpt = list.stream().filter(cm -> "ConfigMap".equals(cm.getKind()) && name.equals(cm.getMetadata().getName())).findFirst();
if (cmOpt.isPresent()) {
return cmOpt.get();
} else {
LOGGER.warn("ConfigMap {} not found in file {}", name, yamlPath);
return null;
}
} catch (InvalidFormatException e) {
throw new IllegalArgumentException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.fasterxml.jackson.dataformat.yaml.YAMLParser in project strimzi-kafka-operator by strimzi.
the class TestUtils method configMapFromYaml.
public static ConfigMap configMapFromYaml(String yamlPath, String name) {
try {
YAMLFactory yaml = new YAMLFactory();
ObjectMapper mapper = new ObjectMapper(yaml);
YAMLParser yamlParser = yaml.createParser(new File(yamlPath));
List<ConfigMap> list = mapper.readValues(yamlParser, new TypeReference<ConfigMap>() {
}).readAll();
Optional<ConfigMap> cmOpt = list.stream().filter(cm -> "ConfigMap".equals(cm.getKind()) && name.equals(cm.getMetadata().getName())).findFirst();
if (cmOpt.isPresent()) {
return cmOpt.get();
} else {
LOGGER.warn("ConfigMap {} not found in file {}", name, yamlPath);
return null;
}
} catch (InvalidFormatException e) {
throw new IllegalArgumentException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.fasterxml.jackson.dataformat.yaml.YAMLParser in project pancm_project by xuwujing.
the class TransferUtils method yml2Properties.
public static Map<String, String> yml2Properties(String path) {
final String DOT = ".";
Map<String, String> map = new HashMap<>();
try {
YAMLFactory yamlFactory = new YAMLFactory();
YAMLParser parser = yamlFactory.createParser(new InputStreamReader(new FileInputStream(path), Charset.forName(ENCODING)));
String key = "";
String value = null;
JsonToken token = parser.nextToken();
while (token != null) {
if (JsonToken.START_OBJECT.equals(token)) {
// do nothing
} else if (JsonToken.FIELD_NAME.equals(token)) {
if (key.length() > 0) {
key = key + DOT;
}
key = key + parser.getCurrentName();
token = parser.nextToken();
if (JsonToken.START_OBJECT.equals(token)) {
continue;
}
value = parser.getText();
map.put(key, value);
int dotOffset = key.lastIndexOf(DOT);
if (dotOffset > 0) {
key = key.substring(0, dotOffset);
}
value = null;
} else if (JsonToken.END_OBJECT.equals(token)) {
int dotOffset = key.lastIndexOf(DOT);
if (dotOffset > 0) {
key = key.substring(0, dotOffset);
} else {
key = "";
}
}
token = parser.nextToken();
}
parser.close();
System.out.println(map);
} catch (Exception e) {
throw new RuntimeException(e);
}
return map;
}
Aggregations