You can use this simple function to turn JSON into Apex equivalent.
console.load('local://~example.js');
var apexify; | |
(function() { | |
apexify = function(value, indent) { | |
indent = indent || 0; | |
let indentation = 'string' === typeof indent | |
? indent | |
: new Array(indent + 1).join(' '); | |
let newLine = indentation ? '\n' : ''; | |
let allSpacing = indentation ? newLine + indentation : ''; | |
let space = indentation ? ' ' : ''; | |
let arrow = space + '=>' + space; | |
let comma = ',' + newLine; | |
return recurseApexify( | |
JSON.parse(JSON.stringify(value)), | |
indentation, | |
newLine, | |
allSpacing, | |
arrow, | |
comma | |
); | |
}; | |
function recurseApexify(value, indentation, newLine, allSpacing, arrow, comma) { | |
var typeName = Object.prototype.toString.call(value).slice(8, -1); | |
if (value === null || typeName === 'Number' || typeName === 'Boolean') { | |
return JSON.stringify(value); | |
} | |
if (typeName === 'Array') { | |
let hasData = value.length > 0; | |
let myNewLine = hasData ? newLine : ''; | |
return 'new List<Object>{' | |
+ myNewLine | |
+ value | |
.map(function(x) { | |
return recurseApexify(x, indentation, newLine, allSpacing, arrow, comma); | |
}) | |
.join(comma) | |
.replace(/^/gm, indentation) | |
+ myNewLine | |
+ '}'; | |
} | |
if (typeName === 'String') { | |
return "'" | |
+ value.replace(/'/g, "\\'").replace(/[^\x20-z]/g, substringify) | |
+ "'"; | |
} | |
let keys = Object.keys(value); | |
let hasData = keys.length > 0; | |
let myNewLine = hasData ? newLine : ''; | |
return 'new Map<String,Object>{' | |
+ myNewLine | |
+ keys | |
.map(function(key) { return recurseApexify(key) + arrow + recurseApexify(value[key], indentation, newLine, allSpacing, arrow, comma); }) | |
.join(comma) | |
.replace(/^/gm, hasData ? indentation : '') | |
+ myNewLine | |
+ '}'; | |
} | |
function substringify(x) { | |
return JSON.stringify(x).slice(1, -1); | |
} | |
})(); |
function apexifyInIO(value, tabSize) { | |
return apexify(JSON.parse(value), tabSize); | |
} |
// HIDE \\ | |
console.load('local://apexify.js'); | |
// \\ | |
var data = {"success": false, "error": "Authentication failed.", "u":{value:null}, user:{first:'Chris',last:'West',grades:[1,2,3,4,5],details:'sdf\n\t\rxcvxc'}}; | |
// Normal call without indents | |
console.log(apexify(data)); | |
// \\ | |
// Call with indenting (number of spaces) | |
console.log(apexify(data, 3)); | |
// \\ | |
// Call with indenting (specify single spacing) | |
console.log(apexify(data, '\t')); |
function getPageParts(params) { | |
return { | |
title: "Apexify - Convert JSON To Apex", | |
description: "Easily conversion any JSON into Salesforce Apex notation.", | |
buttons: { submit: 'Convert', clear: false } | |
}; | |
} | |
function getInputs(params) { | |
var inputs = params.inputs; | |
function onInput(value) { | |
try { | |
JSON.parse(value); | |
} | |
catch (e) { | |
this.reportError('The specified JSON is invalid.'); | |
} | |
} | |
return [ | |
{ | |
key: 'json', | |
label: 'JSON', | |
type: 'code', | |
lang: 'json', | |
value: inputs.json && inputs.json.value, | |
onInit: onInput, | |
onInput: onInput | |
} | |
]; | |
} | |
function getOutputs(params) { | |
var inputs = params.inputs; | |
var apexCode = apexify(params.inputs.json, 2); | |
return [ | |
{ | |
label: 'Salesforce Apex', | |
type: 'code', | |
lang: 'apex', | |
value: apexCode | |
}, | |
{ | |
label: 'Download Apex Code', | |
type: 'download-button', | |
value: apexCode, | |
downloadName: 'example.apex' | |
} | |
]; | |
} | |
eval(GIST_FILES['apexify.js'].text); |
{ | |
// Title of the IO App | |
"title": "apexify()", | |
// Name of the function responsable for transforming the input into the output. | |
"transform": "apexifyInIO", | |
// Where to find the code that will be eval'd. | |
"files": ["apexify.js", "~apexifyInIO.js"], | |
// Input setup | |
"input": { "language": "json" }, | |
// Output setup | |
"output": { "language": "apex" }, | |
// Parameters that can be set. | |
"params": [ | |
{ | |
"type": "number", | |
"label": "Indentation (Spaces)", | |
"name": "tabSize", | |
"value": 4 | |
} | |
], | |
// Type of YourJS IO-App | |
"type": "instant" | |
} |
Here is the Gistly page:
https://www.yourjs.com/gistly/d0240a1295d4df88d1e1c53bf911b241/~README.md