* 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>)
| 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. |
no test coverage detected