2

I'm parsing an YAML file with jackson yaml parser jackson-dataformat-yaml and it's failing to parse the following YAML file.

environments:
  linux:
    - [rbenv,python-2.7]

env:
  global:
    # The username below should have write access to your module repository, to automatically commit new version tags.
    - USERNAME=sdfasfaf
    - secure: "fadfasdf" 

build: |
  set -o errexit
  set -o nounset

  #./bootstrap_factory_utils

It is having trouble parsing - USERNAME=sdfasfaf property. Below are the pojo classes representing the YAML file.

SFYaml.java:

public class SFYaml {

    private Environments environments;

    private Env env;

    private String build;

}

Env.java:

public class Env {

    private Global[] global;

}

Global.java:

public class Global {

    private String secure;

}

Method to parse the yaml file :

public String readYaml() {
        YAMLFactory yamlFactory = new YAMLFactory();
        YAMLMapper yMapper = new YAMLMapper(yamlFactory);
        yMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        String output = "";
        try {
            SFYaml sfYaml = yMapper.readValue(new File(yamlPath), SFYaml.class);
            output = yMapper.writeValueAsString(sfYaml);
            log.info(sfYaml.getPublish());
            // log.info(output);
        } catch (JsonProcessingException e1) {
            e1.printStackTrace();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        return output;
    }

I tried adding @JsonProperty annotation, @JsonIgnoreProperties(ignoreUnknown = true) and tried with various DeserializationFeature features to disable the parsing failures but I always get the below error.

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.amadeus.bitbucket.pojos.Global (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('USERNAME=sdfasfaf') at [Source: (File); line: 12, column: 7] (through reference chain: com.amadeus.bitbucket.pojos.SFYaml["env"]->com.amadeus.bitbucket.pojos.Env["global"]->java.lang.Object[][0]) at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63) at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1342) at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1031) at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:371) at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:323)

How can parse this USERNAME=sdfasfaf property? Or use any workarounds if possible?

Anthon
  • 69,918
  • 32
  • 186
  • 246
harshavmb
  • 3,404
  • 3
  • 21
  • 55
  • There is nothing in `Global` that could match this field . – Arnaud Jul 27 '18 at 07:27
  • Exactly! I tried with string member variable but it is not parsing. What should be the pojo look like? – harshavmb Jul 27 '18 at 07:27
  • You could add a `userName` field to your class, but in that case the YAML part should be something like `userName: "sdfasfaf"` . – Arnaud Jul 27 '18 at 07:30
  • yeah, I thought about it! But, I cannot change the yaml unfortunately as it is owned by a different team. But, it is a valid yaml. Is there a way to parse it or ignore it. I'm fine with both these options. – harshavmb Jul 27 '18 at 07:32
  • 1
    Could you try annotating your `Global` class with `@JsonIgnoreProperties(ignoreUnknown = true)` ? Alternatively, you may also try providing a `Global(Strins something)` constructor . – Arnaud Jul 27 '18 at 07:38
  • I did, it failed. – harshavmb Jul 27 '18 at 07:39
  • A different error now after adding a field constructor. `com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.amadeus.bitbucket.pojos.Global` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (File); line: 15, column: 7] (through reference chain: com.amadeus.bitbucket.pojos.SFYaml["env"]->com.amadeus.bitbucket.pojos.Env["global"]->java.lang.Object[][1]) at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(Mismatc` – harshavmb Jul 27 '18 at 07:42

1 Answers1

1

There is problem in pojo class. Global expect list of diffeent type. Use JsonNode in Env class.

Env class:

public class Env {

private JsonNode global;
}
GolamMazid Sajib
  • 8,698
  • 6
  • 21
  • 39