users@jersey.java.net

Re: [Jersey] Parsing unix timestamp in JSON

From: Chris Carrier <ctcarrier_at_gmail.com>
Date: Thu, 28 Jan 2010 10:26:24 -0800

Seems like the XmlJavaTypeAdapter annotation is going to do it. I have to
write a "custom" Date adapter that looks like:

public class DateAdapter extends XmlAdapter<String, Date> {
    private final static Log log = LogFactory.getLog(DateAdapter.class);


    @Override
    public Date unmarshal(String v) throws Exception {
        return new Date(Long.valueOf(v));
    }

    @Override
    public String marshal(Date v) throws Exception {
        return v.toString();
    }
}

Still seems a little crazy I have to write this code since it seems like it
should be the default. But it's not too much and it gives us the ability to
support multiple date formats if we want.

Thanks for all the help I probably never would have found that package level
annotation!

Chris

On Thu, Jan 28, 2010 at 10:08 AM, Chris Carrier <ctcarrier_at_gmail.com> wrote:

> When I just echo back the value after marshalling/unmarshalling it will
> turn a Unix timestamp from yesterday to:
>
> {"wrapped":"95508544-06-28T19:08:16.768-07:00"}
>
> Which is of course a nonsense year for another few million years. It seems
> a bit strange to me that I should have to go to such lengths to support Unix
> timestamps when the only non-default and non-deprecated java.util.Date
> constructor takes a Unix timestamp. It just seems like the parsers can't
> tell the value is a long. They all want to treat it like a String. I'm new
> to JSON but isn't it standard to wrap everything with quotes?
>
> I'll check out that page but it doesn't immediately look like my issue.
> This functionality worked fine when we were manually handling the servlet
> request/responses and using Jackson's default behavior. If I have to write
> 500 lines of code to do the same thing that we did in one line then I think
> selling my team on the usefulness of a JAX-RS framework is going to be
> difficult.
>
> Thanks
> Chris
>
> 2010/1/28 Felipe Gaścho <fgaucho_at_gmail.com>
>
> > behaving even stranger with my date format. Thanks for pointing out the
>>
>> what kind of strange behaviour ?
>>
>> may be this one:
>>
>> http://weblogs.java.net/blog/felipegaucho/archive/2009/12/06/jaxb-customization-xsddatetime
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>