[SoapRMI] writing (de)serializer
Aleksander Slominski
aslom_at_cs.indiana.edu
Sun, 24 Nov 2002 13:52:09 -0500
"nodep_at_libero.it" wrote:
> I'm trying to understand on how to write a
> (de)serializer for xsoap: can you tell me
> the steps I've to follow?(i.e. write "readObject"
> and "writeObject" method,
hi,
there are two parts: serializer and deserializer.
it is easier to write serializer: you essentially need just to
convert input Object into XML representation - to do this
you need to implement this method (look on BigDecimalHandler.java
as a good example of value object serialization i.e. without
support for multiref href="")
public void writeObject(
SerializeContext sctx,
EncodingStyle enc,
Object o,
String name,
Class baseClass,
String id)
throws SerializeException, XmlMapException, IOException
what matters is to write into sctx and more precisely into
associated Writers:
Writer out = sctx.getWriter();
the one nice thing is to check if current SoapStyle requires writing
xsi:type and write it if needed, ex:
SoapStyle style = sctx.getSoapStyle();
if(style.XSI_TYPED) sctx.writeXsdType("decimal");
more tricky is deserialization as you need to do XML parsing
however as XSOAP is using XML pull parser (XPP2)
it is rather straightforward, read more about XPP2 API at:
http://www.javaworld.com/javaworld/jw-03-2002/jw-0329-xmljava2-p2.html
you could even use the wrapper from the article but i recommend
to do parsing directly with XPP2 - it is really straight forward:
imagine input <bigdecimal>321320312123123</bigdecimal>
to deserialize you need to do this (using BigDecimal as an example):
1. read or skip start tag
if(pp.next() != XmlPullParser.CONTENT) {
throw new DeserializeException("expected element content"
+pp.getPosDesc());
}
2. read string content ("321320312123123")
String value = pp.readContent();
3. convert content into BigDecimal
(it is good to give very prcise error messages that include XML position):
BigDecimal bigDecimal;
try {
bigDecimal = new BigDecimal(value);
} catch(Exception ex) {
throw new DeserializeException("can't parse BigDecimal value
'"+value+"'"+pp.getPosDesc(), ex);
}
4. skip end tag
if(pp.next() != XmlPullParser.END_TAG)
throw new DeserializeException("expected end tag"+pp.getPosDesc());
5. return converted value:
return bigDecimal;
that is it :-)
> how register the new
> serializer (I mean, how can xsoap to know
> that new serializer exists and to use?))
you will need ot register it with SoapEnc class, example:
BigDecimalHandler decimalHandler = new BigDecimalHandler();
SoapEnc.getDefault().registerClassEncodingHandler(
BigDecimal.class,
decimalHandler,
decimalHandler);
thanks,
alek
--
The ancestor of every action is a thought. - Ralph Waldo Emerson