Skip to content

Error when desirializing the result of serialization #144

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

Closed
Hunsu opened this issue Mar 28, 2015 · 3 comments
Closed

Error when desirializing the result of serialization #144

Hunsu opened this issue Mar 28, 2015 · 3 comments

Comments

@Hunsu
Copy link

Hunsu commented Mar 28, 2015

I have a strange problem with deserializing xml into a POJO. I have this test:

    @Test
    public void testSerializeMovies() {
        Movie m1 = new Movie("tt12547", "The Hobbit", 2005);
        Movie m2 = new Movie("1254", "Title", 2007);
        List<Movie> movies = new ArrayList<Movie>();
        movies.add(m1);
        movies.add(m2);
        Movies root = new Movies(movies);
        String expected = "<root>"
                + "<movie imdbID=\"tt12547\" title=\"The Hobbit\" year=\"2005\"/>"
                + "<movie imdbID=\"1254\" title=\"Title\" year=\"2007\"/>"
                + "</root>";
        System.out.println(mapper.writeValueAsString(root));
        assertEquals(expected, mapper.writeValueAsString(root));
        root = mapper.readValue(expected, Movies.class);
        System.out.println(root);
    }

The serialisation works fine but when I try to deserialize the obtained xml I get this error:

    com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: 
    Unrecognized field "movie" (class org.m1.iaws.pcws.model.Movie),
    not marked as ignorable (3 known properties: "title", "imdbID", "year"])

When I do the desrialization of this xml:

    <root response="True">
        <movie title="The Hobbit" year="1977" imdbID="tt0077687" />
    </root>

I get the same error about response attribute even though response is part of the root element not the movie element. Normally response is member of the class Movies not Movie so why I get the error?

Unrecognized field "response" (class org.m1.iaws.pcws.model.Movie), not marked as ignorable
@cowtowncoder
Copy link
Member

Could you also include definition of Movie and Movies classes? I can not reproduce behavior without those.

@Hunsu
Copy link
Author

Hunsu commented Mar 30, 2015

Movie

    import com.fasterxml.jackson.annotation.JsonCreator;
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.annotation.JsonTypeName;
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

    @JacksonXmlRootElement(localName = "movie")
    @JsonIgnoreProperties(ignoreUnknown = false)
    public class Movie {
       @JacksonXmlProperty(isAttribute = true)
       private final String imdbID;
       @JacksonXmlProperty(isAttribute = true)
       private final String title;
      @JacksonXmlProperty(isAttribute = true)
      private int year;

    @JsonCreator
    public Movie(@JsonProperty("imdbID") String id,
            @JsonProperty("title") String title, @JsonProperty("year") int year) {
        this.imdbID = id;
        this.title = title;
        this.year = year;
    }

    public String getId() {
        return imdbID;
    }

    public String getTitle() {
        return title;
    }

    public int getYear() {
        return year;
    }

    public String toString() {
        return title;
    }

}

Movies

    import java.util.List;

    import com.fasterxml.jackson.annotation.JsonCreator;
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.annotation.JsonInclude.Include;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

   @JacksonXmlRootElement(localName = "root")
   @JsonIgnoreProperties(ignoreUnknown = true)
   @JsonInclude(Include.NON_NULL)
    public class Movies {
    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "movie")
    private List<Movie> movies;
    @JsonProperty("error")
    private String error;

    @JacksonXmlProperty(isAttribute = true)
    private boolean response;

    public Movies() {
    }

    @JsonCreator
    public Movies(List<Movie> movies) {
        this.movies = movies;
    }

    public List<Movie> getMovies() {
        return movies;
    }

    public String getError() {
        return error;
    }

    @JsonProperty("error")
    public void setError(String error) {
        this.error = error;
    }

    public void setResponse(boolean response) {
        this.response = true;
    }

    public String toString() {
        return movies.toString();
    }
}

@cowtowncoder
Copy link
Member

Test case now passes in that deserialization succeeds (although exact XML output differs somewhat, not sure if that is problematic or not). I suspect the problem was same as #177 (and/or possibly #167).
Either way, fixed for 2.7.0 or 2.6.5, both to be released in near future.

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

2 participants