简体   繁体   中英

Error parsing YAML file using Jackson APIs

I am having a YAML file defined as below. I verified that it is a valid YAML file. However, I am getting an exception mentioned below.

As the YAML is a valid file, I am not sure where it is lacking that causes the exception. Can someone please provide some hints what I need to change to fix this?

#========================================================================

# Test Context

- transaction:
    request:
      method: populate_db
      params:
        param1: param-value-1
    response:
      result: database1
      error: null

- transaction:
    request:
      method: updateTable
      params:
        param1: database1
    response:
      result: schema
      error: null

#========================================================================

The deserializer code I have written as below.

public class TestScriptDeserializer {

    private static final Logger logger = LoggerFactory.getLogger(TestScriptDeserializer.class.getName());
    private static final long serialVersionUID = 1L;

    public static Map<String, Object> deserialize(String yamlFile) {

        ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());    
        try {
            final Map<String, Object> testScriptObj = yamlReader.readValue(new File(yamlFile), Map.class);

            if (testScriptObj != null) {
                return testScriptObj;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Test Script deserialization failed");
            return null;
        }

        return null;
    }
}

When I run this code, I get the following error.

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
 at [Source: (File); line: 6, column: 1]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
    at com.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1234)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1122)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1075)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer._deserializeFromEmpty(StdDeserializer.java:892)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:358)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:27)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3814)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2756)
    at com.project.automation.utils.parser.TestScriptDeserializer.deserialize(TestScriptDeserializer.java:27)

The error message states what's wrong:

Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token

The deserialiser tries to create a LinkedHashMap from your YAML input. It does so because you tell it that it should read the content into a Map.class , and the LinkedHashMap happens to be the Map implementation the deserialiser uses.

Now while trying to create such a Map , the first token the deserialiser encounters while parsing the YAML is a START_ARRAY token. This is a naming bug somewhere, because YAML has sequences , not arrays . But it is obvious what happens: The root node of your YAML file is a sequence (it contains two block sequence nodes, starting with - each). The deserialiser has no idea how to deserialise a YAML sequence into a LinkedHashMap , hence the error.

To fix it, either deserialise into a List or change the YAML to contain a YAML mapping as root node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM