package flexjson;
import flexjson.factories.DateObjectFactory;
import flexjson.factories.ExistingObjectFactory;
import flexjson.model.Account;
import flexjson.transformer.DateTransformer;
import flexjson.transformer.Transformer;
import flexjson.mock.Person;
import flexjson.mock.*;
import flexjson.mock.superhero.*;
import org.junit.Before;
import org.junit.Test;
import java.awt.geom.Point2D;
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.lang.reflect.Array;
import static org.junit.Assert.*;
public class JSONDeserializerTest {
private static final double DELTA = 0.000000001;
private FixtureCreator creator;
@Before
public void setUp() {
creator = new FixtureCreator();
}
@Test
public void testDeserializeNoIncludes() {
Person charlie = creator.createCharlie();
String json = new JSONSerializer().serialize(charlie);
Person jsonCharlie = new JSONDeserializer<Person>().deserialize(json);
assertNotNull("Make sure we deserialized something non-null", jsonCharlie);
assertEquals(charlie.getLastname(), jsonCharlie.getLastname());
assertEquals(charlie.getFirstname(), jsonCharlie.getFirstname());
assertEquals(charlie.getBirthdate(), jsonCharlie.getBirthdate());
assertEquals(charlie.getHome().getState(), jsonCharlie.getHome().getState());
assertEquals(charlie.getHome().getStreet(), jsonCharlie.getHome().getStreet());
assertEquals(charlie.getHome().getCity(), jsonCharlie.getHome().getCity());
assertEquals(charlie.getWork().getCity(), jsonCharlie.getWork().getCity());
assertEquals(jsonCharlie, jsonCharlie.getWork().getPerson());
}
@Test
public void testDeserializeWithPath() {
Person charlie = creator.createCharlie();
String json = new JSONSerializer().rootName("person").serialize(charlie);
Person jsonCharlie = new JSONDeserializer<Person>().deserialize(json, "person", Person.class );
assertNotNull("Make sure we deserialized something non-null", jsonCharlie);
assertEquals(charlie.getLastname(), jsonCharlie.getLastname());
assertEquals(charlie.getFirstname(), jsonCharlie.getFirstname());
assertEquals(charlie.getBirthdate(), jsonCharlie.getBirthdate());
assertEquals(charlie.getHome().getState(), jsonCharlie.getHome().getState());
assertEquals(charlie.getHome().getStreet(), jsonCharlie.getHome().getStreet());
assertEquals(charlie.getHome().getCity(), jsonCharlie.getHome().getCity());
assertEquals(charlie.getWork().getCity(), jsonCharlie.getWork().getCity());
assertEquals(jsonCharlie, jsonCharlie.getWork().getPerson());
}
@Test
public void testDeserializeWithIncludes() {
Person charlie = creator.createCharlie();
String json = new JSONSerializer().include("phones", "hobbies").serialize(charlie);
Person jsonCharlie = new JSONDeserializer<Person>().deserialize(json);
assertNotNull("Make sure we deserialized something non-null", jsonCharlie);
assertFalse("Make sure that our phones are not empty", jsonCharlie.getPhones().isEmpty());
assertEquals(2, jsonCharlie.getPhones().size());
assertEquals(3, jsonCharlie.getHobbies().size());
assertEquals("Fixing Horse Races", jsonCharlie.getHobbies().get(1));
}
@Test
public void testSubClassDeserialize() {
Employee dilbert = creator.createDilbert();
String json = new JSONSerializer().include("phones", "hobbies").serialize(dilbert);
Person jsonDilbert = new JSONDeserializer<Person>().deserialize(json);
assertNotNull("Make sure we got back dilbert.", jsonDilbert);
assertTrue("Make sure dilbert came back as an employee.", jsonDilbert instanceof Employee);
assertEquals("Make sure dilbert has a company.", dilbert.getCompany(), ((Employee) jsonDilbert).getCompany());
}
@Test
public void testDeserializeInterfaces() {
Hero superman = creator.createSuperman();
String json = new JSONSerializer().include("powers").serialize(superman);
Hero jsonSuperMan = new JSONDeserializer<Hero>().deserialize(json);
assertNotNull("Make sure we got back a superman", jsonSuperMan);
assertEquals("Make sure the super powers were created properly.", 4, jsonSuperMan.getPowers().size());
assertHeroHasPowers(jsonSuperMan);
}
@Test
public void testNoClassHints() {
Hero superman = creator.createSuperman();
String json = new JSONSerializer().exclude("*.class").serialize(superman);
Hero jsonSuperMan = new JSONDeserializer<Hero>().use(null, Hero.class).use("lair", SecretLair.class).use("secretIdentity", SecretIdentity.class).deserialize(json);
assertNotNull("Make sure we got back a superman", jsonSuperMan);
assertEquals("Assert our name is super man", "Super Man", jsonSuperMan.getName());
assertNotNull("Assert our secret identity was restored", jsonSuperMan.getIdentity());
assertEquals("Assert our secret identity is Clark Kent", "Clark Kent", jsonSuperMan.getIdentity().getName());
assertNotNull("Assert our secret lair was restored", jsonSuperMan.getLair());
assertEquals("Assert our lair is the fortress of solitude", "Fortress of Solitude", jsonSuperMan.getLair().getName());
}
@Test
public void testNoHintsButClassesForCollection() {
Hero superman = creator.createSuperman();
String json = new JSONSerializer().include("powers.class").exclude("*.class").serialize(superman);
Hero jsonSuperMan = new JSONDeserializer<Hero>()
.deserialize(json, Hero.class);
assertHeroHasPowers(jsonSuperMan);
}
private void assertHeroHasPowers(Hero jsonSuperMan) {
for (int i = 0; i < jsonSuperMan.getPowers().size(); i++) {
assertTrue("Make sure super powers are instances of SuperPower", jsonSuperMan.getPowers().get(i) instanceof SuperPower);
}
}
@Test
public void testNoClassHintsForCollections() {
Hero superman = creator.createSuperman();
String json = new JSONSerializer()
.include("powers")
.include("powers.class")
.transform(new SimpleClassnameTransformer(), "powers.class")
.exclude("*.class").serialize(superman);
Hero jsonSuperMan = new JSONDeserializer<Hero>()
.use("lair", SecretLair.class)
.use("secretIdentity", SecretIdentity.class)
.use("powers.values", new SimpleClassLocator("flexjson.mock.superhero"))
.deserialize(json, Hero.class);
assertHeroHasPowers(jsonSuperMan);
}
@Test
public void testListSerialization() {
Person ben = creator.createBen();
Person charlie = creator.createCharlie();
Person pedro = creator.createPedro();
List<Person> list = new ArrayList<Person>(3);
list.add(ben);
list.add(charlie);
list.add(pedro);
String json = new JSONSerializer().serialize(list);
List<Person> people = new JSONDeserializer<List<Person>>().deserialize(json);
assertEquals(ArrayList.class, people.getClass());
json = new JSONSerializer().exclude("*.class").serialize( list );
people = new JSONDeserializer<List<Person>>().use("values", Person.class).deserialize(json);
assertEquals(ArrayList.class, people.getClass() );
assertEquals(3, people.size());
assertEquals(Person.class, people.get(0).getClass());
List<Map> peopleMap = new JSONDeserializer<List<Map>>().deserialize(json);
assertEquals(ArrayList.class, peopleMap.getClass() );
assertEquals(3, peopleMap.size());
assertEquals(HashMap.class, peopleMap.get(0).getClass());
}
@Test
public void testGenericTypeDeserialization() {
Pair<Hero, Villian> archenemies = new Pair<Hero, Villian>(creator.createSuperman(), creator.createLexLuthor());
String json = new JSONSerializer().exclude("*.class").serialize(archenemies);
Pair<Hero, Villian> deserialArchEnemies = new JSONDeserializer<Pair<Hero, Villian>>()
.use("first", Hero.class)
.use("second", Villian.class)
.deserialize(json, Pair.class);
assertEquals(archenemies.getFirst().getClass(), deserialArchEnemies.getFirst().getClass());
assertEquals(archenemies.getSecond().getClass(), deserialArchEnemies.getSecond().getClass());
assertEquals(archenemies.getFirst().getIdentity(), deserialArchEnemies.getFirst().getIdentity());
assertEquals(archenemies.getFirst().getLair(), deserialArchEnemies.getFirst().getLair());
assertEquals(archenemies.getFirst().getName(), deserialArchEnemies.getFirst().getName());
assertEquals(archenemies.getSecond().getName(), deserialArchEnemies.getSecond().getName());
assertEquals(archenemies.getSecond().getLair(), deserialArchEnemies.getSecond().getLair());
}
@Test
public void testGeneralMapDeserialization() {
String json = new JSONSerializer().exclude("*.class").serialize(creator.createCharlie());
Map<String, Object> deserialized = new JSONDeserializer<Map<String, Object>>().deserialize(json);
assertEquals("Charlie", deserialized.get("firstname"));
assertEquals("Hubbard", deserialized.get("lastname"));
assertTrue(Map.class.isAssignableFrom(deserialized.get("work").getClass()));
assertTrue(Map.class.isAssignableFrom(deserialized.get("home").getClass()));
}
@Test
public void testListDeserializationNoClass() {
Person ben = creator.createBen();
Person charlie = creator.createCharlie();
Person pedro = creator.createPedro();
List<Person> list = new ArrayList<Person>(3);
list.add(ben);
list.add(charlie);
list.add(pedro);
String json = new JSONSerializer().exclude("*.class").serialize(list);
List<Person> people = new JSONDeserializer<List<Person>>().use("values", Person.class ).deserialize(json);
assertEquals(ArrayList.class, people.getClass());
assertEquals(3, list.size());
assertEquals(ben.getFirstname(), list.get(0).getFirstname());
assertEquals(charlie.getFirstname(), list.get(1).getFirstname());
assertEquals(pedro.getFirstname(), list.get(2).getFirstname());
}
@Test
public void testMixedCase() {
String json = "{\"Birthdate\":196261875187,\"Firstname\":\"Charlie\",\"Home\":{\"City\":\"Atlanta\",\"State\":\"Ga\",\"Street\":\"4132 Pluto Drive\",\"Zipcode\":{\"zipcode\":\"33913\"}},\"lastname\":\"Hubbard\",\"Work\":{\"City\":\"Neptune\",\"State\":\"Milkiway\",\"Street\":\"44 Planetary St.\",\"Zipcode\":{\"Zipcode\":\"30328-0764\"}}}";
Person charlie = new JSONDeserializer<Person>().use(null, Person.class).deserialize(json);
assertEquals("Charlie", charlie.getFirstname());
assertEquals("Hubbard", charlie.getLastname());
assertEquals("Atlanta", charlie.getHome().getCity());
}
@Test
public void testDefaultDateFormats() throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy");
Person charlie = new Person("Charlie", "Hubbard", new Date(), null, null);
charlie.setBirthdate(df.parse("03/21/76"));
DateTransformer transformer = new DateTransformer( df.toPattern() );
String json = new JSONSerializer().transform(transformer, "birthdate").serialize(charlie);
Person newUser = new JSONDeserializer<Person>().deserialize(json);
assertEquals( charlie.getBirthdate(), newUser.getBirthdate() );
assertEquals( "03/21/76", df.format(newUser.getBirthdate()) );
String pattern = "yyyy-MM-dd hh:mm:ss";
json = new JSONSerializer().transform( new DateTransformer( pattern ), Date.class ).serialize(charlie);
Person user = new JSONDeserializer<Person>().use("birthdate", new DateObjectFactory().add(pattern) ).deserialize(json);
assertEquals( charlie.getBirthdate(), user.getBirthdate() );
assertEquals( "03/21/76", df.format( user.getBirthdate() ) );
DateObjectFactory.addDefaultFormat( pattern );
json = new JSONSerializer().transform( new DateTransformer( pattern ), Date.class ).serialize(charlie);
user = new JSONDeserializer<Person>().deserialize(json);
assertEquals( charlie.getBirthdate(), user.getBirthdate() );
assertEquals( "03/21/76", df.format( user.getBirthdate() ) );
}
@Test
public void testDateTransforming() throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
Person charlie = new Person("Charlie", "Hubbard", new Date(), null, null);
charlie.setBirthdate(df.parse("2009/01/02"));
DateTransformer transformer = new DateTransformer("yyyy/MM/dd");
String json = new JSONSerializer().transform(transformer, "birthdate").serialize(charlie);
Person newUser = new JSONDeserializer<Person>().use(transformer, "birthdate").deserialize(json);
assertEquals( charlie.getBirthdate(), newUser.getBirthdate() );
assertEquals( "2009/01/02", df.format(newUser.getBirthdate()) );
json = new JSONSerializer().serialize(charlie);
newUser = new JSONDeserializer<Person>().deserialize(json);
assertEquals( charlie.getBirthdate(), newUser.getBirthdate() );
assertEquals( "2009/01/02", df.format(newUser.getBirthdate()) );
}
@Test
public void testMapWithEmbeddedObject() {
Map<String,Network> networks = new JSONDeserializer<Map<String,Network>>().deserialize( "{\"1\": {\"class\":\"flexjson.mock.Network\", \"name\": \"Charlie\"} }" );
assertNotNull( networks );
assertEquals( 1, networks.size() );
assertTrue( networks.containsKey("1") );
assertNotNull( networks.get("1") );
assertEquals( Network.class, networks.get("1").getClass() );
assertEquals( "Charlie", networks.get( "1" ).getName() );
Map<String,Pair<Phone,Network>> complex = new JSONDeserializer<Map<String,Pair<Phone,Network>>>()
.use("values", Pair.class)
.use("values.first", Phone.class)
.use("values.second", Network.class)
.deserialize( "{\"1\": { \"first\": { \"areaCode\": \"404\" }, \"second\": {\"name\": \"Charlie\"} } }" );
assertNotNull( complex );
assertEquals( 1, complex.size() );
assertTrue( complex.containsKey("1") );
assertNotNull( complex.get("1") );
assertEquals( Pair.class, complex.get("1").getClass() );
assertEquals( Phone.class, complex.get("1").getFirst().getClass() );
assertEquals( Network.class, complex.get("1").getSecond().getClass() );
assertEquals( "404", complex.get( "1" ).getFirst().getAreaCode() );
assertEquals( "Charlie", complex.get( "1" ).getSecond().getName() );
}
@Test
public void testArrayType() {
Person charlie = creator.createCharlie();
Person ben = creator.createBen();
Group group = new Group( "brothers", charlie, ben );
String json = new JSONSerializer().include("people").exclude("*.class").serialize( group );
Group bro = new JSONDeserializer<Group>().use( null, Group.class ).deserialize( json );
assertNotNull( bro );
assertEquals( "brothers", bro.getGroupName() );
assertEquals( 2, bro.getPeople().length );
assertEquals( "Charlie", bro.getPeople()[0].getFirstname() );
assertEquals( "Ben", bro.getPeople()[1].getFirstname() );
}
/**
* https://sourceforge.net/tracker/?func=detail&aid=3004001&group_id=194042&atid=947842
*/
@Test
public void testEmptyArray() {
Group group = new JSONDeserializer<Group>().deserialize("{'people': [], 'groupName': 'Nobody' }", Group.class );
assertEquals( "Nobody", group.getGroupName() );
assertEquals( 0, group.getPeople().length );
}
@Test
public void testDeserialization() {
JSONDeserializer<Map<String, Object>> deserializer = new JSONDeserializer<Map<String, Object>>();
String input = "{property: true, property2:5, property3:'abc'}";
Map<String, Object> result = deserializer.deserialize(input);
assertNotNull(result);
assertEquals(3, result.size());
}
@Test
public void testNullDeserialization() {
String input = "{property: null, property2:5, property3:'abc'}";
JSONDeserializer<Map<String, Object>> deserializer = new JSONDeserializer<Map<String, Object>>();
deserializer.use( null, HashMap.class );
Map<String, Object> result = deserializer.deserialize(input);
assertNotNull(result);
// fails on this line, because the first property is not deserialized
assertEquals(3, result.size());
assertTrue(result.containsKey("property"));
assertNull("the value should be null", result.get("property"));
}
@Test
public void testArrayAndClassLocatorsInsideMaps() {
ClassLocator locator = new ClassLocator() {
public Class locate(ObjectBinder context, Path currentPath) throws ClassNotFoundException {
Object source = context.getSource();
if( source instanceof Map ) {
Map map = (Map)source;
if( map.containsKey("actLevStart") ) return HashMap.class;
if( map.containsKey("class") ) return Class.forName( (String)map.get("class") );
return HashMap.class;
} else if( source instanceof List ) {
return LinkedList.class;
} else {
return source.getClass();
}
}
};
Map<String,Object> bound = new JSONDeserializer<Map<String,Object>>().use("values", locator)
.deserialize( "{'foo1': 'bar1', 'foo2': {'actLevStart': 1, 'actLevEnd': 2}," +
"'foo3': {'someMapKey': 'someMapValue'}, 'foo4': [1, 2, 3]}" );
assertEquals( "bar1", bound.get("foo1") );
assertTrue( bound.get("foo2") instanceof Map );
assertTrue( bound.get("foo4") instanceof LinkedList );
}
@Test
public void testArraysAndClassLocators() {
ClassLocator locator = new ClassLocator() {
public Class locate(ObjectBinder context, Path currentPath) throws ClassNotFoundException {
Object source = context.getSource();
if( source instanceof Map ) {
Map map = (Map)source;
if( map.containsKey("actLevStart") ) return HashMap.class;
if( map.containsKey("class") ) return Class.forName( (String)map.get("class") );
return HashMap.class;
} else if( source instanceof List ) {
return LinkedList.class;
} else {
return source.getClass();
}
}
};
List<Map<String,Object>> list = new JSONDeserializer<List<Map<String,Object>>>().use("values", locator).deserialize("[{'foo1': 'bar1', 'foo2': {'actLevStart': 1, 'actLevEnd': 2 }, 'foo3': {'someMapKey': 'someMapValue'}}]");
assertEquals( 1, list.size() );
assertEquals( 3, list.get(0).size() );
}
@Test
public void testPrimitives() {
List<Date> dates = new ArrayList<Date>();
dates.add( new Date() );
dates.add( new Date(1970, 1, 12) );
dates.add( new Date(1986, 3, 21) );
String json = new JSONSerializer().serialize( dates );
List<Date> jsonDates = new JSONDeserializer<List<Date>>().use(null,ArrayList.class).use("values", Date.class ).deserialize( json );
assertEquals( jsonDates.size(), dates.size() );
assertEquals( Date.class, jsonDates.get(0).getClass() );
List<? extends Number> numbers = Arrays.asList( 1, 0.5, 100.4f, (short)5 );
json = new JSONSerializer().serialize( numbers );
List<Number> jsonNumbers = new JSONDeserializer<List<Number>>().deserialize( json );
assertEquals( numbers.size(), jsonNumbers.size() );
for( int i = 0; i < numbers.size(); i++ ) {
assertEquals( numbers.get(i).floatValue(), jsonNumbers.get(i).floatValue(), DELTA );
}
List<Boolean> bools = Arrays.asList( true, false, true, false, false );
json = new JSONSerializer().serialize( bools );
List<Boolean> jsonBools = new JSONDeserializer<List<Boolean>>().deserialize( json );
assertEquals( bools.size(), jsonBools.size() );
for( int i = 0; i < bools.size(); i++ ) {
assertEquals( bools.get(i), jsonBools.get(i) );
}
assertEquals( numbers.size(), jsonNumbers.size() );
}
@Test
public void testArray() {
Person[] p = new Person[3];
p[0] = creator.createCharlie();
p[1] = creator.createDilbert();
p[2] = creator.createBen();
String json = new JSONSerializer().serialize( p );
Person[] jsonP = new JSONDeserializer<Person[]>().use("values", Person.class).deserialize(json, Array.class);
assertEquals( 3, jsonP.length );
assertEquals( "Charlie", jsonP[0].getFirstname() );
assertEquals( "Dilbert", jsonP[1].getFirstname() );
assertEquals( "Ben", jsonP[2].getFirstname() );
}
@Test
public void testDeserializeIntoExistingObject() {
Person charlie = creator.createCharlie();
Phone fakePhone = new Phone( PhoneNumberType.MOBILE, "303 555 1234");
Person charlieClone = new Person( "Chauncy", "Beauregard", null, null, null );
charlieClone.getPhones().add( fakePhone );
charlieClone.getHobbies().add("Being Fake");
charlieClone.getHobbies().add("Assuming Other Identities");
String json = new JSONSerializer().include("hobbies").exclude("firstname", "lastname").serialize( charlie );
Person p = new JSONDeserializer<Person>().deserializeInto(json, charlieClone);
assertSame("Make sure the root object is the exact same reference as the one provided to the factory", charlieClone, p );
assertEquals( charlieClone.getFirstname(), p.getFirstname() );
assertEquals( charlieClone.getLastname(), p.getLastname() );
assertEquals( charlie.getBirthdate(), p.getBirthdate() );
assertEquals( charlie.getFirstBaseBallGame(), p.getFirstBaseBallGame() );
assertEquals( charlie.getWork(), p.getWork() );
assertEquals( charlie.getWork().getZipcode(), p.getWork().getZipcode() );
assertEquals( charlie.getHome(), p.getHome() );
assertEquals( charlie.getHobbies().size(), p.getHobbies().size() );
assertEquals( 1, p.getPhones().size() );
assertEquals( 2, charlie.getPhones().size() );
assertSame( fakePhone, p.getPhones().get(0) );
}
@Test
public void deserializeIntoExistingCollection() {
List<Person> people = new ArrayList<Person>();
people.add( creator.createCharlie() );
people.add( creator.createBen() );
String json = new JSONSerializer().include("hobbies").serialize(people);
List<Person> people2 = new JSONDeserializer<List<Person>>().deserializeInto(json, new LinkedList<Person>());
assertEquals( people2.getClass(), LinkedList.class );
assertEquals( people2.size(), people.size() );
}
@Test
public void deserializeIntoExistingMap() {
Map<String,Person> people = new HashMap<String, Person>();
people.put("Florida", creator.createCharlie());
people.put("Alabama", creator.createBen());
String json = new JSONSerializer().include("*.hobbies").serialize( people );
Map<String,Person> people2 = new JSONDeserializer<Map<String,Person>>().deserializeInto( json, new TreeMap<String,Person>() );
assertEquals( TreeMap.class, people2.getClass() );
assertEquals( people.size(), people2.size() );
assertEquals( 3, people2.get("Florida").getHobbies().size() );
assertEquals( 3, people2.get("Alabama").getHobbies().size() );
}
@Test
public void testDeserializationIntoPublicFields() {
Spiderman spiderman = new Spiderman();
spiderman.spideySense = false;
spiderman.superpower = "Creates Many Webs and Super Tough";
String json = new JSONSerializer().serialize( spiderman );
Spiderman jsonSpiderman = new JSONDeserializer<Spiderman>().deserialize( json );
assertEquals( spiderman.spideySense, jsonSpiderman.spideySense );
assertEquals( spiderman.superpower, jsonSpiderman.superpower );
}
/**
* https://sourceforge.net/tracker/?func=detail&atid=947844&aid=3004785&group_id=194042
*/
@Test
public void testAutoTypeConvertToNumerical() {
Account account = new JSONDeserializer<Account>().deserialize("{'id': '5', 'accountNumber': '1234567-123'}", Account.class);
assertEquals( new Integer(5), account.getId() );
XRayVision xray = new JSONDeserializer<XRayVision>().deserialize("{ 'power': '2.3' }", XRayVision.class);
assertEquals( 2.3f, xray.getPower(), DELTA);
}
/**
* https://sourceforge.net/tracker/?func=detail&aid=2973789&group_id=194042&atid=947842
*/
@Test
public void testDeserializeURL() {
String json = "{\n" +
" \"oslc_cm:next\": \"http:\\/\\/localhost:9080\\/results\\/3\",\n" +
" \"oslc_cm:previous\": \"http:\\/\\/localhost:9080\\/results\\/1\", \n" +
" \"oslc_cm:totalCount\" : 27,\n" +
" \"oslc_cm:results\": [\n" +
" {\n" +
" \"rdf:resource\": \"http:\\/\\/localhost:9080\\/records\\/1234\"\n" +
" },\n" +
" {\n" +
" \"rdf:resource\": \"http:\\/\\/localhost:9080\\/records\\/1235\"\n" +
" },\n" +
" {\n" +
" \"rdf:resource\": \"http:\\/\\/localhost:9080\\/records\\/1236\"\n" +
" } \n" +
" ]\n" +
"}";
Map<String,Object> page2 = new JSONDeserializer<Map<String,Object>>().deserialize( json );
assertEquals( "http://localhost:9080/results/3", page2.get("oslc_cm:next") );
assertEquals( 3, ((List)page2.get("oslc_cm:results")).size() );
}
/**
* https://sourceforge.net/tracker/?func=detail&aid=2968434&group_id=194042&atid=947842
*/
@Test
public void testPoint() {
String json = new JSONSerializer().serialize( new Point2D.Float(1.0f, 2.0f) );
Point2D.Float point = new JSONDeserializer<Point2D.Float>().deserialize( json );
assertEquals( 1.0f, point.x, DELTA );
assertEquals( 2.0f, point.y, DELTA );
}
@Test
public void testUnixEpoch() {
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.set(Calendar.YEAR, 1970);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.AM_PM, Calendar.AM);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Person hank = new Person("Hank", "Paulsen", cal.getTime(), null, null);
String json = new JSONSerializer().serialize(hank);
Person deHank = new JSONDeserializer<Person>().deserialize(json, Person.class);
assertEquals( hank.getFirstname(), deHank.getFirstname() );
assertEquals( hank.getLastname(), deHank.getLastname() );
assertEquals( hank.getBirthdate(), deHank.getBirthdate() );
}
@Test
public void testDecimalVsLong() {
String json = "{ 'long': 3, 'exponent': 3e+2, 'decimal': 4.5, 'negativeLong': -45, 'positiveLong': 34 }";
Map obj = new JSONDeserializer<Map>().deserialize(json, Map.class);
assertEquals(3l, obj.get("long") );
assertEquals(300d, obj.get("exponent") );
assertEquals(4.5d, obj.get("decimal") );
assertEquals(-45l, obj.get("negativeLong") );
assertEquals(34l, obj.get("positiveLong") );
}
public static class SimpleClassnameTransformer implements Transformer {
public void transform(Object value) {
int classname = value.toString().lastIndexOf('.');
if (classname > 0) {
JSONContext.get().writeQuoted( value.toString().substring(classname + 1) );
} else {
JSONContext.get().writeQuoted( value.toString() );
}
}
}
public static class SimpleClassLocator implements ClassLocator {
private String packageName;
public SimpleClassLocator(String packageName) {
this.packageName = packageName;
}
public Class locate(ObjectBinder context, Path currentPath) throws ClassNotFoundException {
Map map = (Map) context.getSource();
return Class.forName(packageName + "." + map.get("class").toString());
}
}
}