编程语言
首页 > 编程语言> > 简单XML的自定义转换器未从xml序列化为Java对象

简单XML的自定义转换器未从xml序列化为Java对象

作者:互联网

我有一个简单的XML带注释的类,我想用于序列化/反序列化.我有一个byte []数组,正在与自定义转换器一起使用,但在read方法上失败.

这是我的简单XML注释对象

@Root
public class Device implements Serializable
{
    @Element
    @Convert(ByteArrayConverter.class)
    protected byte[] imageRef;
...
}

这是我的转换器

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");     
    }         
}

这是我的XML,称为device.xml

<device>
    <imageRef>AQIDBAU=</imageRef>
</device>

这是我的序列化代码失败

Strategy strategy = new AnnotationStrategy();
Serializer serializer = new Persister(strategy);
File file = new File("device.xml");     
Device device = serializer.read(Device.class, file);

我在ByteArrayConverter.read()方法中设置了一个断点,并说它可以正确执行并返回一个字节数组.但是在那之后的简单XML中,我得到了这个

Exception in thread “main”
org.simpleframework.xml.core.InstantiationException: Incompatible
class [B for field ‘imageRef’ protected byte[]
com.test.Device.imageRef at line 4 at
org.simpleframework.xml.core.Factory.getOverride(Factory.java:147) at
org.simpleframework.xml.core.PrimitiveFactory.getInstance(PrimitiveFactory.java:82)
at
org.simpleframework.xml.core.Primitive.readElement(Primitive.java:186)
at org.simpleframework.xml.core.Primitive.read(Primitive.java:124)
at
org.simpleframework.xml.core.Composite.readVariable(Composite.java:623)
at
org.simpleframework.xml.core.Composite.readInstance(Composite.java:573)
at
org.simpleframework.xml.core.Composite.readUnion(Composite.java:549)
at
org.simpleframework.xml.core.Composite.readElement(Composite.java:532)
at
org.simpleframework.xml.core.Composite.readElements(Composite.java:445)
at
org.simpleframework.xml.core.Composite.access$400(Composite.java:59)
at
org.simpleframework.xml.core.Composite$Builder.read(Composite.java:1383)
at org.simpleframework.xml.core.Composite.read(Composite.java:201)
at org.simpleframework.xml.core.Composite.read(Composite.java:148)
at org.simpleframework.xml.core.Traverser.read(Traverser.java:92) at
org.simpleframework.xml.core.Persister.read(Persister.java:625) at
org.simpleframework.xml.core.Persister.read(Persister.java:606) at
org.simpleframework.xml.core.Persister.read(Persister.java:584) at
org.simpleframework.xml.core.Persister.read(Persister.java:543) at
org.simpleframework.xml.core.Persister.read(Persister.java:521) at
org.simpleframework.xml.core.Persister.read(Persister.java:426)

任何指针都会有所帮助.

解决方法:

您正在使用原始类型数组,这可能会导致异常.您的字节数组是否已编码(例如base64)?如果使用编码,则也必须在read()/ write()中对此加以考虑.

转换器不是这里的最佳选择,最好采用Transform.有了这些,您就可以实现Object<->.自己进行字符串转换.

这是一个非常简单的示例:

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);
    }

}

这使您可以删除转换器(该字段的@Convert注释和AnnotationStrategy).

最后是绑定变压器的方法:

RegistryMatcher m = new RegistryMatcher();
m.bind(byte[].class, new ByteArrayTransformer());

Serializer ser = new Persister(m);

Device d = ser.read(Device.class, f);

标签:simple-framework,xml-serialization,java,android,xml-parsing
来源: https://codeday.me/bug/20191123/2064605.html