简体   繁体   中英

Parse yaml file with Jackson

I am trying to parse a yaml file looking like this(it is dynamic so this is just an example):

size: 4533
vehicles:
  '1':
    vecUUID: fa461669-6b88-418d-8e7c-219e784a21fc
    ownerUUID: da36067f-7af8-411b-b820-8e0709b4d64e
    invString: |
      rO0ABXcEAAAACXBwcHBwcHBwcA==
  '2':
    vecUUID: 3c192337-c9dd-45f0-92c0-545d42d5404d
    ownerUUID: da36067f-7af8-411b-b820-8e0709b4d64e
    invString: |
      rO0ABXcEAAAACXBwcHBwcHBwcA==
  '3':
    vecUUID: d5df362b-c4f8-4d82-9e3a-c37fba1162f0
    ownerUUID: fd44ce79-32b5-4080-9953-3d0f4d5399b1
    invString: |
      rO0ABXcEAAAAEnBwcHBwcHBwc3IAGm9yZy5idWtraXQudXRpbC5pby5XcmFwcGVy8lBH7PESbwUC
      AAFMAANtYXB0AA9MamF2YS91dGlsL01hcDt4cHNyADVjb20uZ29vZ2xlLmNvbW1vbi5jb2xsZWN0
      LkltbXV0YWJsZU1hcCRTZXJpYWxpemVkRm9ybQAAAAAAAAAAAgACWwAEa2V5c3QAE1tMamF2YS9s
      YW5nL09iamVjdDtbAAZ2YWx1ZXNxAH4ABHhwdXIAE1tMamF2YS5sYW5nLk9iamVjdDuQzlifEHMp
      bAIAAHhwAAAAA3QAAj09dAAEdHlwZXQABmFtb3VudHVxAH4ABgAAAAN0AB5vcmcuYnVra2l0Lmlu
      dmVudG9yeS5JdGVtU3RhY2t0AAxHTEFTU19CT1RUTEVzcgARamF2YS5sYW5nLkludGVnZXIS4qCk
      94GHOAIAAUkABXZhbHVleHIAEGphdmEubGFuZy5OdW1iZXKGrJUdC5TgiwIAAHhwAAAADHBwcHBw
      cHBwcA==

I have created 3 classes, Vehicle, VehicleInfo and MyYamlFile as seen below.

Vehicle

public class Vehicle {
  @JsonProperty
  private List<VehicleInfo> info;
  // getter methods ommitted
}

VehicleInfo

public class VehicleInfo {
    @JsonProperty
    private String vecUUID;
    @JsonProperty
    private String ownerUUID;
    @JsonProperty
    private String invString;
    // getter methods ommitted
 }

MyYamlFile

public class MyYamlFile {
    @JsonProperty
    private List<Vehicle> vehicles;
    @JsonProperty
    private int size;
    // getter methods ommitted
}

The problem is when I run it as seen here:

public class Fix {
    public static void main(String[] args) {
        MyYamlFile file = readYaml(new File("E:\\dev\\FrihedenBilInventoryFix\\VehicleInventory.yaml"));
        System.out.print("test: " + file.getSize());
    }
    public static MyYamlFile readYaml(final File file) {
        final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson databind
        try {
            return mapper.readValue(file, MyYamlFile.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

I get an error

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: (File); line: 3, column: 3] (through reference chain: MyYamlFile["vehicles"])

any idea why?

You are trying to parse your file into a List .

According to YAML documentation :

All members of a list are lines beginning at the same indentation level starting with a "- " (a dash and a space):

Whereas in your case, all items that are supposed to be members of the list have the following format '1': , which is incorrect.

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