Handling JSON in the ESB
Jan 08, 2015 10:02 0 Comments Adapters Nancy






INTRODUCTION


In version 9.x of webMethods, JSON support is out of the box, however for the older versions, you need to write your own handler.

This article will provide sample code on how to use GSON (Google's JSON parser) to handle JSON strings and arrays in the ESB.
We are assuming your IData objects are of type key (String) and value (Object)

IS JAVA SERVICES




JSONToDocument


public static final void JSONToDocument(IData pipeline) throws ServiceException { 
// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
String jsonString = IDataUtil.getString(pipelineCursor, "jsonString");
pipelineCursor.destroy();

Gson gson = new Gson();
Type type = new TypeToken<Map<String, Object>>() {}.getType();
Map<String, Object> jsonMapList = gson.fromJson(jsonString, type);
IData document = convertToIData(jsonMapList);
// pipeline
IDataCursor pipelineCursor_1 = pipeline.getCursor();
IDataUtil.put(pipelineCursor_1, "document", document);
pipelineCursor_1.destroy(); }


JSONToDocumentList


public static final void JSONToDocumentList(IData pipeline) throws ServiceException { 
// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
String jsonString = IDataUtil.getString(pipelineCursor, "jsonString");
pipelineCursor.destroy();

Gson gson = new Gson();
Type type = new TypeToken<List<Map<String, Object>>>() {}.getType();
List<Map<String, Object>> jsonMapList = gson.fromJson(jsonString, type);
List<IData> docList = new ArrayList<IData>()
for(Map<String, Object> map : jsonMapList) {
docList.add(convertToIData(map));
}
IData[] documentList = new IData[docList.size()];
docList.toArray(documentList);
// pipeline
IDataCursor pipelineCursor_1 = pipeline.getCursor();
IDataUtil.put(pipelineCursor_1, "documentList", documentList);
pipelineCursor_1.destroy();
}


DocumentToJSON



public static final void documentToJSON(IData pipeline) throws ServiceException {

// pipeline

IDataCursor pipelineCursor = pipeline.getCursor();

IData document = IDataUtil.getIData(pipelineCursor, "document");

String callback = IDataUtil.getString(pipelineCursor, "callback");

 

if(document == null) {

return;

}

Map result = convertToMap(document, new HashMap());

Gson gson = new Gson();

String jsonString = gson.toJson(result);

if(callback != null && !callback.isEmpty()) {

jsonString = callback + "(" + jsonString + ")";

}

IDataUtil.put(pipelineCursor, "jsonString", jsonString);

pipelineCursor.destroy();

}


DocumentListToJSON



public static final void documentListToJSON(IData pipeline)

throws ServiceException {

// pipeline

IDataCursor pipelineCursor = pipeline.getCursor();

IData[] documentList = IDataUtil.getIDataArray(pipelineCursor, "documentList");

String callback = IDataUtil.getString(pipelineCursor, "callback");

 

if(documentList == null) {

return;

}

List> result = new ArrayList>();

for(IData data : documentList) {

result.add(convertToMap(data, new HashMap()));

}

Gson gson = new Gson();

String jsonString = gson.toJson(result);

if(callback != null && !callback.isEmpty()) {

jsonString = callback + "(" + jsonString + ")";

}

IDataUtil.put(pipelineCursor, "jsonString", jsonString);

pipelineCursor.destroy();

}


SHARED CODE



public static Map convertToMap(IData data, Map map) {
IDataCursor dataCursor = data.getCursor();
while(dataCursor.next()) {
Object key = dataCursor.getKey();
Object val = dataCursor.getValue();
if (val instanceof IData[]) {
IData[] ida = (IData[]) val;
List> valueList = new ArrayList>();
for (int l = 0; l < ida.length; l++) {
valueList.add(convertToMap(ida[l], new HashMap()));
}
map.put(String.valueOf(key),valueList);
} else if (val instanceof IData) {
map.put(String.valueOf(key),convertToMap((IData)val, new HashMap()));
} else {
map.put(String.valueOf(key), val);
}
}
dataCursor.destroy();
return map;
}

 

public static IData convertToIData(Map map) {
IData doc = IDataFactory.create();
IDataCursor docCursor = doc.getCursor();
for(Entry entry : map.entrySet()) {
if(entry.getValue() instanceof List) {
List> list = (ArrayList>) entry.getValue();
List docList = new ArrayList();
for(Map m : list) {
docList.add(convertToIData(m));
}
IData[] docArray = new IData[docList.size()];
IDataUtil.put(docCursor, entry.getKey(), docList.toArray(docArray));
} else {
IDataUtil.put(docCursor, entry.getKey(), entry.getValue());
}
}
docCursor.destroy();
return doc;
}





 








THURSDAY, 8 AUGUST 2013





Integration Patterns: Internal vs External Applications


 


INTRODUCTION


I've been working on a lot of projects that deal with both internal & external system integration and the same question always arises - "What interface scheme should we use to provide/consume data from these systems?" Speaking with a lot of clients and their internal IT team, you hear a lot of different methods that have been used before. They all will potentially work, but sometimes things are overly architected. With the industry slowly moving away from your standard SDLC and more towards AGILE, the way we develop our solutions also need to change.

EXTERNAL SYSTEMS


My idea is that when integrating with an external system, you should make your interfaces strict. What I mean by this is that there should be some sort of schema to validate the request with strong data types. These interfaces shouldn't be changing very often and since there are other organisations that could be using them, you want to cause minimal impact.
I think using WSDLs or a canonical built of an XSD is the way to go with this approach. This caters for both SOAP and other communication means like JMS.

INTERNAL SYSTEMS


For internal system integration, you will usually have databases, CRMs, finance systems etc. Most of these systems will have their own API's/methods for communication, but if you are going to be the provider of the service, I'd recommend using RESTful services with XML or JSON, if the end system can communicate via HTTP, or JMS. Both are lightweight and work well with an AGILE methodology. Internally you will usually have more changes due to the business not knowing exactly what they want.
These aren't strict principles that I follow and each implementation is different based on the requirements.
Let me know in the comments on your thoughts and opinions.
 








TUESDAY, 22 MAY 2012





Apache Camel Tutorial on EIP, Routes and Testing


 

Interesting blog about Apache Camel. I'll be definitely playing around with this on the weekend and post my findings about it.

Please read it at the following link - http://www.kai-waehner.de/blog/2012/05/04/apache-camel-tutorial-introduction/





Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect