вторник, 17 мая 2016 г.

MUMPS + JSON

How to get data in JSON format from the MUMPS?
Tony Toucan (toucan@spuddy.mew.co.uk) wrote on the MUMPS encoder of ordinal MUMPS structures into the JSON format. See detailed description at http://www.cachewiki.org/index.php/JavaScript_ObjectScript_Notation_(J SON)_Builder. And this code can be adopted to any MUMPS implementation, which does not contain JSON support by default.

If routine from Tony Toucan is main part on the server side, on the client side main technology are Javascript and detailed description of JSON from W3C, for example: JSON Tutorial from W3SCHOOLS.

To get data in the MUMPS code we can get data from MWA page where we call MUMPS to get data and encode it in the JSON format. See set of JSON examples files at jsonsample.zip.

Routine %JSON must be imported into the "%SYS" database of MiniM and is accessible from any database becouse name starts with the percent symbol.

File jsonstart.html is root file which call two json examples: static file json.txt and dynamic MWA page json.mwa.

Example uses builtin Javascript object JSON. This object is builtin in the modern WEB browsers Firefox 3.5, Internet Explorer 8, Chrome, Opera 10, Safari 4. For older browsers, a JavaScript library is available at https://github.com/douglascro ckford/JSON-js.

Also, older browsers without the support for the JavaScript function JSON.parse() can use the eval() function to convert a JSON text into a JavaScript object:
var obj = eval ("(" + text + ")");
JSON example calls static JSON data using
getjson("json.txt");
This file contains one simple object
{
  "name": "Simpson"
}
And second JSON example calls dynamic JSON generator
getjson("json.mwa");
This file contains of two lines:
<?exec s data("name")=$zv ?>
<?eval $$FromArray^%JSON("data") ?>
Here first line constructs data with property "name" and assign value of current MUMPS version. And second line outputs given data as HTTP responce.

After both JSON downloaded and parsed, html page shows info about JSON object:
// low-level function, call server by given url
function getjson(url)
{
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status ==  200) {
        var getobj = JSON.parse(xmlhttp.responseText);
        show_object(getobj);
    }
  };
  xmlhttp.open("GET", url, true);
  xmlhttp.send();
}

// this event fired after JSON object downloaded from the server
// and parsed as a Javascript object
function show_object(getobj)
{
  document.getElementById("output").innerHTML = 
    "We got info about object with name: " + getobj.name;
  return false;
}
For showing data this code replaces inner HTML code of HTML object with name "output":
<p id=output></p>
This is very simple example how to generate on the MUMPS side and get to the WEB browser side data in the JSON format. Real complex applications differ by using more complex data. See MWA documentation how to handle CGI variables sent by the WEB browser and JSON Tutorial how to handle embedded objects and arrays of JSON.

Download set of JSON examples files: jsonsample.zip

See other MiniM Knowledge Base articles.

Комментариев нет:

Отправить комментарий