yaml parsing using com.fasterxml.jackson

amrk7 picture amrk7 · Nov 18, 2013 · Viewed 9.4k times · Source

I am trying to parse yaml files into models using jackson

Model --

public class TestModel {

    private String name;

    public String getName() {
        return name;
    }

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

Parsing

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

yaml-file

name: somename

Exception

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)

Answer

Mike Fisher picture Mike Fisher · Jan 29, 2014

Try updating your version of jackson-dataformat-yaml. I was running into the same issue until I updated the version of my Maven dependency.

My dependency looks like:

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