简体   繁体   English

Simple Framework中的自定义匹配器用于反序列化但不能序列化

[英]Custom Matcher in Simple Framework works to deserialize but not serialize

I'm trying to get Simple to play nice with XMLGregorianCalendar. 我试图让Simple更好地使用XMLGregorianCalendar。 In so doing I've created a Matcher and Transform: 这样做我创建了一个Matcher和Transform:

public class MyMatcher implements Matcher {

    @Override
    public Transform<?> match(@SuppressWarnings("rawtypes") final Class type) throws Exception {
        if (type.equals(XMLGregorianCalendar.class)) {
            return new XMLGregorianCalendarTransform();
        }

        return null;
    }

}

and

public class XMLGregorianCalendarTransform implements Transform<XMLGregorianCalendar> {

    @Override
    public XMLGregorianCalendar read(final String value) throws Exception {
        return DatatypeFactory.newInstance().newXMLGregorianCalendar(value);
    }

    @Override
    public String write(final XMLGregorianCalendar value) throws Exception {
        return value.toXMLFormat();
    }

}

When this code is used to deserialize, it works great: 当这段代码用于反序列化时,效果很好:

        final Serializer serializer = new Persister(new MyMatcher());
        obj = serializer.read(type, new ByteArrayInputStream(xml.getBytes(Charset.forName(UTF8_ENCODING))));

Unfortunately when I try to use it to serialize, it doesn't work, it throws the Failed to create xml string for the object. org.simpleframework.xml.transform.TransformException: Transform of class com.sun.org.apache.xerces.internal.jaxp.datatyp e.XMLGregorianCalendarImpl not supported 不幸的是,当我尝试使用它来序列化时,它不起作用,它会抛出Failed to create xml string for the object. org.simpleframework.xml.transform.TransformException: Transform of class com.sun.org.apache.xerces.internal.jaxp.datatyp e.XMLGregorianCalendarImpl not supported Failed to create xml string for the object. org.simpleframework.xml.transform.TransformException: Transform of class com.sun.org.apache.xerces.internal.jaxp.datatyp e.XMLGregorianCalendarImpl not supported error, my Transform's write method is never called (check by break point). Failed to create xml string for the object. org.simpleframework.xml.transform.TransformException: Transform of class com.sun.org.apache.xerces.internal.jaxp.datatyp e.XMLGregorianCalendarImpl not supported错误,我的Transform的write方法永远不会被调用(按断点检查)。

Here's the code I use to serialize: 这是我用来序列化的代码:

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        os = new DataOutputStream(baos);
        final Serializer serializer = new Persister(new MyMatcher());
        serializer.write(this, os);
        os.flush();
        xmlString = new String(baos.toByteArray(), Charset.forName(UTF8_ENCODING));

The write method throws the exception. write方法抛出异常。 I've used Serialization third-party classes with Simple XML (org.simpleframework.xml) as a reference, and it's what got me to the deserialization working, but now I'm stuck. 我已经使用Serial XML第三方类和Simple XML(org.simpleframework.xml)作为参考,这就是让我进行反序列化工作的原因,但现在我被卡住了。 I'm not sure how to proceed. 我不知道该怎么办。

Any help is appreciated. 任何帮助表示赞赏。

It turns out the answer was in the question. 事实证明答案是在问题中。 The error stated XMLGregorianCalendarImpl not supported. 错误声明XMLGregorianCalendarImpl不受支持。 This is not what my Matcher checked for. 这不是我的Matcher检查的内容。 I just had to change: 我只需要改变:

    if (type.equals(XMLGregorianCalendar.class)) {

to

    if (XMLGregorianCalendar.class.isAssignableFrom(type)) {

I hope this helps someone. 我希望这可以帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM