JETTY-1283 JSONPojoConvertorFactory can turn off fromJSON

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2305 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2010-09-28 13:19:05 +00:00
parent 25446eb221
commit de0b7d1a03
3 changed files with 78 additions and 4 deletions

View File

@ -51,8 +51,9 @@ jetty-7.2-SNAPSHOT
+ JETTY-1269 Improve log multithreadedness
+ JETTY-1270 Websocket closed endp protection
+ JETTY-1271 handled unavailable exception
+ JETTY-1279 Make jetty-plus.xml enable plus features for all webapps by default
+ JETTY-1281 Create new session after authentication
+ JETTY-1297 Make jetty-plus.xml enable plus features for all webapps by default
+ JETTY-1283 JSONPojoConvertorFactory can turn off fromJSON
+ Fix jetty-plus.xml for new configuration names
+ Added ignore to Logger interface
+ Improved debug dump

View File

@ -21,7 +21,8 @@ import org.eclipse.jetty.util.ajax.JSON.Output;
public class JSONPojoConvertorFactory implements JSON.Convertor
{
private JSON _json=null;
private final JSON _json;
private final boolean _fromJson;
public JSONPojoConvertorFactory(JSON json)
{
@ -30,8 +31,27 @@ public class JSONPojoConvertorFactory implements JSON.Convertor
throw new IllegalArgumentException();
}
_json=json;
_fromJson=true;
}
/* ------------------------------------------------------------ */
/**
* @param json The JSON instance to use
* @param fromJSON If true, the class name of the objects is included
* in the generated JSON and is used to instantiate the object when
* JSON is parsed (otherwise a Map is used).
*/
public JSONPojoConvertorFactory(JSON json,boolean fromJSON)
{
if (json==null)
{
throw new IllegalArgumentException();
}
_json=json;
_fromJson=fromJSON;
}
/* ------------------------------------------------------------ */
public void toJSON(Object obj, Output out)
{
String clsName=obj.getClass().getName();
@ -41,7 +61,7 @@ public class JSONPojoConvertorFactory implements JSON.Convertor
try
{
Class cls=Loader.loadClass(JSON.class,clsName);
convertor=new JSONPojoConvertor(cls);
convertor=new JSONPojoConvertor(cls,_fromJson);
_json.addConvertorFor(clsName, convertor);
}
catch (ClassNotFoundException e)
@ -67,7 +87,7 @@ public class JSONPojoConvertorFactory implements JSON.Convertor
try
{
Class cls=Loader.loadClass(JSON.class,clsName);
convertor=new JSONPojoConvertor(cls);
convertor=new JSONPojoConvertor(cls,_fromJson);
_json.addConvertorFor(clsName, convertor);
}
catch (ClassNotFoundException e)

View File

@ -16,6 +16,9 @@ package org.eclipse.jetty.util.ajax;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.List;
import java.util.Map;
import org.junit.Test;
@ -69,6 +72,56 @@ public class JSONPojoConvertorFactoryTest
assertEquals(br.getBazs()[1].getMessage(), "baz1");
assertEquals(Color.Green,br.getColor());
}
@Test
public void testFoo2Map()
{
JSON jsonOut = new JSON();
JSON jsonIn = new JSON();
jsonOut.addConvertor(Object.class, new JSONPojoConvertorFactory(jsonOut,false));
jsonOut.addConvertor(Enum.class, new JSONEnumConvertor());
jsonIn.addConvertor(Object.class, new JSONPojoConvertorFactory(jsonIn,false));
jsonIn.addConvertor(Enum.class, new JSONEnumConvertor());
Foo foo = new Foo();
foo._name = "Foo @ " + System.currentTimeMillis();
foo._int1 = 1;
foo._int2 = new Integer(2);
foo._long1 = 1000001l;
foo._long2 = new Long(1000002l);
foo._float1 = 10.11f;
foo._float2 = new Float(10.22f);
foo._double1 = 10000.11111d;
foo._double2 = new Double(10000.22222d);
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 = jsonOut.toJSON(bar);
System.err.println(s);
assertTrue(s.indexOf("class")<0);
Object obj = jsonIn.parse(new JSON.StringSource(s));
assertTrue(obj instanceof Map);
Map<String,Object> br = (Map<String,Object>)obj;
Map<String,Object> bz = (Map<String,Object>)br.get("baz");
Map<String,Object> f = (Map<String,Object>)bz.get("foo");
Object[] bazs = (Object[])br.get("bazs");
assertTrue(bazs.length==2);
assertEquals(((Map)bazs[0]).get("message"), "baz0");
assertEquals(((Map)bazs[1]).get("message"), "baz1");
assertEquals("Green",br.get("color"));
}
enum Color { Red, Green, Blue };