Jackson yaml 库无法解析有效的 yaml 文件
Jackson yaml library unable to parse a valid yaml file
我正在使用 jackson yaml 解析器解析 YAML 文件 jackson-dataformat-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;
}
Env.java:
public class Env {
private Global[] 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 class 有问题。不同类型的全局期望列表。
在环境 class.
中使用 JsonNode
环境 class:
public class Env {
private JsonNode global;
}
我正在使用 jackson yaml 解析器解析 YAML 文件 jackson-dataformat-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;
}
Env.java:
public class Env {
private Global[] 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 class 有问题。不同类型的全局期望列表。 在环境 class.
中使用JsonNode
环境 class:
public class Env {
private JsonNode global;
}