$JSONtoYAML(&in[, &out])
Converts JSON to YAML
&in Binary variable containing JSON data
&out Binary variable to store the output YAML in - If not specified, the result overwrites the contents of &in
Returns $true if sucessful
Returns $false <reason> if failure

$XMLtoYAML(&in[, &out])
Converts XML to YAML
&in Binary variable containing XML data
&out Binary variable to store the output YAML in - If not specified, the result overwrites the contents of &in
Returns $true if sucessful
Returns $false <reason> if failure

Code
alias JSONtoYAML {
  if ($0 < 2) {
    return $toYAML($1)
  }
  return $toYAML($1, $2)
}
alias XMLtoYAML {
  if ($0 < 2) {
    return $toYAML($1).xml
  }
  return $toYAML($1, $2).xml
}

;; jscript that does the heavy lifting
alias -l js {
  return (function(A,B,C){C.async=!1;function D(a,b){if(a===undefined)return;var c=A(a),d,e,f,g,h=[];if(a===null||a===!!a||(c=='number'&&isFinite(a)))return''+a;if(c=='string')return B(a);if(c=='array'){g=Array(b+1).join(' ')+'-';for(d=0;d<a.length;d+=1){if(a[d]===undefined)continue;c=A(e=a[d]);if(c=='string')h.push(g+' '+B(e));else if(e=== null||e===!!e||(c=='number'&&isFinite(e)))h.push(g+' '+e);else if(c=='array'){e=D(e,b+2);if(e=='[]')h.push(g+' []');else h.push(g,e)}else if(c=='object'){e=D(e,b+2);if(e=='{}')h.push(g+' {}');else h.push(g,e)}else continue;f=!0}return f?h.join('\n'):'[]'}if(c=='object'){for(d in a){if(!Object.prototype.hasOwnProperty.call(a,d)||a[d]===undefined)continue;e=a[d];c=A(e);g=Array(b+1).join(' ')+B(d)+':';if(c=='string')h.push(g+' '+B(e));else if(e===null||e===!!e||(c=='number'&&isFinite(e)))h.push(g+' '+e);else if(c=='array'){e=D(e,b+2);if(e=='[]')h.push(g+' []');else h.push(g,e)}else if(c=='object'){e=D(e,b+2);if(e=='{}')h.push(g+' {}');else h.push(g,e)}else continue;f=!0}return f?h.join('\n'):'{}'}}function E(a,b){var c=(a.text===undefined||a.text===null||a.text==='')?!0:a.text,d={'@value':c},e=0,f;if(a.attributes.length==0&&a.childNodes.length==0)d=c;else{for(;e<a.attributes.length;e++){f=a.attributes[e];d['@@'+f.nodeName]=f.text===undefined?!0:f.text}for(e=0;e<a.childNodes.length;e++){f=a.childNodes[e];if(f.nodeType==1)E(f,d)}}if(Object.prototype.hasOwnProperty.call(b,a.nodeName)){if(A(b[a.nodeName])=='array')b[a.nodeName].push(d);else b[a.nodeName]=[b[a.nodeName],d]}else b[a.nodeName]=d;return b}xml2yaml=function(a,b){try{b=C.loadXML(a)}catch(e){throw new Error('INVALID_XML')}if(!b||C.documentElement==null)throw new Error('INVALID_XML');return D(E(C.documentElement,{}),0)};json2yaml=function(a){a=a.replace(/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,function(a){return'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4)});if(!/^[\],:{}\s]*$/.test(a.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,'')))throw new Error("INVALID_JSON");try{a=eval('('+a+')')}catch(e){throw new Error("INVALID_JSON")}return D(a,0)}}(function(a){return Object.prototype.toString.call(a).match(/^\[object (\S+)\]$/)[1].toLowerCase()},function(a){if(a==='')return'""';if(/(?:^[!&*-?#|>@`"' ])|[{}\[\]\(\),:\\\/\t\r\n]|(?:\x20$)|(?:^(?:void|null|true|false)$)/g.test(a)){return'"'+a.replace(/[\\"\u0000-\u001F\u2028\u2029]/g,function(a){return{'"':'\\"','\\':'\\\\','\b':'\\b','\f':'\\f','\n':'\\n','\r':'\\r','\t':'\\t'}[a]||'\\u'+(a.charCodeAt(0)+0x10000).toString(16).substr(1)})+'"';}return a},new ActiveXObject('Msxml2.DOMDocument.6.0')))
}

;; cleanup because we ain't filthy hobbits
alias -l cleanup {
  if ($com(SReject/toYAML/JSEngine)) {
    .comclose SReject/toYAML/JSEngine
  }
  if ($com(SReject/toYAML/JSShell)) {
    .comclose SReject/toYAML/JSShell
  }
  if ($timer(SReject/toYAML/cleanup)) {
    .timerSReject/toYAML/cleanup off
  }
}

;; does the heavy lifting
alias toYAML {


  if (!$isid) {
    if ($1 == cleanup || $1 == -c) {
      cleanup
    }
  }

  var %call = $iif($prop === xml, xml2yaml, json2yaml)
  var %out = $iif($0 < 2, $1, $2)

  ;; prep COM
  if (!$com(SReject/toYAML/JSShell) || !$com(SReject/toYAML/JSEngine)) {
    cleanup
    .comopen SReject/toYAML/JSShell MSScriptControl.ScriptControl
    if (!$com(SReject/toYAML/JSShell) || $comerr) {
      cleanup
      return $false Failed to create script engine
    }
    elseif (!$com(SReject/toYAML/JSShell, language, 4, bstr, jscript) || $comerr) {
      cleanup
      return $false Failed to set script engine language
    }
    elseif (!$com(SReject/toYAML/JSShell, AllowUI, 4, bool, $false) || $comerr) {
      cleanup
      return $false Failed to disable script engine UI
    }
    elseif (!$com(SReject/toYAML/JSShell, timeout, 4, integer, -1) || $comerr) {
      cleanup
      return $false Failed to set script engine time out
    }
    elseif (!$com(SReject/toYAML/JSShell, ExecuteStatement, 1, bstr, $js) || $comerr) {
      cleanup
      return $false Failed to initialize jscript
    }
    elseif (!$com(SReject/toYAML/JSShell, Eval, 1, bstr, this, dispatch* SReject/toYAML/JSEngine) || $comerr || !$com(SReject/toYAML/JSEngine)) {
      cleanup
      return $false Failed to retrieve js engine reference
    }
  }

  ;; cleanup in 5 seconds - done to allow for batching
  .timerSReject/toYAML/Cleanup -io 1 5 cleanup

  ;; call appropriate conversion function
  if (!$com(SReject/toYAML/JSEngine, %call, 1, &bstr, $1) || $comerr) {
    return $false Conversion failed
  }

  ;; clear output bvar  
  if ($bvar(%out)) {
    bunset %out
  }

  ;; fill output bvar with yaml result
  noop $com(SReject/toYAML/JSEngine, %out).result

  return $true
}

Last edited by FroggieDaFrog; 11/08/19 10:28 AM.

I am SReject
My Stuff