I have a simple restful service that I'm developing in java. I have been looking at a few of the options for marshalling/unmarshalling json. The possible approaches available, jaxb jackson etc, are quite new to me and I'm trying to find my feet with them. I was wondering if I could get some advice on what would be the best approach and technology to use especially given that many of the objects I'm interested in I have implemented as being immutable and I have used the builder pattern. So there are no setters and the constructor is private.
I have looked at this previous question: Jackson + Builder Pattern? posted on stackoverflow. I am considering something like this approach although it would be great to get some pointers to more resources about using @JsonDeserialize
Here is a very simple example of the type of object I'm considering
public class Reading {
private final double xCoord;
private final double yCoord;
private final double diameter;
private final double reliability;
private final String qualityCode;
private Reading(Builder builder){
xCoord = builder.xCoord;
yCoord = builder.yCoord;
diameter = builder.diameter;
reliability = builder.reliability;
qualityCode = builder.qualityCode;
}
public static class Builder {
//required parameters
private final double diameter;
//optional parameters
private double xCoord = 0.0;
private double yCoord = 0.0;
private double reliability = 1.0;
private String qualityCode;
public Builder (double diameter){
this.diameter = diameter;
}
public Builder reliability(double val){
reliability = val;
return this;
}
public Builder qualityCode(String qualityCode){
this.qualityCode = qualityCode;
return this;
}
public Builder coordinates(double xCoord, double yCoord){
this.xCoord = xCoord;
this.yCoord = yCoord;
return this;
}
public Reading build(){
return new Reading(this);
}
}
public double getXCoord() {return xCoord;}
public double getYCoord() {return yCoord;}
public String getQualityCode() {return qualityCode;}
public double getDiameter() { return diameter;}
public double getReliability() {return reliability; }
}
There are no problems marshalling this object but unmarshalling doesn't seem to be straight forward. Also is there support for leaving out entries for object values that are null?