java - (De)serialize enum set -
is there easy way serialize , deserialize enum sets jackson?
private enum type { yes, no } @jacksonxmlproperty(localname = "type", isattribute = true) private final enumset<type> types;
this gives following xml:
...<type type="yes" type="no"/>...
this xml not valid, since there duplicate attribute.
i tried following annotations:
@jsonserialize(using = enumsetserializer.class) @jacksonxmlproperty(localname = "type", isattribute = true) private final enumset<type> mtypes;
but gives following error:
exception in thread "main" com.fasterxml.jackson.databind.jsonmappingexception: class com.fasterxml.jackson.databind.ser.std.enumsetserializer has no default (no arg) constructor
in case should write custom enumset
serializer/deserializer convert enum set string. here complete example:
@suppresswarnings({"unchecked", "raw"}) public class jacksonenumset { public enum type { yes, no } public static class bean { @jacksonxmlelementwrapper(usewrapping = false) @jacksonxmlproperty(localname = "type", isattribute = true) @jsonserialize(using = enumsetserializer.class) @jsondeserialize(using = enumsetdeserializer.class) private final enumset<type> types; @jsoncreator public bean(@jsonproperty("type") final enumset<type> types) {this.types = types;} public enumset<type> gettypes() {return types;} @override public string tostring() { return "bean{" + "types=" + types + '}'; } } public static class enumsetserializer extends jsonserializer<enumset> { @override public void serialize( final enumset value, final jsongenerator gen, final serializerprovider serializers) throws ioexception { final stringbuilder builder = new stringbuilder(); (final object e : value) { if (builder.length() > 0) { builder.append(";"); } builder.append(e); } gen.writestring(builder.tostring()); } } public static class enumsetdeserializer extends jsondeserializer<enumset> implements contextualdeserializer { private class enumtype; @override public enumset deserialize( final jsonparser p, final deserializationcontext ctxt) throws ioexception { final string string = p.getvalueasstring(); final enumset enumset = enumset.noneof(enumtype); (final string name : string.split(";")) { enumset.add(enum.valueof(enumtype, name)); } return enumset; } @override public jsondeserializer<?> createcontextual( final deserializationcontext ctxt, final beanproperty property) throws jsonmappingexception { final collectionliketype type = (collectionliketype)property.gettype(); final enumsetdeserializer enumsetdeserializer = new enumsetdeserializer(); enumsetdeserializer.enumtype = type.getcontenttype().getrawclass(); return enumsetdeserializer; } } public static void main(string[] args) throws ioexception { final xmlmapper mapper = new xmlmapper(); final bean bean = new bean(enumset.allof(type.class)); final string xml = mapper.writevalueasstring(bean); system.out.println(xml); system.out.println(mapper.readvalue(xml, bean.class)); } }
output:
<bean xmlns="" type="yes;no"></bean> bean{types=[yes, no]}
Comments
Post a Comment