'Bason: A BSON Serialization Code Generator'

Mon 23 August 2010
  • ANN tags:
  • java
  • maven
  • opensource published: true comments: true

Bason is a code generator for object to bson serialization and deserialization. Different from tranditional reflection way, bason uses an annotation processor to generate serialization manager at compile time. You just add Bason as compilation dependency and drop it in the runtime.

To use Bason, you simply add annotation to JavaBeans:
[cc lang="java"]
/**
*
*/
package info.sunng.bason.example;

import java.util.Date;

import info.sunng.bason.annotations.BsonAlias;
import info.sunng.bason.annotations.BsonDocument;
import info.sunng.bason.annotations.BsonIgnore;

/**
* @author SunNing
*
* @since Aug 18, 2010
*/
@BsonDocument
public class Passenger {

private double packageWeight;

private long ticketId;

private String name;

private Date createdDate;

private Flight flight;

/**
* @return the packageWeight
*/
@BsonIgnore
public double getPackageWeight() {
return packageWeight;
}

/**
* @param packageWeight the packageWeight to set
*/
public void setPackageWeight(double packageWeight) {
this.packageWeight = packageWeight;
}

/**
* @return the ticketId
*/
@BsonAlias("ticket")
public long getTicketId() {
return ticketId;
}

/**
* @param ticketId the ticketId to set
*/
public void setTicketId(long ticketId) {
this.ticketId = ticketId;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @param createdDate the createdDate to set
*/
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}

/**
* @return the createdDate
*/
public Date getCreatedDate() {
return createdDate;
}

/**
* @param flight the flight to set
*/
public void setFlight(Flight flight) {
this.flight = flight;
}

/**
* @return the flight
*/
public Flight getFlight() {
return flight;
}

}

[/cc]

  • @BsonDocument marks this bean to be processed by bason processor. Serialization and deserialization support for this bean will be added to the manager. The bean must follow the Java Bean specification that has a getter and a setter for each property.
  • @BsonAlias on the getter allows user the specify a name for bson document instead of the default java bean property name.
  • @BsonIgnore on the getter marks a property to be transient when serialization and deserialization.

Then you need a bason.properties at the root of classpath which looks like
[cc lang="properties"]
bason.managerClassName=info.sunng.bason.BasonManager
[/cc]
You specify the manager class name here. This name can not be duplicated if you use Bason in multiple modules.

Take maven configuration as an example:
[cc lang="xml"]
info.sunng.bason bason-annotation compile info.sunng.bason bason-internal compile org.mongodb mongo-java-driver

${project.artifactId}

org.apache.maven.plugins maven-compiler-plugin

1.6 1.6

info.sunng.bason.internal.BasonProcessor [/cc]

when everything is ready, run mvn compile to generate the manager source file. By default, in a standard maven project, the generated file will be placed at: /bason-example/target/generated-sources/annotations/ [cc lang="java"]
package info.sunng.bason;
import org.bson.*;
import javax.annotation.Generated;
@Generated({"info.sunng.bason.BasonManager"})
public final class BasonManager{
public static final BSONObject toBson(info.sunng.bason.example.Passenger o){
if (o == null) {
throw new NullPointerException();
}
BSONObject bson = new BasicBSONObject();
bson.put("ticket",o.getTicketId());
bson.put("name",o.getName());
bson.put("createdDate",o.getCreatedDate());
bson.put("flight",toBson(o.getFlight()));
return bson;
}
public static final info.sunng.bason.example.Passenger fromBson(info.sunng.bason.example.Passenger o, BSONObject bson){
if (o == null || bson == null) {
throw new NullPointerException();
}
o.setTicketId((java.lang.Long)bson.get("ticket"));
o.setName((java.lang.String)bson.get("name"));
o.setCreatedDate((java.util.Date)bson.get("createdDate"));
o.setFlight(fromBson(new info.sunng.bason.example.Flight(), (BSONObject)bson.get("flight")));
return o;
}
public static final BSONObject toBson(info.sunng.bason.example.Flight o){
if (o == null) {
throw new NullPointerException();
}
BSONObject bson = new BasicBSONObject();
bson.put("company",o.getCompany());
bson.put("flightId",o.getFlightId());
return bson;
}
public static final info.sunng.bason.example.Flight fromBson(info.sunng.bason.example.Flight o, BSONObject bson){
if (o == null || bson == null) {
throw new NullPointerException();
}
o.setCompany((java.lang.String)bson.get("company"));
o.setFlightId((java.lang.String)bson.get("flightId"));
return o;
}
}

[/cc]

The project is hosted at http://github.com/sunng87/bason

If you have any ideas, just let me know.

The post is brought to you by lekhonee v0.7