Apache Juneau logo

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

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.

JSON

XML

HTML

UON (URL-Encoded Object Notation)

URL-Encoding

MessagePack

SOAP/XML

CSV

Download jar or simply add maven dependency to start with a marshaling example

<dependency>

<groupId>org.apache.juneau</groupId>

<artifactId>juneau-marshall</artifactId>

<version>7.2.1</version>

</dependency>

Create a simple POJO class Person.java as per below

Person.java

Here @BeanConstructor annotation is required for parsing back to objects and it is optional for serialization.

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.

{

"name": "Jackson",

"age": 24,

"nickNames": ["Jil","Bil","Rock"],

"city": "Colombo"

}

If you want in XML simply use XmlSerializer class

Also can serialized to HTML too..

HTML serialized instance of Person

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

Deserialization

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

Conclusion

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! 👍

See community for contribution guide

See you in next article. I am planning to write more about Apache Juneau.

Cheers!!

References