Custom converter for implicit map

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom converter for implicit map

Forage
Hi,

I'm trying to convert an EnumMap object property to an implicit list,
but I can't seem to figure out how to skip the parent node in the
marshal method of the custom converter.

Example:

public class PIzza {
     @XStreamConverter(ContentConverter.class)
     protected EnumMap<ContentType, URI> content =
             new EnumMap<ContentType, URI>(ContentType.class);
}

Desired XML:

<pizza>
     <content type="text">content.txt</content>
     <content type="html">content.html</content>
</pizza>

The key to attribute mapping is the reason I'm creating a custom converter.

Current converter:

public class ContentConverter implements Converter {
     @Override
     public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context) {
         EnumMap<Email.ContentType, URI> content =
             (EnumMap<Email.ContentType, URI>) source;

         for (Entry<Email.ContentType, URI> entry : content.entrySet()) {
             writer.startNode("content");
             writer.addAttribute("type",
entry.getKey().toString().toLowerCase());
             context.convertAnother(entry.getValue());
             writer.endNode();
         }
     }
}

Resulting XML:

<pizza>
     <content>
         <content type="text">content.txt</content>
         <content type="html">content.html</content>
     </content>
<pizza>

How do I prevent the parent "content" node from being created? Marking
the property as implicit causes the converter to be ignored, closing the
writer makes no difference and not starting new nodes will make a mess
of things.

It also makes me wonder how one has to unmarshal an implicit map, as the
entries won't be contained within a parent node any more.

Yours sincerely,

Age Bosma

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Custom converter for implicit map

Jörg Schaible-2
Hi,

Age Bosma wrote:

> Hi,
>
> I'm trying to convert an EnumMap object property to an implicit list,
> but I can't seem to figure out how to skip the parent node in the
> marshal method of the custom converter.
>
> Example:
>
> public class PIzza {
>      @XStreamConverter(ContentConverter.class)
>      protected EnumMap<ContentType, URI> content =
>              new EnumMap<ContentType, URI>(ContentType.class);
> }
>
> Desired XML:
>
> <pizza>
>      <content type="text">content.txt</content>
>      <content type="html">content.html</content>
> </pizza>
>
> The key to attribute mapping is the reason I'm creating a custom
> converter.
>
> Current converter:
>
> public class ContentConverter implements Converter {
>      @Override
>      public void marshal(Object source, HierarchicalStreamWriter writer,
> MarshallingContext context) {
>          EnumMap<Email.ContentType, URI> content =
>              (EnumMap<Email.ContentType, URI>) source;
>
>          for (Entry<Email.ContentType, URI> entry : content.entrySet()) {
>              writer.startNode("content");
>              writer.addAttribute("type",
> entry.getKey().toString().toLowerCase());
>              context.convertAnother(entry.getValue());
>              writer.endNode();
>          }
>      }
> }
>
> Resulting XML:
>
> <pizza>
>      <content>
>          <content type="text">content.txt</content>
>          <content type="html">content.html</content>
>      </content>
> <pizza>
>
> How do I prevent the parent "content" node from being created? Marking
> the property as implicit causes the converter to be ignored, closing the
> writer makes no difference and not starting new nodes will make a mess
> of things.
>
> It also makes me wonder how one has to unmarshal an implicit map, as the
> entries won't be contained within a parent node any more.

See, you already hit the point. The "implicit" support is a capability of
the ReflectionConverter to write included collections/maps/arrays as flat
elements. There's no other converter involved for the collection itself. You
will have to write a custom converter for your Pizza type.

- Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email