Example
Say you have defined some classes using JSON:[ { "type": "record", "name": "Exon", "fields" : [ {"name": "start", "type": "int"}, {"name": "end", "type": "int"} ] }, { "type": "record", "name": "Gene", "fields" : [ {"name": "chrom", "type": "string"}, {"name": "name", "type": "string"}, {"name": "txStart", "type": "int"}, {"name": "txEnd", "type": "int"}, {"name": "cdsStart", "type": "int"}, {"name": "cdsEnd", "type": "int"}, {"name": "exons", "type":{"type":"array","items":"Exon"}} ] } ]and here is a velocity template transforming this json structure to java :
#macro(javaName $s)$s.substring(0,1).toUpperCase()$s.substring(1)#end
#macro(setter $s)set#javaName($s)#end
#macro(getter $s)get#javaName($s)#end
#macro(javaType $f)
#if($f.type.equals("string"))
java.lang.String#elseif($f.type.equals("boolean"))
boolean#elseif($f.type.equals("long"))
long#elseif($f.type.equals("float"))
float#elseif($f.type.equals("double"))
double#elseif($f.type.equals("int"))
int#elseif($f.items)
$f.items#elseif($f.type.type.equals("array"))
java.util.List<#javaType($f.type)>#else
$f.type
#end
#end
#foreach( $class in $avro)
class $class.name
{
#foreach( $field in $class.fields )
private #javaType($field) $field.name;
#end
public ${class.name}()
{
}
public ${class.name}(#foreach( $field in $class.fields )
#if($velocityCount>1),#end#javaType($field) $field.name
#end
)
{
#foreach( $field in $class.fields )
this.$field.name=$field.name;
#end
}
#foreach( $field in $class.fields )
public void #setter($field.name)(#javaType($field) $field.name)
{
this.$field.name=$field.name;
}
public #javaType($field) #getter($field.name)()
{
return this.$field.name;
}
#end
}
#end
The json file can be processed with velocity using the following command line:$ java -jar velocityjson.jar -f avro structure.json json2java.vm
Result
class Exon { private int start; private int end; public Exon() { } public Exon( int start ,int end ) { this.start=start; this.end=end; } public void setStart(int start) { this.start=start; } public int getStart() { return this.start; } public void setEnd(int end) { this.end=end; } public int getEnd() { return this.end; } } class Gene { private java.lang.String chrom; private java.lang.String name; private int txStart; private int txEnd; private int cdsStart; private int cdsEnd; private java.util.List<Exon> exons; public Gene() { } public Gene( java.lang.String chrom ,java.lang.String name ,int txStart ,int txEnd ,int cdsStart ,int cdsEnd ,java.util.List<Exon> exons ) { this.chrom=chrom; this.name=name; this.txStart=txStart; this.txEnd=txEnd; this.cdsStart=cdsStart; this.cdsEnd=cdsEnd; this.exons=exons; } public void setChrom(java.lang.String chrom) { this.chrom=chrom; } public java.lang.String getChrom() { return this.chrom; } public void setName(java.lang.String name) { this.name=name; } public java.lang.String getName() { return this.name; } public void setTxStart(int txStart) { this.txStart=txStart; } public int getTxStart() { return this.txStart; } public void setTxEnd(int txEnd) { this.txEnd=txEnd; } public int getTxEnd() { return this.txEnd; } public void setCdsStart(int cdsStart) { this.cdsStart=cdsStart; } public int getCdsStart() { return this.cdsStart; } public void setCdsEnd(int cdsEnd) { this.cdsEnd=cdsEnd; } public int getCdsEnd() { return this.cdsEnd; } public void setExons(java.util.List<Exon> exons) { this.exons=exons; } public java.util.List<Exon> getExons() { return this.exons; } }
That's it,
Pierre
No comments:
Post a Comment