i have app uses jaxb , jersey (using moxy json provider), , need serialize json in java.util.calendar
objects converted epoch values. far have tried:
1) nothing allow default conversion happen:
@xmlaccessortype( xmlaccesstype.field ) public class foo { @xmlschematype( name = "datetime" ) protected calendar time; public calendar gettime() { return time; } public void settime( calendar value ) { this.time = value; } }
which results in iso-8601:
{ "foo" : { "time" : "2016-06-06t17:52:31.593-04:00" } }
2) use adapter convert long:
@xmlaccessortype( xmlaccesstype.field ) public class foo { @xmljavatypeadapter( calendaradapter.class ) @xmlschematype( name = "datetime" ) protected calendar time; public calendar gettime() { return time; } public void settime( calendar value ) { this.time = value; } }
which results in epoch value, string:
{ "foo" : { "time" : "1465250094634" } }
3) variation of 2:
@xmlaccessortype( xmlaccesstype.field ) public class foo { @xmlelement( type = string.class ) @xmljavatypeadapter( calendaradapter.class ) @xmlschematype( name = "datetime" ) protected calendar time; public calendar gettime() { return time; } public void settime( calendar value ) { this.time = value; } }
in @xmlelement
added set type string
when generate java pojo using xjc , definition:
<xs:annotation> <xs:appinfo> <jaxb:globalbindings> <xjc:javatype name="java.util.calendar" xmltype="xs:datetime" adapter="org.mitre.caasd.ea.core.util.calendaradapter" /> </jaxb:globalbindings> </xs:appinfo> </xs:annotation>
which gives crazy json:
{ "foo" : { "time" : { "type" : "long", "value" : 1465250270147 } } }
now have no idea why happen, nor why act of telling element type string
results in non-string value, bonus points if can explain this. could, however, see how json might increase clarity, client requesting this, not understand it. client existed long before replacement service, , such, need json this:
{ "foo" : { "time" : 1465250094634 } }
any idea how can make happen?
for sake of completeness, calendaradapter implementation trivial solution:
public class calendaradapter extends xmladapter<long, calendar> { @override public calendar unmarshal( long millis ) throws exception { calendar calendar = calendar.getinstance(); calendar.settimeinmillis( millis ); return calendar; } @override public long marshal( calendar calendar ) throws exception { return calendar.gettimeinmillis(); } }
also, using jersey version 2.22.1.
---- update ----
switching jackson instead of moxy , using adapter (over converter) works expected, using approach. perhaps should considered bug in moxy?