[prev in list] [next in list] [prev in thread] [next in thread] 

List:       struts-user
Subject:    Re: JSON plugin suggest : parse String value "" to Number with null / 0
From:       Knight Chen <dolt131943 () gmail ! com>
Date:       2010-11-25 6:21:27
Message-ID: AANLkTikVJA=M1PVbfot73uEgFPyGdObQkkm0GZxR4iUa () mail ! gmail ! com
[Download RAW message or body]

Does this possible?

In source code:
Object org.apache.struts2.json.JSONPopulator.convertPrimitive(Class
clazz, Object value, Method method) throws JSONException {
        if (value == null) {
            if (Short.TYPE.equals(clazz) || Short.class.equals(clazz))
                return (short) 0;
            else if (Byte.TYPE.equals(clazz) || Byte.class.equals(clazz))
                return (byte) 0;
            else if (Integer.TYPE.equals(clazz) || Integer.class.equals(clazz))
                return 0;
            else if (Long.TYPE.equals(clazz) || Long.class.equals(clazz))
                return 0L;
            else if (Float.TYPE.equals(clazz) || Float.class.equals(clazz))
                return 0f;
            else if (Double.TYPE.equals(clazz) || Double.class.equals(clazz))
                return 0d;
            else if (Boolean.TYPE.equals(clazz) || Boolean.class.equals(clazz))
                return Boolean.FALSE;
            else
                return null;
        } else if …

I suggest change the first line to (or other more reasonable code like this) :
If (value == null || ( !String.class.equals(clazz) && (value
instanceof String) && ("".equals(value)))) {
...


this can process empty string "" to a right value, and no throw a
Exception : Number formcat exception for input string"" .

does it more smart?

sorry for my poor english.


Regards.


On Thu, Nov 25, 2010 at 12:43 AM, Maurizio Cucchiara
<maurizio.cucchiara@gmail.com> wrote:
> 2010/11/24 Martin Gainty <mgainty@hotmail.com>
>
>> if the <target> attribute is available in object acquired by
>> invocation.getAction() then you should see the target
>>
>> does this answer your question?
>>
>
> Actually, It doesn't.
> I meant that conversion methods like convertPrimitive (see the code below)
>  use to take class type and value as argument and they always try to
> understand the kind of type they're facing, choosing by value (for this
> reason I called "value driven"). Why don't they choose conversion by type
> destination?
>
>
> private Object convertPrimitive(Class clazz, Object value, Method method)
> throws JSONException {
>        if (value == null) {
>            if (Short.TYPE.equals(clazz) || Short.class.equals(clazz))
>                return (short) 0;
>            ...
>        } else if (value instanceof Number) {
>            Number number = (Number) value;
>
>
> --
> Maurizio Cucchiara
>



-- 
========================
Knight Chen

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic