Skip to content

Serialize your Java objects to JSON on VRaptor 4.x apps just annotating the Controller methods with @JSON.

Notifications You must be signed in to change notification settings

marcioferlan/vraptor-gson-serializer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ABOUT

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();
}

INSTALATION

git clone git://github.com/luizsignorelli/vraptor-gson-serializer.git
cd vraptor-gson-serializer
mvn install

CONFIGURATION

  1. No configuration needed.

CHANGES

0.6.1

  • 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
			}; 
		}
    }

0.6.0

  • Plugin ported to VRaptor 4.x

0.5.0

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));
        }
    }

0.0.2

We've implemented the exclude funcionality. It works just like the XstreamSerializer.

About

Serialize your Java objects to JSON on VRaptor 4.x apps just annotating the Controller methods with @JSON.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%