VMware Cloud Community
qc4vmware
Virtuoso
Virtuoso

Working with vCACCAFELiteralMap class... convert to JSON?

I really hate working with the vCACCAFELiteralMap class.  I've written a small utility to convert them to JSON so I can more easily explore the contents and interact with the data contained within.  Before I spend too much more time on this effort I wanted to make sure I wasn't missing a built in way to accomplish this.  Its not really an issue getting the data once I know where things are its just a pain picking it apart.  I'll include my code as well for comment.  I'm using the .getExtensionData() method from a reservation as my test data

var literalMap = res.getExtensionData();

var jsonObj = convertLiteralMapToJson(literalMap);

System.debug(JSON.stringify(jsonObj));

function convertLiteralMapToJson(literalMap) {

var mapObj = {};

for each (var key in literalMap.keySet()) {

var obj = literalMap.get(key);

var className = System.getObjectClassName(obj);

if (className == "vCACCAFEStringLiteral" || "vCACCAFEDateTimeLiteral" || "vCACCAFEIntegerLiteral" ||

    "vCACCAFEBooleanLiteral" || "vCACCAFEDecimalLiteral" || "vCACCAFESecureStringLiteral") mapObj[key] = obj.getValue();

if (className == "vCACCAFEComplexLiteral") mapObj[key] = convertComplexLiteralToJson(obj);

if (className == "vCACCAFEMultipleLiteral") {

multClassName = System.getObjectClassName(obj.getValue());

mapObj[key] = [];

for each (var item in obj.getValue()) {

var itemClassName = System.getObjectClassName(item);

if (itemClassName == "vCACCAFEComplexLiteral") {

mapObj[key].push(convertComplexLiteralToJson(item));

}

else {

System.debug("unhandled itemClassName::" + itemClassName);

}

}

}

if (className == "vCACCAFEEntityReference" ) {

mapObj[key] = {};

mapObj[key].referenceType = "vCACCAFEEntityReference";

mapObj[key].classId = obj.getClassId();

mapObj[key].componentId = obj.getComponentId();

mapObj[key].id = obj.getId();

mapObj[key].label = obj.getLabel();

mapObj[key].typeId = obj.getTypeId();

mapObj[key].label = obj.getLabel();

}

if (mapObj[key] == undefined) {

System.log(key + " unhandled className::" + className);

mapObj[key] = "QCconvertLiteralMapToJson: This mapping has an unhandled class for key:" + key + ", class: " + classname ;

}

};

return mapObj;

};

function convertComplexLiteralToJson(complexLiteral) {

var complexClass = System.getObjectClassName(complexLiteral.getValue());

if(complexClass == "vCACCAFELiteralMap") {

return convertLiteralMapToJson(complexLiteral.getValue());

}

else {

System.log("not sure what to do with complexClass::" + complexClass);

return "QCconvertComplexLiteralToJson: This complex literal has an unhandled class:" + complexClass ;

}

}

Reply
0 Kudos
1 Reply
xian_
Expert
Expert

hi Paul,

thanks for the code, was useful. I had to make a few corrections, though. I'm processing a request data of type LiteralMap and some values can be null, and that broke your code.

So my modifications:

        if (obj != null) var className = System.getObjectClassName(obj);

        else { var className = null; mapObj[key] = null;}

Also, this condition is wrong, the correct one is (I think I'll modify this to a switch case):

        if (className == "vCACCAFEStringLiteral" || className == "vCACCAFEDateTimeLiteral" || className == "vCACCAFEIntegerLiteral" ||

            className == "vCACCAFEBooleanLiteral" || className == "vCACCAFEDecimalLiteral" || className == "vCACCAFESecureStringLiteral") mapObj[key] = obj.getValue();

And lastly, I modified the unhandled class type condition to strict equals as I handle null values above:

       if (mapObj[key] === undefined) {

Reply
0 Kudos