Parsing Dict files to Javascript objects
From WikiName
Revision as of 07:03, 5 December 2014 by Mattijs@arttech.nl (talk | contribs) (added info how to output a dict)
The javascript functions below convert a dict structure to a javascript object and back. Use them in a js object.
The example below assumes that your patch contains a [dict] object called 'myData'.
// example
var dataDict = new Dict("myData");
var data = new Object();
data = dict_to_jsobj(dataDict);
// returns or includes null if there is a dict without containing data.
function dict_to_jsobj(dict) {
if (dict == null) return null;
var o = new Object();
var keys = dict.getkeys();
if (keys == null || keys.length == 0) return null;
if (keys instanceof Array) {
for (var i = 0; i < keys.length; i++)
{
var value = dict.get(keys[i]);
if (value && value instanceof Dict) {
value = dict_to_jsobj(value);
}
o[keys[i]] = value;
}
} else {
var value = dict.get(keys);
if (value && value instanceof Dict) {
value = dict_to_jsobj(value);
}
o[keys] = value;
}
return o;
}
function jsobj_to_dict(o) {
var d = new Dict();
for (var keyIndex in o) {
var value = o[keyIndex];
if (!(typeof value === "string" || typeof value === "number")) {
var isEmpty = true;
for (var anything in value) {
isEmpty = false;
break;
}
if (isEmpty) {
value = new Dict();
}
else {
var isArray = true;
for (var valueKeyIndex in value) {
if (isNaN(parseInt(valueKeyIndex))) {
isArray = false;
break;
}
}
if (!isArray) {
value = jsobj_to_dict(value);
}
}
}
d.set(keyIndex, value);
}
return d;
}
When working with javascript objects it can be useful to print their contents to the max window.
// example printobj(data, "myData");
// this function will post a JS object's content to the max window
function printobj (obj, name) {
post("---- object " + name + "----" +"\n");
printobjrecurse(obj, name);
}
function printobjrecurse (obj, name) {
if (typeof obj === "undefined") {
post(name + " : undefined" +"\n");
return;
}
if (obj == null) {
post(name + " : null" +"\n");
return;
}
if ((typeof obj == "number") || (typeof obj == "string")) {
post(name + " :" + obj + "\n");
} else {
var num = 0;
for (var k in obj) {
if (obj[k] && typeof obj[k] == "object")
{
printobjrecurse(obj[k], name + "[" + k + "]");
} else {
post(name + "[" + k + "] : " + obj[k] +"\n")
}
num++;
}
if (num == 0) {
post(name + " : empty object" +"\n");
}
}
}
Outputting a dict back to your patch works as follows:
// example: takes a dictionary as input, parses the contents of the dict to an object, parses the object back to a dict and sends out the dict to an outlet. The resulting dict should be identical to the input dict.
function dictionary(dictName) {
var dataDict = new Dict(dictName);
var data = new Object();
data = dict_to_jsobj(dataDict);
var newDict = jsobj_to_dict(data);
outlet(0, "dictionary", newDict.name);
}
If saved as dictTest.js with the method above included, the example above should work with the patch below:
<code> ----------begin_max5_patcher---------- 541.3ocyUEraiBCD8L7Uf7Y1HafDn6s9OzaqphLfapi.Cx1jMYq5+9ZO1jjM MUKToD0CXz77X6273Ml2BCPkc6YJTzOi9UTPvagAA.jEHvGGfZo6qZnJHMTK SonaXnX2bZ1dMfWyqz7NAUdHZHcUAFiyyyFyRLzxEMLMrEIdPdMrvtxs+fPN KytA8XpDOpCRenm4nJBE8reJk9PCfhF2hdpt5UtXyZIqR6xOEms.GGkjuz9h jl.Qlwnmsq48vP6P7Dk.A62FReUEXxEL95Eb1mUvmjWTrsT8OySFHEYKVdTF Vhg.PEF2E5NV8ZCAMKYMUqk7xAsyeDbTLBPr1RFTK3XOROURaYZlbMSPKcLA CyAZ6bUXyop6DWULIeTLymo4ojJ1LSkKgrxJYO.BWR133Wx9LgNnxtxo5jVc e5bvD6qhk2pFm4TxooiY9RmPKLFOXpGkbZyMusxdjJ9e.PR5B7+0yPRKftsG .oK8aX21m7YYqJxJOOwT5o1IdiuF2qndy3pzSlw4onuvaXGsMWTiSVcgCE0v EW9+Sf3V7+UxUcCxpwB2esUzIpWan.WPstwyxwZ0OKoW400LwYdAC6UVNVej je3a+ToyEG004i41lHx2G5PtapCYJrgb2TmIQG7WjNNmMsueGSp76IvDy0Fa 6j1v7XHjKbgvNhjrc7w7cHTooEVaZFGjtls8Et+WgZ6pYRw.GtdHzdxuG9W. PZMbtA -----------end_max5_patcher----------- </code>