HTML and CSS Reference
In-Depth Information
experience any troubles when you deserialize, but only when (and if) you ever happen to work on
deserialized data. In other words, the exception you may run into may come from places in your code
that are not directly related to the deserialization operation.
What can you do?
The fix is actually quick and easy, but it needs be applied on a per-call basis.
// jsonDate is the string you get for a date from JSON.parse.
// To get a date object, you just pass this string to the constructor of the Date object.
var date = new Date(jsonDate);
You need to apply the fix when your applications deserialize from JSON and dates are involved.
Here's the final version of the code you need to have in the
TodoList.invokeOpenPicker
method:
openPicker.pickSingleFileAsync().then(function (file) {
if (file) {
var io = Windows.Storage.FileIO;
io.readTextAsync(file)
.done(function (json) {
var task = TodoList.deserializeTask(json);
TodoList.displayTask(task);
},
function () { TodoList.alert("Unable to read the file") });
}
});
You first deserialize the JSON string to a
Task
object and then you fix any
Date
properties. To keep
code cleaner, you might want to move the deserialization code to a specialized method in
todolist.js
:
TodoList.deserializeTask = function (json) {
var task = JSON.parse(json);
task.dueDate = new Date(task.dueDate);
return task;
}
Figure 10-5 shows the process of selecting a task and Figure 10-6 shows the selected task
displayed in the application's user interface.
Search WWH ::
Custom Search