Multiple json bindings in java

Java has several XML binding frameworks that map Java objects to and from XML. These structures allow you to map different Java hierarchies and classes in the selected XML structure. They can be configured through a separate configuration file. These frameworks include Castor, JiBX and the like. Other structures can be configured programmatically.

Most JSON binding structures simply map the Java object directly to json. Most of them do not extend the flexibility of mapping complex Java hierarchies to any arbitrary selected representation of JSON objects. The configuration is usually quite limited.

I am currently trying to create a web service that returns json responses. If my Java domain classes change in any way, my json responses will also change, causing the service clients waiting for the old version of json responses to break.

Are there any fully flexible JSON binding structures that can map multiple bindings to the same set of classes?

Or (perhaps a more fundamental question), how can I support different versions of JSON bindings in the same set of Java classes?

Or should I just make sure my domain classes never change? (This is not possible)

+4
source share
3 answers

Note. I am EclipseLink JAXB (MOXy) , and a member of the JAXB 2 Expert Group (JSR-22) .

The MOXy component in EclipseLink 2.4 will contain the JSON binding type you are looking for. In the example below, the Address object is mapped to a response to the execution of the Google Geocoding API V2 . This example demonstrates how path-based matching eliminates the need for a close relationship between classes and the JSON structure.

package blog.geocode.json; import javax.xml.bind.annotation.XmlType; import org.eclipse.persistence.oxm.annotations.XmlPath; @XmlType(propOrder={"country", "state", "city", "street", "postalCode"}) public class Address { @XmlPath("Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:Locality/ns:Thoroughfare/ns:ThoroughfareName/text()") private String street; @XmlPath("Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:Locality/ns:LocalityName/text()") private String city; @XmlPath("Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:AdministrativeAreaName/text()") private String state; @XmlPath("Placemark/ns:AddressDetails/ns:Country/ns:CountryNameCode/text()") private String country; @XmlPath("Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:Locality/ns:PostalCode/ns:PostalCodeNumber/text()") private String postalCode; } 

As you noted, a separate mapping document is the key to applying multiple comparisons to an object model. The MOXy binding document can be used for both XML binding and JSON:

You can try JSON binding today using one of the steps:

+2
source

If you use JAXB in conjunction with JAX-RS (Java API for RESTful web services), it is very simple. JAX-RS (or at least Jersey , the JAX-RS reference implementation) understands JAXB annotations and allows you to output JSON instead of XML. you need to change the media type of your webservice method, for example:

 @GET @Path("{customerId}") @Produces(MediaType.TEXT_XML) // Change this to MediaType.APPLICATION_JSON public Customer getCustomer(@PathParam("customerId") String customerId) { // ... } 

Where Customer is a class annotated using JAXB mapping annotations (which may refer to other classes with annotations, etc.).

+1
source
Good question. Alone, I fought myself on a small scale.

The problem is that object serialization interrupts encapsulation. But then it is also displayed on the user interface. But if you do not, what good?

The only solution I could come up with is to create a View object. The view object has getters and setters that allow you to externalize parts of the data view without any private data needed for the actual processing.

The problem is that this requires some duplicate coding, but it seems to prevent some long-term architectural problems.

Sorry that I am not more useful, but this is just one of those unpleasant problems that software developers will fight forever.

My $ .02.

0
source

All Articles