341726 JSONPojoConverter handles characters

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2960 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2011-04-04 06:34:55 +00:00
parent 2ca6b4521b
commit b9db79a381
3 changed files with 46 additions and 17 deletions

View File

@ -23,6 +23,7 @@ jetty-7.4.0-SNAPSHOT
+ 341439 Blocking HttpClient does not use soTimeout for timeouts
+ 341561 Exception when adding o.e.j.s.DoSFilter as managed attribute
+ 341736 Split jetty-nested out of war module
+ 341726 JSONPojoConverter handles characters
+ JETTY-1245 Pooled Buffers implementation
+ JETTY-1354 Added jetty-nested
+ Ensure generated fragment names are unique

View File

@ -37,7 +37,6 @@ import org.eclipse.jetty.util.log.Log;
*/
public class JSONPojoConvertor implements JSON.Convertor
{
public static final Object[] GETTER_ARG = new Object[]{}, NULL_ARG = new Object[]{null};
private static final Map<Class<?>, NumberType> __numberTypes = new HashMap<Class<?>, NumberType>();
@ -245,7 +244,7 @@ public class JSONPojoConvertor implements JSON.Convertor
public static class Setter
{
protected String _propertyName;
protected Method _method;
protected Method _setter;
protected NumberType _numberType;
protected Class<?> _type;
protected Class<?> _componentType;
@ -253,7 +252,7 @@ public class JSONPojoConvertor implements JSON.Convertor
public Setter(String propertyName, Method method)
{
_propertyName = propertyName;
_method = method;
_setter = method;
_type = method.getParameterTypes()[0];
_numberType = __numberTypes.get(_type);
if(_numberType==null && _type.isArray())
@ -270,7 +269,7 @@ public class JSONPojoConvertor implements JSON.Convertor
public Method getMethod()
{
return _method;
return _setter;
}
public NumberType getNumberType()
@ -297,7 +296,7 @@ public class JSONPojoConvertor implements JSON.Convertor
IllegalAccessException, InvocationTargetException
{
if(value==null)
_method.invoke(obj, NULL_ARG);
_setter.invoke(obj, NULL_ARG);
else
invokeObject(obj, value);
}
@ -309,13 +308,17 @@ public class JSONPojoConvertor implements JSON.Convertor
if (_type.isEnum())
{
if (value instanceof Enum)
_method.invoke(obj, new Object[]{value});
_setter.invoke(obj, new Object[]{value});
else
_method.invoke(obj, new Object[]{Enum.valueOf((Class<? extends Enum>)_type,value.toString())});
_setter.invoke(obj, new Object[]{Enum.valueOf((Class<? extends Enum>)_type,value.toString())});
}
else if(_numberType!=null && value instanceof Number)
{
_method.invoke(obj, new Object[]{_numberType.getActualValue((Number)value)});
_setter.invoke(obj, new Object[]{_numberType.getActualValue((Number)value)});
}
else if (Character.TYPE.equals(_type) || Character.class.equals(_type))
{
_setter.invoke(obj, new Object[]{String.valueOf(value).charAt(0)});
}
else if(_componentType!=null && value.getClass().isArray())
{
@ -331,10 +334,10 @@ public class JSONPojoConvertor implements JSON.Convertor
{
// unusual array with multiple types
Log.ignore(e);
_method.invoke(obj, new Object[]{value});
_setter.invoke(obj, new Object[]{value});
return;
}
_method.invoke(obj, new Object[]{array});
_setter.invoke(obj, new Object[]{array});
}
else
{
@ -349,14 +352,14 @@ public class JSONPojoConvertor implements JSON.Convertor
{
// unusual array with multiple types
Log.ignore(e);
_method.invoke(obj, new Object[]{value});
_setter.invoke(obj, new Object[]{value});
return;
}
_method.invoke(obj, new Object[]{array});
_setter.invoke(obj, new Object[]{array});
}
}
else
_method.invoke(obj, new Object[]{value});
_setter.invoke(obj, new Object[]{value});
}
}

View File

@ -45,6 +45,8 @@ public class JSONPojoConvertorTest
foo._float2 = new Float(10.22f);
foo._double1 = 10000.11111d;
foo._double2 = new Double(10000.22222d);
foo._char1='a';
foo._char2=new Character('b');
Bar bar = new Bar("Hello", true, new Baz("World", Boolean.FALSE, foo), new Baz[]{
new Baz("baz0", Boolean.TRUE, null), new Baz("baz1", Boolean.FALSE, null)
@ -52,7 +54,6 @@ public class JSONPojoConvertorTest
bar.setColor(Color.Green);
String s = json.toJSON(bar);
System.err.println(s);
Object obj = json.parse(new JSON.StringSource(s));
@ -64,7 +65,7 @@ public class JSONPojoConvertorTest
Foo f = bz.getFoo();
assertEquals(f, foo);
assertEquals(foo, f);
assertTrue(br.getBazs().length==2);
assertEquals(br.getBazs()[0].getMessage(), "baz0");
assertEquals(br.getBazs()[1].getMessage(), "baz1");
@ -92,6 +93,8 @@ public class JSONPojoConvertorTest
foo._float2 = new Float(10.22f);
foo._double1 = 10000.11111d;
foo._double2 = new Double(10000.22222d);
foo._char1='a';
foo._char2=new Character('b');
Bar bar = new Bar("Hello", true, new Baz("World", Boolean.FALSE, foo));
// bar.setColor(Color.Blue);
@ -291,6 +294,8 @@ public class JSONPojoConvertorTest
private Float _float2;
private double _double1;
private Double _double2;
private char _char1;
private Character _char2;
public Foo()
{
@ -309,7 +314,9 @@ public class JSONPojoConvertorTest
.append("\nfloat1: ").append(_float1)
.append("\nfloat2: ").append(_float2)
.append("\ndouble1: ").append(_double1)
.append("\ndouble2: ").append(_double2)
.append("\ndouble2: ").append(_double2)
.append("\nchar1: ").append(_char1)
.append("\nchar2: ").append(_char2)
.toString();
}
@ -327,7 +334,9 @@ public class JSONPojoConvertorTest
&& getFloat1()==foo.getFloat1()
&& getFloat2().equals(foo.getFloat2())
&& getDouble1()==foo.getDouble1()
&& getDouble2().equals(foo.getDouble2());
&& getDouble2().equals(foo.getDouble2())
&& getChar1()==foo.getChar1()
&& getChar2().equals(foo.getChar2());
}
return false;
@ -405,6 +414,22 @@ public class JSONPojoConvertorTest
{
_double2 = double2;
}
public char getChar1()
{
return _char1;
}
public void setChar1(char char1)
{
_char1 = char1;
}
public Character getChar2()
{
return _char2;
}
public void setChar2(Character char2)
{
_char2 = char2;
}
}