##// END OF EJS Templates
Change serialize/deserialize to to_json/from_json
Jason Grout -
Show More
@@ -197,9 +197,9 class Widget(LoggingConfigurable):
197 197 keys = self.keys if key is None else [key]
198 198 state = {}
199 199 for k in keys:
200 f = self.trait_metadata(k, 'serialize')
200 f = self.trait_metadata(k, 'to_json')
201 201 if f is None:
202 f = self._serialize_trait
202 f = self._trait_to_json
203 203 value = getattr(self, k)
204 204 state[k] = f(value)
205 205 return state
@@ -287,9 +287,9 class Widget(LoggingConfigurable):
287 287 """Called when a state is received from the front-end."""
288 288 for name in self.keys:
289 289 if name in sync_data:
290 f = self.trait_metadata(name, 'deserialize')
290 f = self.trait_metadata(name, 'from_json')
291 291 if f is None:
292 f = self._deserialize_trait
292 f = self._trait_from_json
293 293 value = f(sync_data[name])
294 294 with self._lock_property(name, value):
295 295 setattr(self, name, value)
@@ -309,30 +309,30 class Widget(LoggingConfigurable):
309 309 """Called when a view has been displayed for this widget instance"""
310 310 self._display_callbacks(self, **kwargs)
311 311
312 def _serialize_trait(self, x):
313 """Serialize a trait value to json
312 def _trait_to_json(self, x):
313 """Convert a trait value to json
314 314
315 315 Traverse lists/tuples and dicts and serialize their values as well.
316 316 Replace any widgets with their model_id
317 317 """
318 318 if isinstance(x, dict):
319 return {k: self._serialize_trait(v) for k, v in x.items()}
319 return {k: self._trait_to_json(v) for k, v in x.items()}
320 320 elif isinstance(x, (list, tuple)):
321 return [self._serialize_trait(v) for v in x]
321 return [self._trait_to_json(v) for v in x]
322 322 elif isinstance(x, Widget):
323 323 return "IPY_MODEL_" + x.model_id
324 324 else:
325 325 return x # Value must be JSON-able
326 326
327 def _deserialize_trait(self, x):
327 def _trait_from_json(self, x):
328 328 """Convert json values to objects
329 329
330 We explicitly support converting valid string widget UUIDs to Widget references.
330 Replace any strings representing valid model id values to Widget references.
331 331 """
332 332 if isinstance(x, dict):
333 return {k: self._deserialize_trait(v) for k, v in x.items()}
333 return {k: self._trait_from_json(v) for k, v in x.items()}
334 334 elif isinstance(x, (list, tuple)):
335 return [self._deserialize_trait(v) for v in x]
335 return [self._trait_from_json(v) for v in x]
336 336 elif isinstance(x, string_types) and x.startswith('IPY_MODEL_') and x[10:] in Widget.widgets:
337 337 # we want to support having child widgets at any level in a hierarchy
338 338 # trusting that a widget UUID will not appear out in the wild
General Comments 0
You need to be logged in to leave comments. Login now