- Getting Started
With the appropriate files imported, we can get to the serialization by simply adding two div elements and an onload event to the HTML document. The two div elements will have the IDs body and loading. The loading tag will be used by the AJAX engine to show progress, and the body tag will be used to display messages.
<div id="loading"></div> <div id="body"></div>
<body onload="document.getElementById(’body’).innerHTML = ’<b>Local objects serialized as JSON</b><br/>Auto Object: ’+ Auto.toJSONString();">
This code takes the Auto object and all of its children and serializes them as a JSON string with the JSON parser’s toJSONString method. This data can then be used as a data-interchange format for the server.
Remember that earlier we called the setColor method to change the color of the Car object. I used it then because I wanted to show you that serialization can take place at any point during runtime, but will reflect up-to-date data from the objects.