Debian Codegraph @

jsonpickleJS/pickler.js

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