Re: JSON plugin suggest : parse String value to Number with null / 0
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.cucchi...@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 dolt131...@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.cucchi...@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 dolt131...@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-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org -- Maurizio Cucchiara -- Knight Chen - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org -- Maurizio Cucchiara -- Knight Chen - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
RE: JSON plugin suggest : parse String value to Number with null / 0
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: dolt131...@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.cucchi...@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 dolt131...@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.cucchi...@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 dolt131...@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
Re: JSON plugin suggest : parse String value to Number with null / 0
2010/11/24 Martin Gainty mgai...@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
Re: JSON plugin suggest : parse String value to Number with null / 0
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.cucchi...@gmail.com wrote: 2010/11/24 Martin Gainty mgai...@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-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: JSON plugin suggest : parse String value to Number with null / 0
Hello Knight, I think you can change the Value-Object(Model obj), modify the get method code: if(this.value.equal()){ this.value=null; or this.value=0 } You can pass a Value-Object to server side, instead of Json-Text formation. Regards, Mead On Mon, Nov 22, 2010 at 8:12 PM, Knight Chen dolt131...@gmail.com wrote: 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-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: JSON plugin suggest : parse String value to Number with null / 0
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 dolt131...@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-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org -- Maurizio Cucchiara
Re: JSON plugin suggest : parse String value to Number with null / 0
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.cucchi...@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 dolt131...@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-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org -- Maurizio Cucchiara -- Knight Chen - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: JSON plugin suggest : parse String value to Number with null / 0
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 dolt131...@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.cucchi...@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 dolt131...@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-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org -- Maurizio Cucchiara -- Knight Chen - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org -- Maurizio Cucchiara