About delegating to other converters

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

About delegating to other converters

Jesus Salvador Ramos Cardona

Hi all.

 

I have successfully implemented a custom converter that writes XML with attributes rather than tags for a specific bean. This bean has a string property, and two javabean properties. The string prop serializes into an xml attribute just nicely with this converter, and, according to the user list, it is possible to delegate the conversion  of the other two properties to a default converter. How is this done?

 

Below is a snippet from my converter:

 

 

            /** Overriding method

             * @see com.thoughtworks.xstream.converters.Converter#canConvert(java.lang.Class)

             */

            public boolean canConvert(Class type) {

                        return type.equals(SpeiMensaje.class);

            }

 

            /** Overriding method

             * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object, com.thoughtworks.xstream.io.HierarchicalStreamWriter, com.thoughtworks.xstream.converters.MarshallingContext)

             */

            public void marshal(Object speiBean, HierarchicalStreamWriter writer, MarshallingContext ctx) {

                        SpeiMensaje speiMsg = (SpeiMensaje) speiBean;

                        writeAttribute("categoria", speiMsg.getCategoria(), writer);

                        if (speiMsg.getOrdenpago() != null) {

                                   write("ordenpago", speiMsg.getOrdenpago(), writer);

                        }

                        if (speiMsg.getRespuesta() != null) {

                                   write("respuesta", speiMsg.getRespuesta(), writer);

                        }

            }

 

            /** Overriding method

             * @see com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks.xstream.io.HierarchicalStreamReader, com.thoughtworks.xstream.converters.UnmarshallingContext)

             */

            public Object unmarshal(HierarchicalStreamReader arg0, UnmarshallingContext arg1) {

                        return null;

            }

           

    private void write(String fieldName, Object value, HierarchicalStreamWriter writer) {

        writer.startNode(fieldName);

        writer.setValue(value.toString());

        writer.endNode();

    }

 

    private void writeAttribute(String fieldName, String value, HierarchicalStreamWriter writer) {

        writer.addAttribute(fieldName, value);

    }

}

 

Please, help.

 

J.

 

Jesús Ramos Cardona
Desarrollo de Sistemas de Casa de Bolsa

[hidden email][hidden email]

+52 (55) 51 74 22 22 ext. 1250

Ixe Grupo Financiero

Este correo electrónico y sus anexos pueden contener información CONFIDENCIAL para uso exclusivo de su destinatario. Si ha recibido este correo por error, por favor, notifíquelo al remitente y bórrelo de su sistema. Muchas gracias. Las opiniones expresadas en este correo son las de su autor y no son necesariamente compartidas o apoyadas por la compañía. Ixe Grupo Financiero no asume aquí obligaciones ni se responsabiliza del contenido de este correo, a menos que dicha información sea confirmada por escrito por un representante autorizado. No se garantiza que la transmisión de este correo sea segura o libre de errores, podría haber sido viciada, perdida, destruida, haber llegado tarde, de forma incompleta o contener VIRUS.

 

Reply | Threaded
Open this post in threaded view
|

RE: About delegating to other converters

Jörg Schaible
Hi Jesus,

        From: Jesus Salvador Ramos Cardona
        Sent: Tuesday, January 24, 2006 12:19 AM
        To: [hidden email]
        Subject: [xstream-user] About delegating to other converters

        Hi all.

        I have successfully implemented a custom converter that writes XML with attributes rather than tags for a specific bean. This bean has a string property, and two javabean properties. The string prop serializes into an xml attribute just nicely with this converter, and, according to the user list, it is possible to delegate the conversion  of the other two properties to a default converter. How is this done?

Have a look in our code, here the abstract converter for collections:
http://svn.xstream.codehaus.org/trunk/xstream/src/java/com/thoughtworks/xstream/converters/collections/AbstractCollectionConverter.java?rev=513&view=markup
Have a look at the readItem and writeItem methods.

- Jörg