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)
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
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;
}
IDataCursor dataCursor = data.getCursor();
while(dataCursor.next()) {
Object key = dataCursor.getKey();
Object val = dataCursor.getValue();
if (val instanceof IData[]) {
IData[] ida = (IData[]) val;
List
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;
}
IData doc = IDataFactory.create();
IDataCursor docCursor = doc.getCursor();
for(Entry
if(entry.getValue() instanceof List) {
List
List
for(Map
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;
}
Thank you! Your code saved my time!
ReplyDelete