java - Jackson custom date serializer -
i need set format class' date serialization. have version of jackson, doesn't have @jsonformat. that's why wrote custom class:
public class cdjsondateserializer extends jsonserializer<date>{ @override public void serialize(date date, jsongenerator jsongenerator, serializerprovider provider) throws ioexception { simpledateformat dateformat = new simpledateformat("mm/dd/yyyy"); string datestring = dateformat.format(date); jsongenerator.writestring(datestring); }
}
and used it:
@jsonserialize(using = cdjsondateserializer.class) private date startdate;
but, have fields have different date's formats , don't want create classes serialization. can add needed formats constants cdjsondateserializer class , set needed format annotation @jsonserialize
? this:
@jsonserialize(using = cdjsondateserializer.class, cdjsondateserializer.first_format)
.
after answer below:
it works after corrections. i've changed way of getting annotation in createcontextual method:
@override public jsonserializer createcontextual(serializationconfig serializationconfig, beanproperty beanproperty) { return new customdateserializer(beanproperty.getannotation(jsondateformat.class).value()); }
and i've added @jacksonannotation created new annotation jsondateformat:
@retention(retentionpolicy.runtime) @jacksonannotation public @interface jsondateformat { string value(); }
if cannot use @jsonformat jackson 2, i'd recommend introduce own custom annotation contain format field. serailizer should implement contextualserializer
interface access annotation value.
here example jackson 1.9.x:
public class jacksondateformat { @retention(retentionpolicy.runtime) public static @interface myjsonformat { string value(); } public static class bean { @myjsonformat("dd.mm.yyyy") @jsonserialize(using = mydateserializer.class) public final date date1; @myjsonformat("yyyy-mm-dd") @jsonserialize(using = mydateserializer.class) public final date date2; public bean(final date date1, final date date2) { this.date1 = date1; this.date2 = date2; } } public static class mydateserializer extends jsonserializer<date> implements contextualserializer { private final string format; private mydateserializer(final string format) {this.format = format;} public mydateserializer() {this.format = null;} @override public void serialize( final date value, final jsongenerator jgen, final serializerprovider provider) throws ioexception { jgen.writestring(new simpledateformat(format).format(value)); } @override public jsonserializer createcontextual( final serializationconfig serializationconfig, final beanproperty beanproperty) throws jsonmappingexception { final annotatedelement annotated = beanproperty.getmember().getannotated(); return new mydateserializer(annotated.getannotation(myjsonformat.class).value()); } } public static void main(string[] args) throws ioexception { final objectmapper mapper = new objectmapper(); final bean value = new bean(new date(), new date()); system.out.println(mapper .writerwithdefaultprettyprinter() .writevalueasstring(value)); } }
output:
{ "date1" : "02.12.2014", "date2" : "2014-12-02" }
if have access objectmapper
can register custom serializer date
types, want longer need put @jsonserialize
annotation.
here example:
final objectmapper mapper = new objectmapper(); final simplemodule module = new simplemodule("", version.unknownversion()); module.addserializer(date.class, new mydateserializer(null)); mapper.registermodule(module);
Comments
Post a Comment