Override/extend basic converter

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

Override/extend basic converter

Mircea Cocosila-2
Hi,

I'd like to serialize a date object like this (timezone to be an attribute in
the produced xml):

    <date tz="EDT">
      <date>Sep 2, 2005 2:42:36 PM</date>
    </date>

I think that a custom converter should do it.

I would define a POJO as:

public class DateVO {
  String tz;
  Date date;
}

I would define an alias: alias("date", DateVO.class);

But, please pay attention at these details (maybe I try to stretch XStream in an
unsual way ?!):

- the XStream's basic DateConverter uses a default formatter that doesn't meet
my needs. It is "yyyy-MM-dd HH:mm:ss.S z."
I need it to be "MMM d, yyyy h:mm:ss a."

With the intention of overriding the DateConverter (is this a good way to go
with it?), I created a subclass of DateConverter and redefined the default
formatter like this:

public class DateConverterEx extends DateConverter {

  public DateConverterEx() {
    // this("yyyy-MM-dd HH:mm:ss.S z",
    super("MMM d, yyyy h:mm:ss a z",
      new String[] {
          "yyyy-MM-dd HH:mm:ss.S a",
          // JDK 1.3 needs both versions
          "yyyy-MM-dd HH:mm:ssz", "yyyy-MM-dd HH:mm:ss z",
          // backwards compatability
          "yyyy-MM-dd HH:mm:ssa" });
  }

}

Then, I registered the "custom" converter:

  _xstream.registerConverter(new DateConverterEx());

I could go on explaining (asking questions) for my case, but please tell me if I
go on the right way. Then, I could continue.

Thanks a lot for looking into this.

--Mircea

Reply | Threaded
Open this post in threaded view
|

Re: Override/extend basic converter

Joe Walnes-2
Hi Mircea

> - the XStream's basic DateConverter uses a default formatter that doesn't meet
> my needs. It is "yyyy-MM-dd HH:mm:ss.S z."
> I need it to be "MMM d, yyyy h:mm:ss a."
>
> With the intention of overriding the DateConverter (is this a good way to go
> with it?), I created a subclass of DateConverter and redefined the default
> formatter like this:
(snip)
> Then, I registered the "custom" converter:
>
>   _xstream.registerConverter(new DateConverterEx());

This is fine, although it could be simplified it as you don't actually
need to subclass the DateConverter to change the format.

 _xstream.registerConverter(new DateConverter(/* your formats */));

-Joe
Reply | Threaded
Open this post in threaded view
|

Re: Override/extend basic converter

Cocoshu
Joe Walnes <joe@...> writes:

 
> This is fine, although it could be simplified it as you don't actually
> need to subclass the DateConverter to change the format.
>
>  _xstream.registerConverter(new DateConverter(/* your formats */));
>
> -Joe
>
 

Thanks Joe. I like your proposal.

Now I have another question.
What happens withthe built-in DateConverter that was preregistered when the
 XStream was instantiated. Will it be overriden by the new DateConverter
 instance?

Please (comment a bit) / (give me a pointer to an existing explanation, if any)
on the converters overriding scheme. Could be many converters registered for the
same data type? Anyway, it seems natural that only one has to be active and
applied by default when serializing the data type in question.

--Mircea


Reply | Threaded
Open this post in threaded view
|

Re: Re: Override/extend basic converter

Joe Walnes-2
> Thanks Joe. I like your proposal.
>
> Now I have another question.
> What happens withthe built-in DateConverter that was preregistered when the
>  XStream was instantiated. Will it be overriden by the new DateConverter
>  instance?

When a new Converter is registered it gets added to a list of
converters (optionally with a priority). When XStream attempts to
marshal/unmarshal an object it searches the list of converters, sorted
first by priority and second by the order it was added (newest first).

So if you add a new DateConverter (with the default priority) it will
use that as it's a more recent addition than the existing one.

-Joe
Reply | Threaded
Open this post in threaded view
|

Re: Override/extend basic converter

Cocoshu

Your explanation is crystal clear. Thanks a lot Joe.

--Mircea