Best approach for marshaling unmarshaling immutable objects with a build pattern

I have a simple soothing service that I am developing in java. I considered several options for sorting / untying json. Possible approaches available by jaxb jackson, etc., are completely new to me, and I'm trying to find legs with them. I was wondering if I could get some advice on what would be the best approach and technology to use, especially considering that many of the objects that interest me, I implemented as immutable, and I used the builder pattern. Thus, there are no setters, and the constructor is private.

I reviewed this previous question: Jackson + Builder Pattern? hosted on stackoverflow. I am considering something like this approach, although it would be nice to get some pointers to additional resources about using @JsonDeserialize

Here is a very simple example of the type of object I am 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 is no problem sorting this object, but unmarshalling does not seem straightforward. Is there also support to exclude entries for values ​​of objects that are null?

+8
java jackson jersey jaxb
source share
5 answers

You can use the XmlAdapter with JAXB to handle immutable objects:

+2
source share

you can do this: (implement only getters and use XmlAccessType.FIELD)

 @XmlAccessorType(XmlAccessType.FIELD) public class CreditCardVO implements Serializable { private Long ccNumber; private String ccName; public CreditCardVO(Long ccNumber, String ccName) { this.ccNumber = ccNumber; this.ccName = ccName; } private CreditCardVO() { // for JAXB Magic } public Long getCcNumber() { return ccNumber; } public String getCcName() { return ccName; } } 

taken from http://aniketshaligram.blogspot.com/2010/05/jaxb-immutable-objects.html

+6
source share

To keep things simple: don't go there. Only value objects should be serialized / deserialized by Jersey / Jackson, and there is no reason to make them immutable because they should not have access to them.

those. each service call should generate a new Value object that is not available to other threads. Thus, you do not need to worry about immutability and, therefore, use the standard method with getters and setters.

Do not make life unnecessarily complicated if it does not buy anything!

+1
source share

If you want marshlling / unmarshilling u to be able to use JAXB when you want to get XML output, otherwise for JSON I would prefer to add some Facades that convert the JSON string to objects.

and this means that each thread will have its own Facade instance, so there will be no sense of immutability, and if u tends to make a single Facade, then there will also be no problem.

When using JSON, you can write your own logic to create an object, which means you can use a constructor or setters. By adding above and then using Facade, you can also maintain subclasses and everything under the same facade.

0
source share

Why are there plans (but not ready-made code) to support the Builder style for deserialization (according to this entry in the Jire ), Whenever it is implemented, it depends on the number of people working on the problem (or at least expressing interest).

0
source share

All Articles