yaml parsing using com.fasterxml.jackson
Asked Answered
K

1

7

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)
Kev answered 18/11, 2013 at 13:2 Comment(0)
Q
8

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>
Quarantine answered 29/1, 2014 at 16:16 Comment(1)
Yes i was also working with a different version of the lib. Updating solved the problem. I should have updated the answer.Kev

© 2022 - 2024 — McMap. All rights reserved.