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

@JacksonXmlElementWrapper not respected when serializing Iterators / Iterables #148

Closed
stanislawosinski opened this issue Apr 16, 2015 · 2 comments
Milestone

Comments

@stanislawosinski
Copy link

I'm using Jackson 2.5.2 to serialize objects both to JSON and XML. My use case involves serializing large dynamically generated collections, hence the need to expose an Iterator rather than a List for serialization.

Everything works perfectly with one snag: @JacksonXmlElementWrapper does not seem to be respected when serializing Iterator / Iterable. I'd expect the following code:

public class WrapperTest {
  public static void main(String[] args) throws IOException {
    new XmlMapper().writeValue(System.out, new ToSerialize());
  }

  private static class ToSerialize {
    @JsonProperty("item")
    @JacksonXmlElementWrapper(localName = "list")
    public Iterator<String> items() {
      return new Iterator<String>() {
        int item = 4;

        @Override
        public boolean hasNext() {
          return item > 0;
        }

        @Override
        public String next() {
          item--;
          return Integer.toString(item);
        }
      };
    }
  }
}

to produce:

<ToSerialize><list><item>3</item><item>2</item><item>1</item><item>0</item></list></ToSerialize>

but instead I'm getting:

<ToSerialize><item>3</item><item>2</item><item>1</item><item>0</item></ToSerialize>

Am I doing anything wrong here?

If I replace the Iterator with a List, the wrapping element gets correctly added, but switching to in-memory lists is not practical in our use case. I could instead return a dummy list that returns my original iterator in the iterator() method (and throws UnsupportedOperationException in all other methods), but this seems a rather ugly hack.

@cowtowncoder cowtowncoder changed the title @JacksonXmlElementWrapper not respected when serializing Iterators / Iterables @JacksonXmlElementWrapper not respected when serializing Iterators / Iterables May 23, 2020
@cowtowncoder cowtowncoder added this to the 2.12.0 milestone Jun 7, 2020
cowtowncoder added a commit that referenced this issue Jun 7, 2020
cowtowncoder added a commit that referenced this issue Jun 7, 2020
@cowtowncoder cowtowncoder modified the milestones: 2.12.0, 2.11.1 Jun 7, 2020
@cowtowncoder
Copy link
Member

I can not reproduce this so it has probably fixed somewhere since 2.5.2.
Marking as fixed in 2.11.0.

@cowtowncoder
Copy link
Member

As per @JooHyukKim I think this is actually being only fixed in 2.16 now.

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