MCPcopy
hub / github.com/jupyter-widgets/ipywidgets / serialize

Method serialize

packages/base/src/widget.ts:563–584  ·  view source on GitHub ↗

* Serialize widget state. * * A serializer is a function which takes in a state attribute and a widget, * and synchronously returns a JSONable object. The returned object will * have toJSON called if possible, and the final result should be a * primitive object that is a snapshot of t

(state: Dict<any>)

Source from the content-addressed store, hash-verified

561 * binary array buffers.
562 */
563 serialize(state: Dict<any>): JSONObject {
564 const serializers =
565 (this.constructor as typeof WidgetModel).serializers ||
566 JSONExt.emptyObject;
567 for (const k of Object.keys(state)) {
568 try {
569 if (serializers[k] && serializers[k].serialize) {
570 state[k] = serializers[k].serialize!(state[k], this);
571 } else {
572 // the default serializer just deep-copies the object
573 state[k] = JSON.parse(JSON.stringify(state[k]));
574 }
575 if (state[k] && state[k].toJSON) {
576 state[k] = state[k].toJSON();
577 }
578 } catch (e) {
579 console.error('Error serializing widget state attribute: ', k);
580 throw e;
581 }
582 }
583 return state;
584 }
585
586 /**
587 * Send a sync message to the kernel.

Callers 3

syncMethod · 0.95
widget_test.tsFile · 0.80
serialize_stateFunction · 0.80

Calls 1

toJSONMethod · 0.65

Tested by

no test coverage detected