Java源码示例:com.fasterxml.jackson.dataformat.yaml.YAMLParser
示例1
@Override
protected Object parse(Resource resource, Class<?> pluginConfigClass)
throws Exception{
InputStream input = new FileInputStream(resource.getFile());
YAMLParser yamlParser = yamlFactory.createParser(input);
final JsonNode node = objectMapper.readTree(yamlParser);
if(node == null){
return pluginConfigClass.newInstance();
}
TreeTraversingParser treeTraversingParser = new TreeTraversingParser(node);
return objectMapper.readValue(treeTraversingParser, pluginConfigClass);
}
示例2
@Override
public Optional<JsonNode> apply(InputStream t) {
try {
YAMLParser parser = yamlFactory.createParser(t);
return Optional.ofNullable(mapper.readTree(parser));
} catch (IOException e) {
throw new RuntimeException("Error reading config data", e);
}
}
示例3
static JsonNode read(String yaml) {
ByteArrayInputStream in = new ByteArrayInputStream(yaml.getBytes());
try {
YAMLParser parser = new YAMLFactory().createParser(in);
return new ObjectMapper().readTree(parser);
} catch (IOException e) {
throw new RuntimeException("Error reading yaml: " + yaml, e);
}
}
示例4
public <T> T readFile(File file, Class<T> type)
throws IOException
{
// TODO use yaml if file path ends with dig or yml, otherwise use json?
try (YAMLParser out = yaml.createParser(new FileInputStream(file))) {
// TODO write to a String first, then write to file. to not create partially-written broken file
return mapper.readValue(out, type);
}
}
示例5
KubeCloudImageImpl(@NotNull final KubeCloudImageData kubeCloudImageData,
@NotNull final KubeApiConnector apiConnector) {
myImageData = kubeCloudImageData;
final String templateContent = myImageData.getCustomPodTemplateContent();
if (StringUtil.isEmpty(templateContent)){
myPodTemplate = null;
myPVCTemplate = null;
} else if (templateContent.contains("\n---\n")){
final String processedContent;
final String placeholderReplacement = "__INSTANCE__ID__";
if (templateContent.contains("%instance.id%")){
processedContent = templateContent.replaceAll("%instance.id%", placeholderReplacement);
} else {
processedContent = templateContent;
}
YAMLFactory factory = new YAMLFactory();
ObjectMapper mapper = new ObjectMapper();
final AtomicReference<String> podTemplateRef = new AtomicReference<>();
final AtomicReference<String> pvcTemplateRef = new AtomicReference<>();
try {
YAMLParser parser = factory.createParser(processedContent);
List<ObjectNode> list = mapper.readValues(parser, ObjectNode.class).readAll();
list.forEach(node->{
if (node.get("kind") != null && "Pod".equals(node.get("kind").textValue())){
if (podTemplateRef.get() != null){
throw new RuntimeException("More than one Pod template is specified for image " + myImageData.getAgentNamePrefix());
}
podTemplateRef.set(node.toString().replaceAll(placeholderReplacement, "%instance.id%"));
} else if (node.get("kind") != null && "PersistentVolumeClaim".equals(node.get("kind").textValue())){
if (pvcTemplateRef.get() != null){
throw new RuntimeException("More than one PVC template is specified for image " + myImageData.getAgentNamePrefix());
}
pvcTemplateRef.set(node.toString().replaceAll(placeholderReplacement, "%instance.id%"));
} else{
throw new RuntimeException("Unknown yaml template is specified for image " + myImageData.getAgentNamePrefix());
}
});
} catch (IOException e) {
ExceptionUtil.rethrowAsRuntimeException(e);
}
myPodTemplate = podTemplateRef.get();
myPVCTemplate = pvcTemplateRef.get();
} else {
myPodTemplate = templateContent;
myPVCTemplate = null;
}
myApiConnector = apiConnector;
}
示例6
protected <T> T deserialize(Class<T> type, String yml) throws IOException {
YAMLParser parser = new YAMLFactory().createParser(yml);
return jacksonService.newObjectMapper().readValue(parser, type);
}