construction & class unmarshaling

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

construction & class unmarshaling

cruisercoder
I have a custom converter and I want xstream to unmarshall a sub-element into a
type using context.convertAnother() and having xstream preform the normal class
member unmarshalling,  but I want to create the object rather than having
xstream do it so that I can call a constructor.  This class has a field that can
only be set by calling a single argument constructor. Is there a way to do this?

erik

Reply | Threaded
Open this post in threaded view
|

Re: construction & class unmarshaling

Jörg Schaible-2
Hi Erik,

Erik Smith wrote:

> I have a custom converter and I want xstream to unmarshall a sub-element
> into a type using context.convertAnother() and having xstream preform the
> normal class
> member unmarshalling,  but I want to create the object rather than having
> xstream do it so that I can call a constructor.  This class has a field
> that can only be set by calling a single argument constructor. Is there a
> way to do this?

Have a look in our code, here the converter for javax.security.auth.Subject:
http://svn.xstream.codehaus.org/trunk/xstream/src/java/com/thoughtworks/xstream/converters/extended/SubjectConverter.java?rev=650&view=markup

The Subject instance is created manually in the unmarshal method, but the
members of the class are unmarshalled using the standrad mechanism.

- Jörg

Reply | Threaded
Open this post in threaded view
|

Re: construction & class unmarshaling

cruisercoder
> Have a look in our code, here the converter for javax.security.auth.Subject:
>
http://svn.xstream.codehaus.org/trunk/xstream/src/java/com/thoughtworks/xstream/converters/extended/SubjectConverter.java?rev=650&view=markup
>
> The Subject instance is created manually in the unmarshal method, but the
> members of the class are unmarshalled using the standrad mechanism.
>
> - Jörg


I think this is not exactly what I'm looking for.  In this case you are creating
the Security class directly, but you are using the readItem method to populate a
set.    What I need is something like this:

// in converter...

MyClass my = new MyClass("one");

// now do something like context.convertAnother(null, MyClass.type)
//   to preform class member population, but use the object that I
//   created rather than having xstream create one internally


erik

Reply | Threaded
Open this post in threaded view
|

RE: Re: construction & class unmarshaling

Jörg Schaible
In reply to this post by cruisercoder
Hi Erik,

Erik Smith wrote on Friday, January 20, 2006 10:40 PM:

[snip]

> I think this is not exactly what I'm looking for.  In this
> case you are creating
> the Security class directly, but you are using the readItem
> method to populate a
> set.    What I need is something like this:
>
> // in converter...
>
> MyClass my = new MyClass("one");
>
> // now do something like context.convertAnother(null, MyClass.type)
> //   to preform class member population, but use the object that I
> //   created rather than having xstream create one internally

Then I don't understand your use case in total. IMHO the Subject converter does exactly what you describe here.

- Jörg