Over a million developers have joined DZone.

Easy Conversion of HTTP Payloads

DZone 's Guide to

Easy Conversion of HTTP Payloads

Payload, a new tool up on GitHub, promises to serialize and deserialize data without the need to use third-party libraries like Jackson or Gson.

· Java Zone ·
Free Resource

Almost every application needs to handle data in an interchangeable format. In the world of HTTP JSON-based APIs, the task of serializing and deserializing the payloads is something usually delegated to a third party library. Some examples are Jackson, Gson, and the most recent Java EE spec JsonP. What if there was a way where applications can be decoupled from these providers in a similar fashion to how SLF4J does for logging? That's the goal of Payload.


// Serialization
Payload<MyObject> payload = 
JSON.payload().newInstance(new MyObject());
String json = payload.raw();
// Deserialization
Payload<MyObject> payload =
XML.payload().newInstance(xmlAsString, MyObject.class);
MyObject obj = payload.get();

The design is quite flexible in the way it can accommodate custom implementations of the mechanism that does the conversion. I call it the Provider in this context. Click here to understand more about providers.

Testing is something that you might want to use this library for. The ability to swap the underlying provider gives you a way to assemble your JSON test data using a different instrument rather than the one used in production, but while still keeping the same API.

No major performance drawbacks have been identified by using Payload instead of directly employing Jackson or Gson. The project contains a few JUnit [benchmark tests] to address the matter.

I have created a very small app as a proof of concept for this project: Payload Tests. There, you can see how things are expected to work, including the implementation of a custom provider.

Great effort has been put into the design aspect. It is fair to mention that the main motivation behind this initiative was actually to exercise good practices such as object composition, OOP, and building a pipeline to continuously release in Maven Central, just to name a few. If you have read this far and have also found value in what has been built, I'd be more than happy to accept your contributions. Keep coding!

java ,data binding ,serialization ,deserialization ,payload

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}