android - Custom converter of Simple XML is not serializing from xml to java object -
i have simple xml annotated class want use serialization/deserialization. have byte[] array using custom converter fails @ read method..
here simple xml annotated object
@root public class device implements serializable { @element @convert(bytearrayconverter.class) protected byte[] imageref; ... }
here converter
import org.simpleframework.xml.convert.converter; import org.simpleframework.xml.stream.inputnode; import org.simpleframework.xml.stream.outputnode; public class bytearrayconverter implements converter<byte[]> { @override public byte[] read(inputnode node) throws exception { string value = node.getvalue(); //return value.getbytes(); return new byte[]{1,2,3,4,5}; } @override public void write(outputnode node, byte[] bytearray) throws exception { node.setvalue("something"); } }
here xml called device.xml
<device> <imageref>aqidbau=</imageref> </device>
here serialization code failing
strategy strategy = new annotationstrategy(); serializer serializer = new persister(strategy); file file = new file("device.xml"); device device = serializer.read(device.class, file);
i have put break point in bytearrayconverter.read() method , executes correctly , returns byte array. after somewhere in simple xml
exception in thread "main" org.simpleframework.xml.core.instantiationexception: incompatible class [b field 'imageref' protected byte[] com.test.device.imageref @ line 4 @ org.simpleframework.xml.core.factory.getoverride(factory.java:147) @ org.simpleframework.xml.core.primitivefactory.getinstance(primitivefactory.java:82) @ org.simpleframework.xml.core.primitive.readelement(primitive.java:186) @ org.simpleframework.xml.core.primitive.read(primitive.java:124) @ org.simpleframework.xml.core.composite.readvariable(composite.java:623) @ org.simpleframework.xml.core.composite.readinstance(composite.java:573) @ org.simpleframework.xml.core.composite.readunion(composite.java:549) @ org.simpleframework.xml.core.composite.readelement(composite.java:532) @ org.simpleframework.xml.core.composite.readelements(composite.java:445) @ org.simpleframework.xml.core.composite.access$400(composite.java:59) @ org.simpleframework.xml.core.composite$builder.read(composite.java:1383) @ org.simpleframework.xml.core.composite.read(composite.java:201) @ org.simpleframework.xml.core.composite.read(composite.java:148) @ org.simpleframework.xml.core.traverser.read(traverser.java:92) @ org.simpleframework.xml.core.persister.read(persister.java:625) @ org.simpleframework.xml.core.persister.read(persister.java:606) @ org.simpleframework.xml.core.persister.read(persister.java:584) @ org.simpleframework.xml.core.persister.read(persister.java:543) @ org.simpleframework.xml.core.persister.read(persister.java:521) @ org.simpleframework.xml.core.persister.read(persister.java:426)
any pointers helpful..
you using primitive type array, may cause exception. byte array encoded (e.g. base64)? if use encoding, have care in read()
/ write()
too.
a converter
not best choice here, better take transform
. can implemented object <-> string transformation own.
here's simple example:
public class bytearraytransformer implements transform<byte[]> { @override public byte[] read(string value) throws exception { /* * todo: implement proper encoding */ return value.getbytes(); } @override public string write(byte[] value) throws exception { /* * todo: implement proper encoding */ return new string(value); } }
this allows remove converter (@convert
annotation of field , annotationstrategy
).
and here's how bind transformer:
registrymatcher m = new registrymatcher(); m.bind(byte[].class, new bytearraytransformer()); serializer ser = new persister(m); device d = ser.read(device.class, f);
Comments
Post a Comment