Converting properties to String

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

Converting properties to String

Laurie Harper
I understand how to use the Converter interface with BeanUtils to
convert String data to a target type. How do I do the reverse? In other
words, how to I extend the standard type of X --> String conversions?

I realise I can call getConvertUtils().register(myConverter,
String.class) but that would replace the default to-String conversions,
which is not what I want.

I could get the default converter with
getConvertUtils.lookup(String.class) and pass that into my converter,
allowing me to delegate to it when the source type of the property is
not one I'm interested in, but that seems a little cumbersome.

Is there an easier / better way?

Thanks,

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Converting properties to String

Simon Kitching
On Wed, 2005-06-08 at 23:18 -0400, Laurie Harper wrote:

> I understand how to use the Converter interface with BeanUtils to
> convert String data to a target type. How do I do the reverse? In other
> words, how to I extend the standard type of X --> String conversions?
>
> I realise I can call getConvertUtils().register(myConverter,
> String.class) but that would replace the default to-String conversions,
> which is not what I want.
>
> I could get the default converter with
> getConvertUtils.lookup(String.class) and pass that into my converter,
> allowing me to delegate to it when the source type of the property is
> not one I'm interested in, but that seems a little cumbersome.
>
> Is there an easier / better way?

ConvertUtils really isn't designed to handle custom mappings to string.
It is fundamemtally about mapping strings (from xml input, or from web
forms, etc) to objects. So I don't think there *is* an elegant solution
to your question within ConvertUtils.

If you need a general type-conversion framework you might like to look
at http://morph.sourceforge.net/

Regards,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Converting properties to String

Craig McClanahan-2
Just for the record, this is why the Converter interface in JavaServer
Faces is explicitly two-way:

    public interface Converter {
        public Object getAsObject(FacesContext context, UIComponent
component, String value);
        public String getAsString(FacesContext context, UIComponent
component, Object value);
    }

When originally conceived, ConvertUtils was only concerned with the
String->Object half.

Craig

On 6/8/05, Simon Kitching <[hidden email]> wrote:

> On Wed, 2005-06-08 at 23:18 -0400, Laurie Harper wrote:
> > I understand how to use the Converter interface with BeanUtils to
> > convert String data to a target type. How do I do the reverse? In other
> > words, how to I extend the standard type of X --> String conversions?
> >
> > I realise I can call getConvertUtils().register(myConverter,
> > String.class) but that would replace the default to-String conversions,
> > which is not what I want.
> >
> > I could get the default converter with
> > getConvertUtils.lookup(String.class) and pass that into my converter,
> > allowing me to delegate to it when the source type of the property is
> > not one I'm interested in, but that seems a little cumbersome.
> >
> > Is there an easier / better way?
>
> ConvertUtils really isn't designed to handle custom mappings to string.
> It is fundamemtally about mapping strings (from xml input, or from web
> forms, etc) to objects. So I don't think there *is* an elegant solution
> to your question within ConvertUtils.
>
> If you need a general type-conversion framework you might like to look
> at http://morph.sourceforge.net/
>
> Regards,
>
> Simon
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Converting properties to String

Laurie Harper
Craig McClanahan wrote:
> When originally conceived, ConvertUtils was only concerned with the
> String->Object half.

Yeah, that was the feeling I had... Time for a little software
re-purposing :)

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Converting properties to String

Laurie Harper
In reply to this post by Simon Kitching
Simon Kitching wrote:
> ConvertUtils really isn't designed to handle custom mappings to string.
> It is fundamemtally about mapping strings (from xml input, or from web
> forms, etc) to objects. So I don't think there *is* an elegant solution
> to your question within ConvertUtils.
>
> If you need a general type-conversion framework you might like to look
> at http://morph.sourceforge.net/

Thanks Simon, I'll check that out.

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]