summaryrefslogtreecommitdiff
path: root/swat/scripting/client/encoder.js
blob: 4aa4cc095473d199c52952795d7ad176e4092275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
	client side js functions for encoding/decoding objects into linear strings

	Copyright Andrew Tridgell 2005
	released under the GNU GPL Version 2 or later
*/
/*
	usage:

	  enc = encodeObject(obj);
	  obj = decodeObject(enc);

       The encoded format of the object is a string that is safe to
       use in URLs

       Note that only data elements are encoded, not functions
*/

function count_members(o) {
	var i, count = 0;
	for (i in o) { 
		count++;  
	}
	return count;
}

function encodeObject(o) {
	var i, r = count_members(o) + ":";
	for (i in o) {
		var t = typeof(o[i]);
		if (t == 'object') {
			r = r + "" + i + ":" + t + ":" + encodeObject(o[i]);
		} else if (t == 'string') {
			var s = encodeURIComponent(o[i]).replace(/%/g,'#');
			r = r + "" + i + ":" + t + ":" + s + ":";
		} else if (t == 'boolean' || t == 'number') {
			r = r + "" + i + ":" + t + ":" + o[i] + ":";
		} else if (t == 'undefined' || t == 'null') {
			r = r + "" + i + ":" + t + ":";
		} else if (t != 'function') {
			alert("Unable to encode type " + t);
		}
	}
	return r;
}

function decodeObjectArray(a) {
	var o = new Object();
	var i, count = a[a.i]; a.i++;
	for (i=0;i<count;i++) {
		var name  = a[a.i]; a.i++;
		var type  = a[a.i]; a.i++;
		var value;
		if (type == 'object') {
			o[name] = decodeObjectArray(a);
		} else if (type == "string") {
			value = decodeURIComponent(a[a.i].replace(/#/g,'%')); a.i++;
			o[name] = value;
		} else if (type == "boolean") {
			value = a[a.i]; a.i++;
			if (value == 'true') {
				o[name] = true;
			} else {
				o[name] = false;
			}
		} else if (type == "undefined") {
			o[name] = undefined;
		} else if (type == "null") {
			o[name] = null;
		} else if (type == "number") {
			value = a[a.i]; a.i++;
			o[name] = value * 1;
		} else {
			alert("Unable to delinearise type " + type);
		}
	}
	return o;
}

function decodeObject(str) {
	var a = str.split(':');
	a.i = 0;
	return decodeObjectArray(a);
}