Serializing and deserializing POJOs in to many formats using Apache Juneau
POJO (Plain Old Java Objects) are normal reusable java objects with several rules like,
- Having getters and setters per each private property
- Only inherits methods from default Object class
POJOs can be serialized/deserialized easily by implementing Serializable interface from java API. But how about serializing in to another formats like JSON, XML or HTML. This is where we can use a sub module of Apache Juneau
Apache Juneau is an open source, lightweight set of libraries for marshaling POJOs, building quick microservices. It is having simple but powerful variable replacement language too. See all modules of Juneau here
Getting Started with juneau-marshall
juneau-marshall module offers serializing and deserializing support for following string formats.
- UON (URL-Encoded Object Notation)
Download jar or simply add maven dependency to start with a marshaling example
Create a simple POJO class Person.java as per below
@BeanConstructor annotation is required for parsing back to objects and it is optional for serialization.
Let’s fill above POJO with some data and do the serialization to JSON format via PersonSerialize.java
This will output human readable JSON of serialized pojo object.
If you want in XML simply use XmlSerializer class
Also can serialized to HTML too..
Serializers use builder pattern so you will be able to easily customize those as you wish by using chain methods eg — if you need single quotes instead double quotes for JSON use builder as JsonSerializer.create().sq().build();
Juneau supports many formats for serialization. See all supported serializers here
For the deserialization we can use Parsers which will take the structure of class and plug all properties.
This will simply output string representation of nickNames array.
june-marshall does the parsing and serializing recursively. Therefore aggregated beans also will work properly. For somewhat complex example for serialization see this
juneau-marshall module is only a sub module of Apache Juneau project. Whole project can be defined as an ecosystem because it is having multiple interactive modules for various kind of works. Thus each modules are combined and forms another module or sub module and so on. eg — juneau-config module is a combination of juneau-marshall and juneau-svl.
Apache Juneau is open source and anyone can contribute to improve the project by adding new features or resolving issues from issue tracker. It is currently in used by companies such as IBM, The Open Group, and Salesforce. Begin by cloning the repository today! 👍
Apache Juneau is a single cohesive framework. Contribute to apache/juneau development by creating an account on GitHub.
See community for contribution guide
See you in next article. I am planning to write more about Apache Juneau.