JType Wiki
Status: Beta
Brought to you by:
benhoskins
@Test public void autoConvertToAppropriateTypes() throws Exception {
Person person = Convert("id=1001&title=Mr&firstName=Forrest&lastNames=Winston%2CGroom%2CGump&male=true").to(Person.class);
assertThat(person.getTitle(), is("Mr"));
assertThat(person.getFirstName(), is("Forrest"));
List<String> lastNames = person.getLastNames();
assertThat(lastNames.get(0), is("Winston"));
assertThat(lastNames.get(1), is("Groom"));
assertThat(lastNames.get(2), is("Gump"));
assertThat(person.getId(), is(1001));
assertThat(person.isMale(), is(true));
}
@Test public void autoConvertToAppropriateTypes() throws Exception {
Map<String, String> map = Convert("id=1001&title=Mr&firstName=Forrest&lastNames=Winston,Groom,Gump&male=true").to(Map.class);
Person person = Person.class.newInstance();
Field[] personFields = person.getClass().getDeclaredFields();
for (Field personField : personFields) {
personField.setAccessible(true);
personField.set(person, Convert(map.get(personField.getName())).to(personField.getType()));
}
assertThat(person.getTitle(), is("Mr"));
assertThat(person.getFirstName(), is("Forrest"));
List<String> lastNames = person.getLastNames();
assertThat(lastNames.get(0), is("Winston"));
assertThat(lastNames.get(1), is("Groom"));
assertThat(lastNames.get(2), is("Gump"));
assertThat(person.getId(), is(1001));
assertThat(person.isMale(), is(true));
}
public class Person {
private Integer id;
private String title;
private String firstName;
private List<String> lastNames;
private Boolean male;
public Person() {}
public Person(int id, String title, String firstName, List<String> lastNames, boolean male) {
this.id = id;
this.title = title;
this.firstName = firstName;
this.lastNames = lastNames;
this.male = male;
}
public String getTitle() { return title; }
public Boolean isMale() { return male; }
public String getFirstName() { return firstName; }
public List<String> getLastNames() { return lastNames; }
public Integer getId() { return id; }
}