Serialization converts structured data into a representation suitable for storage or transmission. JSON serialization turns fields and values into a string; deserialization reconstructs data from that representation. section 1.2.3 section 6.3
A deceptively simple task becomes awkward when data is locked inside object-oriented classes. Each new JSON shape can require another class and repeated serialization and deserialization code, whereas data-oriented designs provide more generic access to data. section 1.2.3 section 1.3
Practical rules make serialized data easier to use:
- Serialize structured values with JSON.stringify before writing them to disk. section 15.4
- Deserialize stored or returned JSON with JSON.parse before comparing or processing its data. section 6.3
- Avoid comparing JSON strings directly: different field orders can serialize the same data. section 6.3
- Pretty-print JSON with an indentation argument when people need to inspect captured data. section 15.4
Testing a JSON search result
A search function can return JSON.stringify(results). A test should apply JSON.parse to that result and compare the resulting data with [bookInfo] for a matching query or [] for a nonmatching query. section 6.3
Listing 6.12 The code of Library.searchBooksByTitleJSON
Library.searchBooksByTitleJSON = function (libraryData, query) {
var catalogData = _.get(libraryData, "catalog");
var results = Catalog.searchBooksByTitle(catalogData, query);
var resultsJSON = JSON.stringify(results);
return resultsJSON;
};
Inspect a complete JSON-producing search function and trace how its result is tested after deserialization. section 6.3























