gpt4 book ai didi

java - Jackson yaml 库无法解析有效的 yaml 文件

转载 作者:行者123 更新时间:2023-11-30 10:13:00 27 4
gpt4 key购买 nike

我正在使用 jackson yaml 解析器 jackson-dataformat-yaml 解析 YAML 文件,但无法解析以下 YAML 文件。

environments:
linux:
- [rbenv,python-2.7]

env:
global:
# The username below should have write access to your module repository, to automatically commit new version tags.
- USERNAME=sdfasfaf
- secure: "fadfasdf"

build: |
set -o errexit
set -o nounset

#./bootstrap_factory_utils

解析 - USERNAME=sdfasfaf 属性时遇到问题。下面是代表 YAML 文件的 pojo 类。

SFYaml.java:

public class SFYaml {

private Environments environments;

private Env env;

private String build;

}

环境.java:

public class Env {

private Global[] global;

}

全局.java:

public class Global {

private String secure;

}

解析yaml文件的方法:

public String readYaml() {
YAMLFactory yamlFactory = new YAMLFactory();
YAMLMapper yMapper = new YAMLMapper(yamlFactory);
yMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String output = "";
try {
SFYaml sfYaml = yMapper.readValue(new File(yamlPath), SFYaml.class);
output = yMapper.writeValueAsString(sfYaml);
log.info(sfYaml.getPublish());
// log.info(output);
} catch (JsonProcessingException e1) {
e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
return output;
}

我尝试添加 @JsonProperty 注释,@JsonIgnoreProperties(ignoreUnknown = true) 并尝试使用各种 DeserializationFeature 功能来禁用解析失败但是我总是收到以下错误。

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.amadeus.bitbucket.pojos.Global (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('USERNAME=sdfasfaf') at [Source: (File); line: 12, column: 7] (through reference chain: com.amadeus.bitbucket.pojos.SFYaml["env"]->com.amadeus.bitbucket.pojos.Env["global"]->java.lang.Object[][0]) at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63) at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1342) at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1031) at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:371) at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:323)

如何解析这个 USERNAME=sdfasfaf 属性?或者尽可能使用任何解决方法?

最佳答案

pojo 类有问题。不同类型的全局期望列表。在 Env 类中使用 JsonNode

环境等级:

public class Env {

private JsonNode global;
}

关于java - Jackson yaml 库无法解析有效的 yaml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51552843/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com