Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 341726

Summary: JSONPojoConverter doesn't handle char/Character fields on fromJSON
Product: [RT] Jetty Reporter: David Wegener <dpwegener>
Component: otherAssignee: Project Inbox <jetty-inbox>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: gregw
Version: unspecified   
Target Milestone: 7.2.x   
Hardware: PC   
OS: Linux   
Whiteboard:
Attachments:
Description Flags
Modified JSONPojoConvert with Setter.invokeObject modified to deal with char/Character fields none

Description David Wegener CLA 2011-04-03 14:58:45 EDT
Build Identifier: 7.3.1.v20110307

The JSONPojoConverter class isn't able to convert a char or Character fields when de-serializing JSON strings.  Characters are sent as strings.  The JSONPojoConverter.Setter class doesn't take this into account when invoking the setter method for the character field.  The Setter.invokeObject passes the field value as a String to the setter method.  The String needs to be converted to a char prior to invoking the setter method of the target object.

Reproducible: Always

Steps to Reproduce:
1.Define a class that includes a char or Character field.  Make sure that the class has a getter/setter for the field.
2. Register the JSONPojoConverter as the converter for the class.
3. Create an instance of the class and set the fields.  Use JSON.toString to convert the instance to a string.
4. Use a JSON.parse method to convert back to an object.

When the character field is encountered an InvocationTargetException because a String is passed instead of a char/Character to the set method.

Here is a sample class the demonstrates the problem.  The "type" field is the char field that causes the problem, and here is a sample of the JSON string created for a class instance.
{"class":"com.cboe.flex.translator.ProductRequestStruct","strike":100,"symbol":"IBM","expirationType":1,"priceType":0,"month":"JAN","settlementTime":0,"type":"C"}
public class ProductRequestStruct {
	
	private String symbol;
	private String month;
	private int strike;
	private int priceType;
	private char type;
	private int expirationType;
	private int settlementTime;
	public String getSymbol() {
		return symbol;
	}
	public void setSymbol(String symbol) {
		this.symbol = symbol;
	}
	public String getMonth() {
		return month;
	}
	public void setMonth(String month) {
		this.month = month;
	}
	public int getStrike() {
		return strike;
	}
	public void setStrike(int strike) {
		this.strike = strike;
	}
	public int getPriceType() {
		return priceType;
	}
	public void setPriceType(int priceType) {
		this.priceType = priceType;
	}
	public char getType() {
		return type;
	}
	public void setType(char type) {
		this.type = type;
	}
	public int getExpirationType() {
		return expirationType;
	}
	public void setExpirationType(int expirationType) {
		this.expirationType = expirationType;
	}
	public int getSettlementTime() {
		return settlementTime;
	}
	public void setSettlementTime(int settlementTime) {
		this.settlementTime = settlementTime;
	}
	

}
Comment 1 David Wegener CLA 2011-04-03 15:04:34 EDT
Created attachment 192426 [details]
Modified JSONPojoConvert with Setter.invokeObject modified to deal with char/Character fields

Added a check at the end of the Setter.invokeObject method that checks to see if _type is a char or Character.  If so, use the first character of the value object as the character to set in the field.

Here is the snippet of code that is changed:

            else if (_type.equals(char.class) || _type.equals(Character.class)){
            	char charValue;
            	if (value instanceof String) {
            		charValue = ((String) value).charAt(0);
            	}
            	else {
            		charValue = value.toString().charAt(0);
            	}
        		_method.invoke(obj, new Object[] {charValue});
            }
            else {
                _method.invoke(obj, new Object[]{value});
            }
Comment 2 Greg Wilkins CLA 2011-04-04 02:35:17 EDT
Thanks for the report and patch.

I fixed, but I didn't use your patch because we want to release this week and patches take a little while to get IP clearance.  So I created test harness and did fixed it myself.

r2960