Saturday, December 14, 2013

Book Review: JavaScript and JSON Essentials

Packt Publishing recently invited me to review JavaScript and JSON Essentials. They provided this book authored by Sai Srinivas Sriparasa in electronic format. Note that Sriparasa has also written a Packt Publishing article called Getting Started with JSON.

The Preface of JavaScript and JSON Essentials describes optional prerequisites for readers of the book: "It ... would be good to have some knowledge about HTML and JavaScript. Some familiarity with server-side languages such as PHP, C#, or Python would be preferred, but not required."

Chapter 1: JavaScript Basics

The initial chapter of JavaScript and JSON Essentials provides history and background on JavaScript. It describes JavaScript's relationship to HTML, CSS, and web browsers. It then moves to coverage of JavaScript syntax basics.

Chapter 2: Getting Started with JSON

The second chapter of JavaScript and JSON Essentials introduces JSON (JavaScript Object Notation) with a very brief historical background and emphasizes that "JSON is a format and not a language." The author wastes no time in touting some of the basics and virtues of JSON: "JSON is derived from JavaScript and bears a close resemblance to JavaScript objects, but it is not dependent on JavaScript. JSON is language-independent, and support for the JSON data format is available in all the popular languages." This chapter also provides a very basic comparison of JSON to XML as data interchange formats.

JSON's MIME representations, a sample Hello World example, and a comparison of JSON to JavaScript objects are all provided in the second chapter.

The second chapter includes other aspects you'd expect in a chapter introducing a format. For example, it describes JSON data types and programming languages that support JSON (with emphasis on PHP and Python).

Chapter 3: Working with Real-time JSON

With the basics of JavaScript covered in Chapter 1 and the basics of JSON covered in Chapter 2, Chapter 3 covers using JSON in "real-world applications." There is significant discussion and examples in this chapter of parsing and modifying static JSON with JavaScript.

Up until this point, the author used JavaScript's alert() to quickly and easily provide output from JavaScript code. I was pleased to see in this third chapter that the author explains the availability of console.log in modern browsers and explains some of the reasons console.log is preferred over alert().

Chapter 4: AJAX Calls with JSON Data

Chapter 4 moves JavaScript and JSON Essentials's focus from working with relatively static JSON data to working with dynamic JSON data. The chapter begins with a brief history of web application development and the move from synchronous communication to increasingly asynchronous communication (IFrame, XML HTTP ActiveX control, XMLHttpRequest). Sriparasa describes how the 'X' in AJAX (now commonly spelled Ajax) was originally for XML but that now JSON is commonly used instead of XML.

The fourth chapter spends considerable time explaining Ajax and demonstrating use of JSON with Ajax. The descriptive text and screen snapshots cover use of JSON with Ajax using straight JavaScript before demonstrating how to do this with jQuery (with emphasis on using jQuery.getJSON()). I really like that the author demonstrated implementing Ajax with direct JavaScript before introducing jQuery because I think it is advantageous to understand the underlying fundamental principles behind the libraries we use. This would probably be overkill in a book solely devoted to JSON, but seemed well-placed in a book that covers JavaScript's "essentials."

To illustrate the Ajax/JSON examples, Sriparasa first leads the reader through the "setting up Apache and PHP to develop server-side programs on a Linux machine" and "running a .NET-powered web application on Windows." As part of this, installation and use of the LAMP stack is covered and there are numerous screen snapshots of Visual Studio being used.

Chapter 5: Cross-domain Asynchronous Requests

JavaScript and JSON Essentials's fifth chapter begins with a deeper dive into the basic HTTP methods of GET and POST. The chapter mentions web debug tool Fiddler before going on to an example of POST-ing JSON data using jQuery.ajax() method, PHP, and MySQL. Another example in the fifth chapter leverages Reddit JSON-based APIs. JSONP (JSON with Padding) is introduced in Chapter 5 as part of the Reddit API-based example.

Chapter 6: Building the Carousel Application

The sixth chapter of JavaScript and JSON Essentials builds a "rotating notification board application" (a "photo gallery" style application) upon the concepts covered in the previous five chapters. The example is based on HTML, jQuery, and jQuery Cycle and spends a number of pages on applying jQuery Cycle.

Chapter 7: Alternate Implementations of JSON

The first six chapters of JavaScript and JSON focused on using JSON with HTML and JavaScript and on using JSON as a data interchange format, but the seventh chapter shifts focus to using JSON with other languages and in ways other than data interchange. Specifically, the author talks about using JSON capabilities of Composer (PHP) and Node Packaged Modules (Node.js) for dependency management, using JSON as a language-independent (Python and PHP get emphasized here) way to store application configuration data, and how JSON compares to YAML (described as "another software language-agnostic data interchange format").

Chapter 8: Debugging JSON

The final chapter of JavaScript and JSON Essentials, Chapter 8, looks at "different ways ... we can debug, validate, and format JSON." The chapter begins with a very high-level introduction to web browser support for diagnosing what's happening with JSON in the request or response and then delves more deeply into using Firebug with the Firefox browser and using the developer tools associated with Google Chrome, Safari, and Internet Explorer (F12) browsers.

Chapter 8's coverage of JSON validation tools looks at JSONLint. The author talso introduces JSON Editor Online as a JSON formatting tool. He explains some advantages of both tools and shows screen snapshots of each in use. Chapter 8's Summary's first paragraph summarizes the chapter and its other paragraph summarizes the book. One sentence from this book summary paragraph nicely summarizes what I feel is the ultimately accomplished objective of JavaScript and JSON Essentials: "JavaScript and JSON Essentials [is] targeted to provide you with an in-depth insight of how data can be stored and transferred in the JSON data format."

General Observations
  • JavaScript and JSON Essentials seems best suited for developers with only very basic JavaScript background or who have not used JavaScript regularly since the mid-2000s because its focus is on use of JSON, Ajax, and other JavaScript related technologies that have been more prevalent since around 2005. There is an introductory chapter on JavaScript, but a person with no experience with JavaScript might want to prepare for this book by also reviewing online references such as Introduction to JavaScript for the total beginner and A re-introduction to JavaScript (JS Tutorial).
  • JavaScript and JSON Essentials is a quick read. With eight chapters, just over 100 pages, and numerous screen snapshots (code and tools), this book is not overly long and does not waste a lot of space on unnecessary details.
  • JavaScript and JSON Essentials uses numerous color screen snapshots to demonstrate code and applications. There are aesthetic advantages (such as color coded syntax and better visual separation of code from prose) with these images and they do provide a sense of realistic applicability, but one downside is that image snapshots of code cannot be copied and pasted. Fortunately, all code examples associated with this book are available for download.
  • The screen snapshots of code listings have black and dark gray backgrounds with light fonts for the text. This works well for the electronic version of the book I reviewed, but it does not seem like it would work well nearly as well in a printed book with such dark backgrounds.
  • There are some minor editing-related issues such as cases where one sentence that would fit on a single line in the book is spread over multiple lines with only a few words on each of the lines. There are only two or three of these cases and most of the prose is readable.
Conclusion

JavaScript and JSON Essentials introduces Java Script, JSON, and Ajax and some of the most important aspects of each along with how to use them together.

No comments: