question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serializing a named list unintuitive

See original GitHub issue

My use case is very simple, I have to create an XML document that has, among others, a list of “item” nodes with a parent “items” node. It could look like this:

<channel>
  <items>
    <item>...</item>
    <item>...</item>
  </items>
</chanell>

However, the naive approach of using a list/array cannot seem to produce this with any amount of configuration via annotations:

public class Channel
{
  public final List<Item> items = new ArrayList<>();
}

public class Item
{
}

// Produces: <channel><items><items></items>...</items></channel>

Ultimately, the only way I was able to create the output conforming to the schema from above was to create a useless wrapper class, turn off wrapping, and name my list nonsensically:

public class Channel
{
  public final Items items = new Items();
}

public class Items
{
  @JacksonXmlElementWrapper(useWrapping=false)
  public final List<Item> item = new ArrayList<>();
}

public class Item
{
}

// Produces: <channel><items><item></item>...</items></channel>

This does result in the correct output schema, but now I am forced to access my objects in a non-intuitive way. For instance, if I want to add an item to my list of items, I must call the following:

channel.items.item.add(new Item());

which looks like I am adding an Item to another Item, rather than the list of items.

What I expected to work but did not was the following:

public class Channel
{
  // with wrapping enabled, the parent node should be "<items>"
  public final List<Item> items = new ArrayList<>();
}

// Each Item instance should be rendered as "<item>"
@JacksonXmlRootElement(localName="item")
public class Item
{
}

This would allow me to access my data in a sane manner:

channel.items.add(new Item());

TL;DR - It seems to me that @JacksonXmlRootElement(localName="item") does not work on any object in a collection/array, and instead the localName of the collection is used for the parent node (correct) AND each object in the collection/array (incorrect).

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
jacobharris919commented, Aug 20, 2015

Have you tried this?

   public class Channel
   {
      @JacksonXmlElementWrapper(localName = "items")
      @JacksonXmlProperty(localName = "item")
      public final List<Item> items = new ArrayList<Item>();
    }
2reactions
jackyguruicommented, Aug 27, 2015

OK, I think I have found a solution:

public class Channel
{
  @JacksonXmlElementWrapper(useWrapping = false)
  @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
  public final List<Item> items = new ArrayList<Item>();
}

Which gives me exactly what I wanted:

<channel>
  <items>
    <pen>...</pen>
    <pencil>...</pencil>
    <paper>...</paper>
  </items>
</channel>

However I am not entirely sure if it is a bug that if I didn’t specify @JacksonXmlElementWrapper(useWrapping = false) I would end up with an output like this:

<channel>
  <items>
      <items>
        <pen>...</pen>
        <pencil>...</pencil>
        <paper>...</paper>
      </items>
  </items>
</channel>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Serializing Object with Lists Takes Longer than serializing the ...
If I store all of the separate Lists, and the Lists of arrays. It takes .75 seconds to serialize, and 2 seconds to...
Read more >
Serializing Python Objects - Dive Into Python 3
Lists, tuples, dictionaries, and sets containing any combination of native ... To do this, it serializes the data structure using a data format...
Read more >
JSON Serialize | Guide to Syntax and Examples of ... - eduCBA
In C#, JSON Serialization, it supports two data structures. Collection of name/ value pairs; Ordered list of values. Object: An object begins with...
Read more >
The Serializer Component (Symfony Docs)
This normalizer converts DateTimeZone objects into strings that represent the name of the timezone according to the list of PHP timezones. DataUriNormalizer ...
Read more >
Make a Python Class JSON Serializable - PYnative
dumps() only knows how to serialize the basic set of object types by default (e.g., dictionary, lists, strings, numbers, None, etc.). To solve...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found