299455 Enum support in JSONPojoConvertor

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1195 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2010-01-14 01:42:38 +00:00
parent 3987ed20aa
commit 8b59dcf1bf
3 changed files with 15 additions and 7 deletions

View File

@ -35,7 +35,7 @@ public class ArrayQueue<E> extends AbstractList<E> implements Queue<E>
protected Object[] _elements;
protected int _nextE;
protected int _nextSlot;
protected int _size;
protected volatile int _size;
protected int _growCapacity;
/* ------------------------------------------------------------ */

View File

@ -222,7 +222,8 @@ public class JSONPojoConvertor implements JSON.Convertor
{
Log.ignore(t);
}
/* ------------------------------------------------------------ */
public static class Setter
{
protected String _propertyName;
@ -286,9 +287,13 @@ public class JSONPojoConvertor implements JSON.Convertor
protected void invokeObject(Object obj, Object value) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException
{
if (_type.isEnum())
{
_method.invoke(obj, new Object[]{Enum.valueOf((Class<? extends Enum>)_type,value.toString())});
if (value instanceof Enum)
_method.invoke(obj, new Object[]{value});
else
_method.invoke(obj, new Object[]{Enum.valueOf((Class<? extends Enum>)_type,value.toString())});
}
else if(_numberType!=null && value instanceof Number)
{

View File

@ -22,13 +22,13 @@ import junit.framework.TestCase;
*/
public class JSONPojoConvertorTest extends TestCase
{
public void testFoo()
{
JSON json = new JSON();
json.addConvertor(Foo.class, new JSONPojoConvertor(Foo.class));
json.addConvertor(Bar.class, new JSONPojoConvertor(Bar.class));
json.addConvertor(Baz.class, new JSONPojoConvertor(Baz.class));
// json.addConvertor(Enum.class, new JSONEnumConvertor(true));
Foo foo = new Foo();
foo._name = "Foo @ " + System.currentTimeMillis();
@ -44,8 +44,10 @@ public class JSONPojoConvertorTest extends TestCase
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)
});
bar.setColor(Color.Green);
String s = json.toJSON(bar);
System.err.println(s);
Object obj = json.parse(new JSON.StringSource(s));
@ -61,6 +63,7 @@ public class JSONPojoConvertorTest extends TestCase
assertTrue(br.getBazs().length==2);
assertEquals(br.getBazs()[0].getMessage(), "baz0");
assertEquals(br.getBazs()[1].getMessage(), "baz1");
assertEquals(Color.Green,br.getColor());
}
public void testExclude()
@ -85,7 +88,7 @@ public class JSONPojoConvertorTest extends TestCase
foo._double2 = new Double(10000.22222d);
Bar bar = new Bar("Hello", true, new Baz("World", Boolean.FALSE, foo));
bar.setColor(Color.Blue);
// bar.setColor(Color.Blue);
String s = json.toJSON(bar);
Object obj = json.parse(new JSON.StringSource(s));
@ -105,7 +108,7 @@ public class JSONPojoConvertorTest extends TestCase
assertNull(f.getInt2());
assertFalse(foo.getInt2().equals(f.getInt2()));
assertNull(f.getName());
assertEquals(Color.Blue,br.getColor());
assertEquals(null,br.getColor());
}
enum Color { Red, Green, Blue };
@ -116,7 +119,7 @@ public class JSONPojoConvertorTest extends TestCase
private Baz _baz;
private boolean _boolean1;
private Baz[] _bazs;
private Color _color=Color.Red;
private Color _color;
public Bar()
{