2 server side js functions for encoding/decoding objects into linear strings
4 Copyright Andrew Tridgell 2005
5 released under the GNU GPL Version 2 or later
10 enc = encodeObject(obj);
11 obj = decodeObject(enc);
13 The encoded format of the object is a string that is safe to
16 Note that only data elements are encoded, not functions
19 function __count_members(o) {
27 function __replace(str, old, rep) {
28 var s = string_init();
29 var a = s.split(old, str);
30 var j = s.join(rep, a);
31 return s.join(rep, a);
34 function encodeObject(o) {
35 var s = string_init();
36 var i, r = s.sprintf("%u:", __count_members(o));
39 if (t == 'object' && o[i] == null) {
43 r = s.sprintf("%s%s:%s:%s", r, i, t, encodeObject(o[i]));
44 } else if (t == "string") {
45 var enc = s.encodeURIComponent(o[i]);
46 var rep = __replace(enc, '%', '#');
47 r = s.sprintf("%s%s:%s:%s:",
48 r, i, t, __replace(s.encodeURIComponent(o[i]),'%','#'));
49 } else if (t == "boolean" || t == "number") {
50 r = s.sprintf("%s%s:%s:%s:", r, i, t, "" + o[i]);
51 } else if (t == "undefined" || t == "null") {
52 r = s.sprintf("%s%s:%s:", r, i, t);
54 println("Unable to linearise type " + t);
60 function decodeObjectArray(a) {
61 var s = string_init();
63 var i, count = a[a.i]; a.i++;
64 for (i=0;i<count;i++) {
65 var name = a[a.i]; a.i++;
66 var type = a[a.i]; a.i++;
68 if (type == 'object') {
69 o[name] = decodeObjectArray(a);
70 } else if (type == "string") {
71 value = s.decodeURIComponent(__replace(a[a.i],'#','%')); a.i++;
73 } else if (type == "boolean") {
74 value = a[a.i]; a.i++;
75 if (value == 'true') {
80 } else if (type == "undefined") {
82 } else if (type == "null") {
84 } else if (type == "number") {
85 value = a[a.i]; a.i++;
88 println("Unable to delinearise type " + t);
89 assert(t == "supported type");
95 function decodeObject(str) {
96 var s = string_init();
97 var a = s.split(':', str);
99 return decodeObjectArray(a);