Sie sind auf Seite 1von 5

Capsules for the Java Mind

Conversion Box
v1.0
Documentation

Capsules for the Java Mind

Sr. No.

Topic

Page Number

Overview

JARs Required

Methods

Known Issues and Limitations

Capsules for the Java Mind

Overview
The Conversion Box component works with the idea of converting data from one structure to another.
It can be used to carry out conversions between Maps, JSONs, XMLs and Java Objects. It also converts
Lists of Maps embedded within them to their corresponding equivalent. Also, the hierarchy within the
data structure is maintained.
The conversion concept is common for all scenarios. Every data structure in the input is converted to
its corresponding equivalent in the output. It can be summarized as follows:
Map

JSON

XML

Java Object

Map

JSON Object

XML representation of Map

Object / Map within Object

List of Maps

JSON Array

XML representation of List


of Maps

List of Maps within Object

The values of the Keys are maintained in all the conversions except for the scenarios where Lists of
Maps are involved. This can be illustrated in the following example:
Consider a Map with multiple values. Every value has its corresponding key. Lets assume this Map contains
a key that has a List of Maps mapped to it. Now we know that elements of a List do not have keys
assigned to it and their corresponding XML representations also do not necessarily need one.
However, this component works on the concept of a key for every value. Thus, it will assign a Constant
LIST_ELEMENT appended with the parent key as a key to every element of the List. This applies to all the
Lists in the input.
This applies to JSONs and Java Objects and works conversely as well. The component expects the List
representation in an XML (to be converted into a Map, JSON or Java Object) to be enclosed within a
tag for itself.

JARs required
1. JSON
2. Apache Commons BeanUtils
3. Apache Commons Logging
4. Apache Commons Collections (if required)

Capsules for the Java Mind

Methods
Class: cjm.component.cb.map.ToMap
public Map<String, Object> convertToMap(Object object)
This method will convert an Object to a Map. This Object could be a JSON, XML or a Java Object.
Class: cjm.component.cb.json.ToJSON
public JSONObject convertToJSON(Object object)
This method will convert an Object to a JSON. This Object could be a Map, XML or a Java Object.
Class: cjm.component.cb.xml.ToXML
public StringBuilder convertToXML(Object object, boolean emptyAction)
This method will convert an Object to an XML. This Object could be a Map, JSON or a Java Object.
What is emptyAction?
When there are no value to a key, the key is represented in the XML in the following two ways:
1. <KEY></KEY>
2. <KEY/>
This can be achieved by setting true to the parameter emptyAction for the first result and false
for the second.
Class: cjm.component.cb.object.ToObject
public Object convertToObject(Object object, Object objectPrimary, List<Object> objectList)
This method will convert an Object to Java Object. This Object could be a Map, JSON or an XML.
What is objectPrimary ?
When trying to convert a data structure from any form to a Java Object, the objectPrimary
parameter is the object to which the data structure is mapped to. It uses this dummy parameter to
locate members and map the appropriate values to them.
What is objectList?
When trying to convert a data structure from any form to a Java Object, the objectList parameter
is the list of objects that are nested members of the primary object to which the data is converted
to. It uses this dummy parameter to locate nested object members and map the appropriate values
to them.

Capsules for the Java Mind

Known Issues and Limitations


1. The component works for Keys with values represented as <KEY/>. However, it does not work for
representations where there is a space after the key (like <KEY />).
2. Though white spaces within XMLs are not an issue, it is always preferable to have an XML without
any white spaces between two tags when there are Lists involved. White spaces within values are not
an issue.
3. Lists of Maps embedded in Maps, JSONs or Java Objects are enclosed within a Constant
LIST_ELEMENT appended by the primary key in its corresponding XML. This Constant is not
configurable.
4. All Lists embedded within a data structure need to be Lists of Maps and not just Lists.
5. Special characters are not escaped or handled.
6. In case of conversions where Java Objects are involved, Maps and Lists of Maps within a Java Object
are supported. However, Java Objects within Maps or Lists are not supported.
7. Every key in the data structure (nested or not) needs to be unique.

Das könnte Ihnen auch gefallen