Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialization format compatible with Structured Clone Algorithm?

Tags:

html

Does one exist? The Structured Clone Algorithm is defined for HTML5 to allow browsers a consistent mechanism to persist and transfer complex Javascript objects, used by IndexedDB and window.postMessage() but it is NOT a serialization format. Does anyone know of such a format that can represent everything valid in the SCA?

In addition to types JSON can represent, this has to include cycling references between objects, and also RegExp objects, Blob, File, FileList, and ImageData objects.

Does anything fit the bill?

like image 255
ironfroggy Avatar asked Jan 01 '13 01:01

ironfroggy


1 Answers

The existence of a structured clone algorithm in html5-browsers is

http://www.w3.org/TR/2011/WD-html5-20110525/common-dom-interfaces.html#safe-passing-of-structured-data

It is implemented at least in FF:

https://developer.mozilla.org/en-US/docs/DOM/The_structured_clone_algorithm

I think to use it you just give a graph of objects (may be cyclic) into the message argument of:

window.postMessage(message, targetOrigin); .

https://developer.mozilla.org/en-US/docs/DOM/window.postMessage

It says so implicitly here:

Note: Prior to Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3), the message parameter must be a string. Starting in Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3), the message parameter is serialized using the structured clone algorithm. This means you can pass a broad variety of data objects safely to the destination window without having to serialize them yourself.

like image 71
Bernd Elkemann Avatar answered Nov 01 '22 14:11

Bernd Elkemann