我正在尝试为 Simple XML 中的 byte[] 编写自定义转换器。它正在工作中途意味着调用写入方法而不是读取。有人能指出为什么吗??
这是我的简单 xml 注释对象
@Root
public class Device implements Serializable
{
private final static long serialVersionUID = 1L;
@Element
@Convert(ByteArrayConverter.class)
protected byte[] imageRef;
public byte[] getImageRef() {
return imageRef;
}
public void setImageRef(byte[] imageRef) {
this.imageRef = imageRef;
}
这是我的自定义转换器
public class ByteArrayConverter implements Converter<byte[]>
{
@Override
public byte[] read(InputNode node) throws Exception
{
//put a break point here BUT Code not getting here
("I am here in read")
String s = node.getValue();
return Base64.decode(s);
}
@Override
public void write(OutputNode node, byte[] byteArray) throws Exception
{
("I am here in write")
node.setValue(Base64.encode(byteArray))
}
这是我的序列化/反序列化代码
Registry registry = new Registry();
Strategy strategy = new RegistryStrategy(registry);
Serializer serializer = new Persister(strategy);
try
{
registry.bind(byte[].class, ByteArrayConverter.class);
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Device device = new Device();
device.setImageRef(new byte[]{1,2,3});
File file = new File("myDevice.xml");
serializer.write(device, file);
Device readDevice = serializer.read(Device.class, file);
当我在调试器中执行此操作时,我确实看到调试器在 write 方法中停止,但它并没有在read()方法中停止,因此我没有得到预期的结果。当我们重新构造对象时,为什么这段代码不会停止阅读?谢谢