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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
define(['./util', './handlers', './tags'], function(util, handlers, tags) {
var pickler = {};
pickler.encode = function(value, options) {
var params = {
unpicklable: false,
make_refs: true,
keys: false,
max_depth: undefined,
reset: true,
backend: undefined, // does nothing; python compat
context: undefined,
};
util.merge(params, options);
if (params.context === undefined) {
outparams = {
unpicklable: params.unpicklable,
make_refs: params.make_refs,
keys: params.keys,
backend: params.backend,
max_depth: params.max_depth,
};
params.context = new pickler.Pickler(outparams);
var fixed_obj = params.context.flatten(value, params.reset);
return JSON.stringify(fixed_obj);
}
};
pickler.Pickler = function (options) {
var params = {
unpicklable: true,
make_refs: true,
max_depth: undefined,
backend: undefined,
keys: false,
};
util.merge(params, options);
this.unpicklable = params.unpicklable;
this.make_refs = params.make_refs;
this.backend = params.backend;
this.keys = params.keys;
this._depth = -1;
this._max_depth = params.max_depth;
this._objs = [];
this._seen = [];
};
pickler.Pickler.prototype.reset = function () {
this._objs = [];
this._depth = -1;
this._seen = [];
};
pickler.Pickler.prototype._push = function () {
this._depth += 1;
};
pickler.Pickler.prototype._pop = function (value) {
this._depth -= 1;
if (this._depth == -1) {
this.reset();
}
return value;
};
pickler.Pickler.prototype._mkref = function (obj) {
var found_id = this._get_id_in_objs(obj);
//console.log(found_id);
if (found_id != -1) {
return false;
// if (this.unpicklable == false || this.make_refs == false) {
// return true;
// } else {
// return false;
// }
}
this._objs.push(obj);
return true;
};
pickler.Pickler.prototype._get_id_in_objs = function (obj) {
var objLength = this._objs.length;
// console.log('sought obj', obj);
// console.log('stored objs: ', this._objs);
for (var i = 0; i < objLength; i++) {
if (obj === this._objs[i]) {
return i;
}
}
return -1;
};
pickler.Pickler.prototype._getref = function (obj) {
var wrap_obj = {};
wrap_obj[tags.ID] = this._get_id_in_objs(obj);
return wrap_obj;
};
pickler.Pickler.prototype.flatten = function (obj, reset) {
if (reset === undefined) {
reset = true;
}
if (reset == true) {
this.reset();
}
var flatOut = this._flatten(obj);
console.log(this._objs);
return flatOut;
};
pickler.Pickler.prototype._flatten = function (obj) {
this._push();
return this._pop(this._flatten_obj(obj));
};
pickler.Pickler.prototype._flatten_obj = function (obj) {
this._seen.push(obj);
max_reached = (this._depth == this._max_depth) ? true : false;
if (max_reached || (this.make_refs == false && this._get_id_in_objs(obj) != -1)) {
// no repr equivalent, use str;
return toString(obj);
} else {
var flattener = this._get_flattener(obj);
//console.log(flattener);
return flattener.call(this, obj);
}
};
pickler.Pickler.prototype._list_recurse = function (obj) {
var l = [];
for (var i = 0; i < obj.length; i ++) {
l.push(this._flatten(obj[i]));
}
return l;
};
pickler.Pickler.prototype._get_flattener = function (obj) {
if (util.is_primitive(obj)) {
return function (obj) { return obj; };
}
if (util.is_list(obj)) {
if (this._mkref(obj)) {
return this._list_recurse;
} else {
this._push();
return this._getref;
}
}
if (util.is_tuple(obj)) {
if (this.unpicklable == false) {
return this._list_recurse;
} else {
return function (obj) {
var obj_wrap = {};
obj_wrap[tags.TUPLE] = this._list_recurse(obj);
};
}
}
if (util.is_set(obj)) {
if (this.unpicklable == false) {
return this._list_recurse;
} else {
return function (obj) {
var obj_wrap = {};
obj_wrap[tags.SET] = this._list_recurse(obj);
};
}
}
// better -- translate as object...
//if (util.is_dictionary(obj)) {
// return this._flatten_dict_obj;
//}
//if (util.is_type(obj)) {
// return _mktyperef;
//}
if (util.is_object(obj)) {
return this._ref_obj_instance;
}
console.log('no flattener for ', obj, ' of type ', typeof obj);
return undefined;
};
pickler.Pickler.prototype._ref_obj_instance = function (obj) {
if (this._mkref(obj)) {
return this._flatten_obj_instance(obj);
}
return this._getref(obj);
};
pickler.Pickler.prototype._flatten_obj_instance = function (obj) {
var data = {};
has_class = (obj[tags.PY_CLASS] !== undefined); // ? or ...
has_dict = true;
has_slots = false;
has_getstate = (obj.__getstate__ !== undefined);
if (has_class && util.is_module(obj) == false) {
var fullModuleName = pickler._getclassdetail(obj);
if (this.unpicklable) {
data[tags.OBJECT] = fullModuleName;
console.log(data);
}
handler = handlers[fullModuleName];
if (handler !== undefined) {
handler.flatten(obj, data);
}
}
if (util.is_module(obj)) {
// todo if possible to have this happen...
}
if (util.is_dictionary_subclass(obj)) {
// todo if possible to have this happen...
}
if (has_dict) {
// where every object currently ends up
if (util.is_sequence_subclass(obj)) {
// todo if possible to be...
}
if (has_getstate) {
return this._getstate(obj, data);
}
return this._flatten_dict_obj(obj, data);
}
// todo: is_sequence_subclass
// todo: is_noncomplex
// todo: has_slots...
};
pickler.Pickler.prototype._flatten_dict_obj = function (obj, data) {
if (data === undefined) {
data = new obj.prototype.constructor();
}
var key_index = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
key_index.push(key);
}
}
for (var i = 0; i < key_index.length; i++) {
var key = key_index[i];
var value = obj[key];
if (key === tags.PY_CLASS) {
continue;
}
this._flatten_key_value_pair(key, value, data);
}
// default_factory...
return data;
};
// _flatten_newstyle_with_slots
pickler.Pickler.prototype._flatten_key_value_pair = function (k, v, data) {
if (util.is_picklable(k, v) == false) {
return data;
}
// assume all keys are strings -- Javascript;
data[k] = this._flatten(v);
return data;
};
//pickler.Pickler.prototype._flatten_sequence_obj = function () {};
pickler.Pickler.prototype._getstate = function (obj, data) {
var state = this._flatten_obj(obj.__getstate__());
if (this.unpicklable) {
data[tags.STATE] = state;
} else {
data = state;
}
return data;
};
//pickler._mktyperef = function (obj) {};
pickler._getclassdetail = function(obj) {
// just returns the Python class name
return obj[tags.PY_CLASS];
};
if (jsonpickle !== undefined) {
jsonpickle.pickler = pickler;
}
return pickler;
});
|