Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YAML MismatchedInputException: Cannot deserialize instance of java.lang.String when passing file path as String #40

Closed
serpro69 opened this issue Oct 3, 2017 · 1 comment

Comments

@serpro69
Copy link

serpro69 commented Oct 3, 2017

I have a yaml file that I want to read into Map<String, String>:

    events:
      key1: val1
      key2: val2
      key3: val3

This piece of code is working fine.

public class EventGenerator {    
     private Map<String, String> events;
   
     public Map<String, String> getEvents() {
       return this.events;
     }
   
     public void setEvents() {
       ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
           try {
               FileInputStream fis = new FileInputStream("file.yaml");
               EventGenerator eventGenerator = mapper.readValue(fis, EventGenerator.class);
               fis.close();
               System.out.print(eventGenerator.getEvents());
           } catch (IOException e) {
               e.printStackTrace();
           }
   
     }
   
   }

But when I pass path of yaml file as a String (and/or File) argument

    public void setEvents(String filePath) {
    // same code
    FileInputStream fis = new FileInputStream(filePath)
    // same code
    }

I'm getting an exception

jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.lang.String

Also, when I override the setEvents() method and have both setEvents() and setEvents(String filePath) then neither are working and both are giving the above exception.

UPD (sorry, forgot to add pom)
jackson pom dependencies:

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
            <version>2.9.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.1</version>
        </dependency>
@serpro69
Copy link
Author

serpro69 commented Oct 4, 2017

Resolved

@serpro69 serpro69 closed this as completed Oct 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant