yaml使用com.fasterxml.jackson解析

时间:2013-11-18 13:02:37

标签: java parsing jackson yaml fasterxml

我正在尝试使用jackson

将yaml文件解析为模型

模型 -

public class TestModel {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

解析

private static ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
TestModel obj = mapper.readValue(yamlFile, TestModel.class);

YAML的文件

name: somename

异常

Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'name': was expecting 'null', 'true', 'false' or NaN
[error]  at [Source: /Users/amruth.s/Documents/Analytics/hydra/reportManager/target/scala-2.9.1/classes/viz/LineGraph.yaml; line: 1, column: 6]
[error]     at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1369)
[error]     at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:599)
[error]     at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:2449)
[error]     at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:2426)
[error]     at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._matchToken(UTF8StreamJsonParser.java:2403)
[error]     at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:760)
[error]     at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:665)
[error]     at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:2926)
[error]     at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2873)
[error]     at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1988)
[error]     at com.flipkart.hydra.util.YAMLUtil.toObject(YAMLUtil.java:29)

1 个答案:

答案 0 :(得分:7)

尝试更新您的jackson-dataformat-yaml版本。我遇到了同样的问题,直到我更新了Maven依赖的版本。

我的依赖关系如下:

<dependency>
 <groupId>com.fasterxml.jackson.dataformat</groupId>
 <artifactId>jackson-dataformat-yaml</artifactId>
 <version>2.1.3</version>
</dependency>