struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Gainty <mgai...@hotmail.com>
Subject RE: JSON plugin suggest : parse String value "" to Number with null / 0
Date Wed, 24 Nov 2010 14:35:24 GMT

driven off the Introspector.getBeanInfo 
    public void populateObject(Object object, final Map elements) throws IllegalAccessException,
            InvocationTargetException, NoSuchMethodException, IntrospectionException,
            IllegalArgumentException, JSONException, InstantiationException {
        Class clazz = object.getClass();

        BeanInfo info = Introspector.getBeanInfo(clazz);

from the object parameter sent as first parameter to populateObject e.g.

            rootObject = invocation.getAction();
        
        if ((contentType != null) && contentType.equalsIgnoreCase("application/json"))
{
            // load JSON object
            Object obj = JSONUtil.deserialize(request.getReader());
            if (obj instanceof Map) {
                Map json = (Map) obj;
                // clean up the values
                if (dataCleaner != null)
                    dataCleaner.clean("", json);

                // populate fields
                populator.populateObject(rootObject, json);

if the <target> attribute is available in object acquired by invocation.getAction()
then you should see the target

does this answer your question?

Happy Thanksgiving to one and all
Martin 
______________________________________________ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten
wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist
unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet
keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen
wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire
prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe
quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information
seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les
email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune
responsabilité pour le contenu fourni.




> Date: Wed, 24 Nov 2010 20:16:45 +0800
> Subject: Re: JSON plugin suggest : parse String value "" to Number with null / 0
> From: dolt131943@gmail.com
> To: user@struts.apache.org
> 
> Thanks,Maurizio,
> 
> This is not a wrong approach.
> but can be take a improved for easier use. :P
> 
> Regards.
> 
> 
> On Tue, Nov 23, 2010 at 12:10 PM, Maurizio Cucchiara
> <maurizio.cucchiara@gmail.com> wrote:
> > Hi Knight,
> > I take your point, JSONPopulator.convertPrimitive function maybe uses a
> > wrong approach.
> > Does anyone know why convert method process is driven by value and type
> > instead of target type only?
> >
> > 2010/11/23 Knight Chen <dolt131943@gmail.com>
> >
> >> Hi All,
> >> Thanks for reply, and sorry for my poor English.
> >>
> >> In this case:
> >>
> >> I have a POJO:
> >> public class UserDTO {
> >>      private int age;
> >>      ...
> >>      public void setAge(int age) {
> >>            this.age = age;
> >>      }
> >> }
> >>
> >>
> >> And Action has a property with this type:
> >>
> >> public Class UserAction {
> >>      private UserDTO userInfo;
> >>
> >>      ...
> >>      public void setUserInfo(UserDTO userInfo {
> >>            this.userInfo = userInfo;
> >>      }
> >>
> >>      ...
> >> }
> >>
> >> when I use ajax request and JSON format to transfer this UserDTO obj,
> >> this UserDTO is Serialization from html page's text input field, and
> >> age input field is empty(user not enter and business logic allow
> >> this),
> >> then this JSON String will be like this:
> >> {'userInfo' : { 'age' : ''}}
> >>
> >> so, when use JSON plugin to parse this JSON string to UserAction's
> >> userInfo property,there will be a Number format exception ( for input
> >> String "")
> >>
> >> I check JSONPopulator.java , it has process null value for int(or
> >> other number primitive type) with default value 0,
> >> I think if it can parse the "" to int/Integer(or other number type)
> >> with 0/null like the same process above mentioned,
> >> if do this will easier the front process, because front JS code will
> >> not to check the age's type,and transform the empty text input value
> >> "" to null.
> >>
> >> Just a suggeset.
> >>
> >> Thanks all.
> >>
> >> Regards,
> >>
> >> On Tue, Nov 23, 2010 at 9:46 AM, Maurizio Cucchiara
> >> <maurizio.cucchiara@gmail.com> wrote:
> >> > I'm not sure understand what you mean, it could be ognl conversion
> >> related.
> >> > For further details, could you provide the NPE stack?
> >> >
> >> > 2010/11/22 Knight Chen <dolt131943@gmail.com>
> >> >
> >> >> Hi all,
> >> >>
> >> >> I often encounter Number format exception when I use JSON plugin to
> >> >> parse input String "" from AJAX request to Number,
> >> >> because in Web front,  that will be a zero length String "" transfer
> >> >> to backend if the text input has no enter,
> >> >> so will encounter the Number format exception for input String "".
> >> >>
> >> >> I check the JSONPopulator, that has process the null value to 0 when
> >> >> parse to Number type, but not process the String "",
> >> >> So can JSONPopulator to support process String "" to null or 0,that
> >> >> will be easier web front develop.
> >> >>
> >> >>
> >> >> --
> >> >> ========================
> >> >> Knight Chen
> >> >>
> >> >> ---------------------------------------------------------------------
> >> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> >> For additional commands, e-mail: user-help@struts.apache.org
> >> >>
> >> >>
> >> >
> >> >
> >> > --
> >> > Maurizio Cucchiara
> >> >
> >>
> >>
> >>
> >> --
> >> ========================
> >> Knight Chen
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail: user-help@struts.apache.org
> >>
> >>
> >
> >
> > --
> > Maurizio Cucchiara
> >
> 
> 
> 
> -- 
> ========================
> Knight Chen
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
 		 	   		  
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message