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:
parent
2ca6b4521b
commit
b9db79a381
|
@ -23,6 +23,7 @@ jetty-7.4.0-SNAPSHOT
|
||||||
+ 341439 Blocking HttpClient does not use soTimeout for timeouts
|
+ 341439 Blocking HttpClient does not use soTimeout for timeouts
|
||||||
+ 341561 Exception when adding o.e.j.s.DoSFilter as managed attribute
|
+ 341561 Exception when adding o.e.j.s.DoSFilter as managed attribute
|
||||||
+ 341736 Split jetty-nested out of war module
|
+ 341736 Split jetty-nested out of war module
|
||||||
|
+ 341726 JSONPojoConverter handles characters
|
||||||
+ JETTY-1245 Pooled Buffers implementation
|
+ JETTY-1245 Pooled Buffers implementation
|
||||||
+ JETTY-1354 Added jetty-nested
|
+ JETTY-1354 Added jetty-nested
|
||||||
+ Ensure generated fragment names are unique
|
+ Ensure generated fragment names are unique
|
||||||
|
|
|
@ -37,7 +37,6 @@ import org.eclipse.jetty.util.log.Log;
|
||||||
*/
|
*/
|
||||||
public class JSONPojoConvertor implements JSON.Convertor
|
public class JSONPojoConvertor implements JSON.Convertor
|
||||||
{
|
{
|
||||||
|
|
||||||
public static final Object[] GETTER_ARG = new Object[]{}, NULL_ARG = new Object[]{null};
|
public static final Object[] GETTER_ARG = new Object[]{}, NULL_ARG = new Object[]{null};
|
||||||
private static final Map<Class<?>, NumberType> __numberTypes = new HashMap<Class<?>, NumberType>();
|
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
|
public static class Setter
|
||||||
{
|
{
|
||||||
protected String _propertyName;
|
protected String _propertyName;
|
||||||
protected Method _method;
|
protected Method _setter;
|
||||||
protected NumberType _numberType;
|
protected NumberType _numberType;
|
||||||
protected Class<?> _type;
|
protected Class<?> _type;
|
||||||
protected Class<?> _componentType;
|
protected Class<?> _componentType;
|
||||||
|
@ -253,7 +252,7 @@ public class JSONPojoConvertor implements JSON.Convertor
|
||||||
public Setter(String propertyName, Method method)
|
public Setter(String propertyName, Method method)
|
||||||
{
|
{
|
||||||
_propertyName = propertyName;
|
_propertyName = propertyName;
|
||||||
_method = method;
|
_setter = method;
|
||||||
_type = method.getParameterTypes()[0];
|
_type = method.getParameterTypes()[0];
|
||||||
_numberType = __numberTypes.get(_type);
|
_numberType = __numberTypes.get(_type);
|
||||||
if(_numberType==null && _type.isArray())
|
if(_numberType==null && _type.isArray())
|
||||||
|
@ -270,7 +269,7 @@ public class JSONPojoConvertor implements JSON.Convertor
|
||||||
|
|
||||||
public Method getMethod()
|
public Method getMethod()
|
||||||
{
|
{
|
||||||
return _method;
|
return _setter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NumberType getNumberType()
|
public NumberType getNumberType()
|
||||||
|
@ -297,7 +296,7 @@ public class JSONPojoConvertor implements JSON.Convertor
|
||||||
IllegalAccessException, InvocationTargetException
|
IllegalAccessException, InvocationTargetException
|
||||||
{
|
{
|
||||||
if(value==null)
|
if(value==null)
|
||||||
_method.invoke(obj, NULL_ARG);
|
_setter.invoke(obj, NULL_ARG);
|
||||||
else
|
else
|
||||||
invokeObject(obj, value);
|
invokeObject(obj, value);
|
||||||
}
|
}
|
||||||
|
@ -309,13 +308,17 @@ public class JSONPojoConvertor implements JSON.Convertor
|
||||||
if (_type.isEnum())
|
if (_type.isEnum())
|
||||||
{
|
{
|
||||||
if (value instanceof Enum)
|
if (value instanceof Enum)
|
||||||
_method.invoke(obj, new Object[]{value});
|
_setter.invoke(obj, new Object[]{value});
|
||||||
else
|
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)
|
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())
|
else if(_componentType!=null && value.getClass().isArray())
|
||||||
{
|
{
|
||||||
|
@ -331,10 +334,10 @@ public class JSONPojoConvertor implements JSON.Convertor
|
||||||
{
|
{
|
||||||
// unusual array with multiple types
|
// unusual array with multiple types
|
||||||
Log.ignore(e);
|
Log.ignore(e);
|
||||||
_method.invoke(obj, new Object[]{value});
|
_setter.invoke(obj, new Object[]{value});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_method.invoke(obj, new Object[]{array});
|
_setter.invoke(obj, new Object[]{array});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -349,14 +352,14 @@ public class JSONPojoConvertor implements JSON.Convertor
|
||||||
{
|
{
|
||||||
// unusual array with multiple types
|
// unusual array with multiple types
|
||||||
Log.ignore(e);
|
Log.ignore(e);
|
||||||
_method.invoke(obj, new Object[]{value});
|
_setter.invoke(obj, new Object[]{value});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_method.invoke(obj, new Object[]{array});
|
_setter.invoke(obj, new Object[]{array});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
_method.invoke(obj, new Object[]{value});
|
_setter.invoke(obj, new Object[]{value});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,8 @@ public class JSONPojoConvertorTest
|
||||||
foo._float2 = new Float(10.22f);
|
foo._float2 = new Float(10.22f);
|
||||||
foo._double1 = 10000.11111d;
|
foo._double1 = 10000.11111d;
|
||||||
foo._double2 = new Double(10000.22222d);
|
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[]{
|
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)
|
new Baz("baz0", Boolean.TRUE, null), new Baz("baz1", Boolean.FALSE, null)
|
||||||
|
@ -52,7 +54,6 @@ public class JSONPojoConvertorTest
|
||||||
bar.setColor(Color.Green);
|
bar.setColor(Color.Green);
|
||||||
|
|
||||||
String s = json.toJSON(bar);
|
String s = json.toJSON(bar);
|
||||||
System.err.println(s);
|
|
||||||
|
|
||||||
Object obj = json.parse(new JSON.StringSource(s));
|
Object obj = json.parse(new JSON.StringSource(s));
|
||||||
|
|
||||||
|
@ -64,7 +65,7 @@ public class JSONPojoConvertorTest
|
||||||
|
|
||||||
Foo f = bz.getFoo();
|
Foo f = bz.getFoo();
|
||||||
|
|
||||||
assertEquals(f, foo);
|
assertEquals(foo, f);
|
||||||
assertTrue(br.getBazs().length==2);
|
assertTrue(br.getBazs().length==2);
|
||||||
assertEquals(br.getBazs()[0].getMessage(), "baz0");
|
assertEquals(br.getBazs()[0].getMessage(), "baz0");
|
||||||
assertEquals(br.getBazs()[1].getMessage(), "baz1");
|
assertEquals(br.getBazs()[1].getMessage(), "baz1");
|
||||||
|
@ -92,6 +93,8 @@ public class JSONPojoConvertorTest
|
||||||
foo._float2 = new Float(10.22f);
|
foo._float2 = new Float(10.22f);
|
||||||
foo._double1 = 10000.11111d;
|
foo._double1 = 10000.11111d;
|
||||||
foo._double2 = new Double(10000.22222d);
|
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 bar = new Bar("Hello", true, new Baz("World", Boolean.FALSE, foo));
|
||||||
// bar.setColor(Color.Blue);
|
// bar.setColor(Color.Blue);
|
||||||
|
@ -291,6 +294,8 @@ public class JSONPojoConvertorTest
|
||||||
private Float _float2;
|
private Float _float2;
|
||||||
private double _double1;
|
private double _double1;
|
||||||
private Double _double2;
|
private Double _double2;
|
||||||
|
private char _char1;
|
||||||
|
private Character _char2;
|
||||||
|
|
||||||
public Foo()
|
public Foo()
|
||||||
{
|
{
|
||||||
|
@ -310,6 +315,8 @@ public class JSONPojoConvertorTest
|
||||||
.append("\nfloat2: ").append(_float2)
|
.append("\nfloat2: ").append(_float2)
|
||||||
.append("\ndouble1: ").append(_double1)
|
.append("\ndouble1: ").append(_double1)
|
||||||
.append("\ndouble2: ").append(_double2)
|
.append("\ndouble2: ").append(_double2)
|
||||||
|
.append("\nchar1: ").append(_char1)
|
||||||
|
.append("\nchar2: ").append(_char2)
|
||||||
.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -327,7 +334,9 @@ public class JSONPojoConvertorTest
|
||||||
&& getFloat1()==foo.getFloat1()
|
&& getFloat1()==foo.getFloat1()
|
||||||
&& getFloat2().equals(foo.getFloat2())
|
&& getFloat2().equals(foo.getFloat2())
|
||||||
&& getDouble1()==foo.getDouble1()
|
&& getDouble1()==foo.getDouble1()
|
||||||
&& getDouble2().equals(foo.getDouble2());
|
&& getDouble2().equals(foo.getDouble2())
|
||||||
|
&& getChar1()==foo.getChar1()
|
||||||
|
&& getChar2().equals(foo.getChar2());
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -405,6 +414,22 @@ public class JSONPojoConvertorTest
|
||||||
{
|
{
|
||||||
_double2 = double2;
|
_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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue