It's basically an annotation to tell VRaptor to serialize your method's return to JSON format.
Instead of writing something like:
public void list(){
result.use(Results.json()).withoutRoot().from(dao.findAll()).recursive().serialize();
}
You just annotate the method with @Json and let it return your Java objects, like:
@Json
public List<Person> list(){
return dao.findAll();
}
git clone git://github.com/luizsignorelli/vraptor-gson-serializer.git
cd vraptor-gson-serializer
mvn install
- No configuration needed.
- Ability to tell exactly which fields should be serialized (excludes all others)
@Resource
public class CustomerController {
@Json(fields = CustomerFields.class) @Get("/customer/byName")
public List<Customer> findByName(String name){
return Arrays.asList(customers.findByName(name));
}
}
Where CustomerFields class would look something like:
public class CustomerFields extends extends JsonFields {
@Override
public String[] includes() {
return new String[] {
"name", // simple property
"address.street", // object property
"phones.number" // list-of-objects property
};
}
}
- Plugin ported to VRaptor 4.x
We've added a JsonInterceptor and a @Json annotation, so now you can do this:
@Resource
public class CustomerController {
private final Customers customers;
public CustomerController(Customers customers) {
this.customers = customers;
}
@Json @Get("/customer/byName")
public List<Customer> findByName(String name){
return Arrays.asList(customers.findByName(name));
}
}
The annotation marks the method to be intercepted, and it will just use the result.use(json()) to serialize the return of the method.
You can exclude some fields too:
@Resource
public class CustomerController {
private final Customers customers;
public CustomerController(Customers customers) {
this.customers = customers;
}
@Json(exclude = {"address, age"}) @Get("/customer/byName")
public List<Customer> findByName(String name){
return Arrays.asList(customers.findByName(name));
}
}
We've implemented the exclude funcionality. It works just like the XstreamSerializer.