Beginning JSON - link.springer.com978-1-4842-0202-9/1.pdfApress and friends of ED books may be...
Transcript of Beginning JSON - link.springer.com978-1-4842-0202-9/1.pdfApress and friends of ED books may be...
Beginning JSON
Ben Smith
Beginning JSON
Copyright © 2015 by Ben Smith
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
ISBN-13 (pbk): 978-1-4842-0203-6
ISBN-13 (electronic): 978-1-4842-0202-9
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image, we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the author nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Managing Director: Welmoed SpahrLead Editor: Ben Renow-ClarkeTechnical Reviewer: Victor SumnerEditorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan,
Jim DeWolf, Jonathan Gennick, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Steve Weiss
Coordinating Editor: Christine RickettsCopy Editor: Michael G. LaraqueCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/.
To my wife, for her patience with the late evenings and stay-at-home weekends, as well as for her constant encouragement
v
Contents at a Glance
About the Author �����������������������������������������������������������������������������������������������������xv
About the Technical Reviewer �������������������������������������������������������������������������������xvii
Acknowledgments ��������������������������������������������������������������������������������������������������xix
Introduction ������������������������������������������������������������������������������������������������������������xxi
Chapter 1: JavaScript Basics ■ �������������������������������������������������������������������������������� 1
Chapter 2: Special Objects ■ ���������������������������������������������������������������������������������� 17
Chapter 3: String Manipulation ■ ��������������������������������������������������������������������������� 27
Chapter 4: Introducing JSON ■ ������������������������������������������������������������������������������� 37
Chapter 5: Creating JSON ■ ������������������������������������������������������������������������������������ 49
Chapter 6: Parsing JSON ■ ������������������������������������������������������������������������������������� 69
Chapter 7: Persisting JSON: I ■ ������������������������������������������������������������������������������ 81
Chapter 8: Data Interchange ■ ������������������������������������������������������������������������������ 101
Chapter 9: X-Origin Resources ■ �������������������������������������������������������������������������� 133
Chapter 10: Serving JSON ■ ��������������������������������������������������������������������������������� 159
Chapter 11: Posting JSON ■ ���������������������������������������������������������������������������������� 191
Chapter 12: Persisting JSON: II ■ ������������������������������������������������������������������������� 213
Chapter 13: Working with Templates ■ ���������������������������������������������������������������� 243
Chapter 14: Putting It All Together ■ �������������������������������������������������������������������� 269
Index ��������������������������������������������������������������������������������������������������������������������� 295
vii
Contents
About the Author �����������������������������������������������������������������������������������������������������xv
About the Technical Reviewer �������������������������������������������������������������������������������xvii
Acknowledgments ��������������������������������������������������������������������������������������������������xix
Introduction ������������������������������������������������������������������������������������������������������������xxi
Chapter 1: JavaScript Basics ■ �������������������������������������������������������������������������������� 1
JavaScript History ������������������������������������������������������������������������������������������������������������ 1
JavaScript Essentials ������������������������������������������������������������������������������������������������������� 2
Values ������������������������������������������������������������������������������������������������������������������������������� 2
Primitive Types ��������������������������������������������������������������������������������������������������������������������������������������� 2
Expressions ���������������������������������������������������������������������������������������������������������������������� 6
Operators ������������������������������������������������������������������������������������������������������������������������������������������������ 6
Statements ��������������������������������������������������������������������������������������������������������������������� 10
Line Terminators ����������������������������������������������������������������������������������������������������������������������������������� 10
Control Statements ������������������������������������������������������������������������������������������������������������������������������� 11
Block Statements ��������������������������������������������������������������������������������������������������������������������������������� 12
Truthy/Falsy ������������������������������������������������������������������������������������������������������������������������������������������ 12
Loop Statements����������������������������������������������������������������������������������������������������������������������������������� 13
Declarations ������������������������������������������������������������������������������������������������������������������� 14
Variables ����������������������������������������������������������������������������������������������������������������������������������������������� 14
Functions ������������������������������������������������������������������������������������������������������������������������ 14
Summary ������������������������������������������������������������������������������������������������������������������������ 15
Key Points from This Chapter ��������������������������������������������������������������������������������������������������������������� 15
■ Contents
viii
Chapter 2: Special Objects ■ ���������������������������������������������������������������������������������� 17
Objects ��������������������������������������������������������������������������������������������������������������������������� 17
Objects Are Collections ��������������������������������������������������������������������������������������������������� 17
Built-in Objects ��������������������������������������������������������������������������������������������������������������� 18
Object ����������������������������������������������������������������������������������������������������������������������������� 18
Creating Objects ������������������������������������������������������������������������������������������������������������� 19
Access Notation ������������������������������������������������������������������������������������������������������������� 19
Array ������������������������������������������������������������������������������������������������������������������������������� 21
Object Literals ���������������������������������������������������������������������������������������������������������������� 22
Designing Literals ����������������������������������������������������������������������������������������������������������� 24
The Object Literal ��������������������������������������������������������������������������������������������������������������������������������� 24
The Array Literal ����������������������������������������������������������������������������������������������������������������������������������� 25
Summary ������������������������������������������������������������������������������������������������������������������������ 26
Key Points from This Chapter ��������������������������������������������������������������������������������������������������������������� 26
Chapter 3: String Manipulation ■ ��������������������������������������������������������������������������� 27
String Concatenation ������������������������������������������������������������������������������������������������������ 27
The String object ������������������������������������������������������������������������������������������������������������ 28
Creating String objects ��������������������������������������������������������������������������������������������������� 28
The Interface of the String object ����������������������������������������������������������������������������������� 28
length���������������������������������������������������������������������������������������������������������������������������������������������������� 29
toString ������������������������������������������������������������������������������������������������������������������������������������������������� 30
charAt ��������������������������������������������������������������������������������������������������������������������������������������������������� 30
indexOf ������������������������������������������������������������������������������������������������������������������������������������������������� 30
lastIndexOf �������������������������������������������������������������������������������������������������������������������������������������������� 32
match���������������������������������������������������������������������������������������������������������������������������������������������������� 32
replace �������������������������������������������������������������������������������������������������������������������������������������������������� 32
slice ������������������������������������������������������������������������������������������������������������������������������������������������������ 33
substr���������������������������������������������������������������������������������������������������������������������������������������������������� 34
■ Contents
ix
split ������������������������������������������������������������������������������������������������������������������������������������������������������� 34
toUpperCase ����������������������������������������������������������������������������������������������������������������������������������������� 35
toLowerCase ����������������������������������������������������������������������������������������������������������������������������������������� 35
The Implicit String object ����������������������������������������������������������������������������������������������� 35
Summary ������������������������������������������������������������������������������������������������������������������������ 36
Key Points from This Chapter �������������������������������������������������������������������������������������������������������������� 36
Chapter 4: Introducing JSON ■ ������������������������������������������������������������������������������� 37
History ���������������������������������������������������������������������������������������������������������������������������� 37
JSON Grammar ��������������������������������������������������������������������������������������������������������������� 38
Composite Structures ��������������������������������������������������������������������������������������������������������������������������� 38
JSON Values ����������������������������������������������������������������������������������������������������������������������������������������� 41
JSON Tokens������������������������������������������������������������������������������������������������������������������� 45
Summary ������������������������������������������������������������������������������������������������������������������������ 46
Key Points from This Chapter ��������������������������������������������������������������������������������������������������������������� 47
Chapter 5: Creating JSON ■ ������������������������������������������������������������������������������������ 49
The Serialization Process—Demystified ������������������������������������������������������������������������ 50
The JSON Object ������������������������������������������������������������������������������������������������������������ 52
stringify������������������������������������������������������������������������������������������������������������������������������������������������� 53
Summary ������������������������������������������������������������������������������������������������������������������������ 65
Key Points from This Chapter ��������������������������������������������������������������������������������������������������������������� 66
Chapter 6: Parsing JSON ■ ������������������������������������������������������������������������������������� 69
JSON�parse ��������������������������������������������������������������������������������������������������������������������� 69
eval ������������������������������������������������������������������������������������������������������������������������������������������������������� 71
reviver ��������������������������������������������������������������������������������������������������������������������������������������������������� 73
Summary ������������������������������������������������������������������������������������������������������������������������ 79
Key Points from This Chapter ��������������������������������������������������������������������������������������������������������������� 79
■ Contents
x
Chapter 7: Persisting JSON: I ■ ������������������������������������������������������������������������������ 81
HTTP Cookie ������������������������������������������������������������������������������������������������������������������� 82
Syntax ��������������������������������������������������������������������������������������������������������������������������������������������������� 82
document�cookie ������������������������������������������������������������������������������������������������������������ 87
Web Storage ������������������������������������������������������������������������������������������������������������������� 94
Web Storage Interface �������������������������������������������������������������������������������������������������������������������������� 95
Summary ������������������������������������������������������������������������������������������������������������������������ 99
Key Points from This Chapter ��������������������������������������������������������������������������������������������������������������� 99
Chapter 8: Data Interchange ■ ������������������������������������������������������������������������������ 101
Hypertext Transfer Protocol ������������������������������������������������������������������������������������������ 101
HTTP-Request ������������������������������������������������������������������������������������������������������������������������������������� 102
HTTP Response ����������������������������������������������������������������������������������������������������������������������������������� 106
Ajax ������������������������������������������������������������������������������������������������������������������������������ 110
XMLHttpRequest Interface ������������������������������������������������������������������������������������������� 112
Global Aspects ������������������������������������������������������������������������������������������������������������������������������������ 112
The Request Aspect ���������������������������������������������������������������������������������������������������������������������������� 116
The Response Aspect ������������������������������������������������������������������������������������������������������������������������� 121
Obtaining the Response ���������������������������������������������������������������������������������������������������������������������� 124
Summary ���������������������������������������������������������������������������������������������������������������������� 130
Key Points from This Chapter ������������������������������������������������������������������������������������������������������������� 131
Chapter 9: X-Origin Resources ■ �������������������������������������������������������������������������� 133
Same-Origin Policy ������������������������������������������������������������������������������������������������������� 133
Circumventing Same-Origin Policy ������������������������������������������������������������������������������� 142
CORS ��������������������������������������������������������������������������������������������������������������������������������������������������� 143
The Proxy �������������������������������������������������������������������������������������������������������������������������������������������� 148
JSONP ������������������������������������������������������������������������������������������������������������������������������������������������� 152
Summary ���������������������������������������������������������������������������������������������������������������������� 157
Key Points from This Chapter ������������������������������������������������������������������������������������������������������������� 157
■ Contents
xi
Chapter 10: Serving JSON ■ ��������������������������������������������������������������������������������� 159
Node�JS ������������������������������������������������������������������������������������������������������������������������ 159
Windows Installation �������������������������������������������������������������������������������������������������������������������������� 159
Mac Installation ���������������������������������������������������������������������������������������������������������������������������������� 165
Building an HTTP Server����������������������������������������������������������������������������������������������� 168
Node HTTP Web Server ����������������������������������������������������������������������������������������������������������������������� 168
Node API ��������������������������������������������������������������������������������������������������������������������������������������������� 170
Modules ���������������������������������������������������������������������������������������������������������������������������������������������� 170
The HTTP Module ��������������������������������������������������������������������������������������������������������� 171
http�createServer �������������������������������������������������������������������������������������������������������������������������������� 172
http�IncomingMessage ����������������������������������������������������������������������������������������������������������������������� 172
http�ServerResponse �������������������������������������������������������������������������������������������������������������������������� 172
http�Server ������������������������������������������������������������������������������������������������������������������������������������������ 173
CORS-Enabled Server ��������������������������������������������������������������������������������������������������� 176
JSONP Server ��������������������������������������������������������������������������������������������������������������� 178
The Proxy Server ���������������������������������������������������������������������������������������������������������� 181
http�request ���������������������������������������������������������������������������������������������������������������������������������������� 182
http�Stream ����������������������������������������������������������������������������������������������������������������������������������������� 183
http�ClientRequest ����������������������������������������������������������������������������������������������������������������������������� 184
Summary ���������������������������������������������������������������������������������������������������������������������� 188
Key Points from This Chapter ������������������������������������������������������������������������������������������������������������� 188
Chapter 11: Posting JSON ■ ���������������������������������������������������������������������������������� 191
Request Entity Body ����������������������������������������������������������������������������������������������������� 191
HTML Form POST �������������������������������������������������������������������������������������������������������������������������������� 192
Processing a JSON POST �������������������������������������������������������������������������������������������������������������������� 197
Preflight Request ���������������������������������������������������������������������������������������������������������� 203
OPTIONS Request Method ������������������������������������������������������������������������������������������������������������������ 204
What Determines Preflight ����������������������������������������������������������������������������������������������������������������� 210
Summary ���������������������������������������������������������������������������������������������������������������������� 211
Key Points from This Chapter ������������������������������������������������������������������������������������������������������������� 211
■ Contents
xii
Chapter 12: Persisting JSON: II ■ ������������������������������������������������������������������������� 213
CouchDB ����������������������������������������������������������������������������������������������������������������������� 213
Windows Installation �������������������������������������������������������������������������������������������������������������������������� 213
Mac Installation ���������������������������������������������������������������������������������������������������������������������������������� 214
Working with CouchDB ������������������������������������������������������������������������������������������������� 215
Futon��������������������������������������������������������������������������������������������������������������������������������������������������� 215
Creating Views ������������������������������������������������������������������������������������������������������������������������������������ 220
Creating Our First Custom View ���������������������������������������������������������������������������������������������������������� 221
Connecting Node and CouchDB ������������������������������������������������������������������������������������ 224
Cradle for Node ����������������������������������������������������������������������������������������������������������������������������������� 225
Working with Databases ��������������������������������������������������������������������������������������������������������������������� 226
Cradle Database API ��������������������������������������������������������������������������������������������������������������������������� 227
Creating Documents via Cradle ���������������������������������������������������������������������������������������������������������� 235
Creating Design Documents via Cradle ���������������������������������������������������������������������������������������������� 236
Summary ���������������������������������������������������������������������������������������������������������������������� 241
Key Points from This Chapter ������������������������������������������������������������������������������������������������������������� 241
Chapter 13: Working with Templates ■ ���������������������������������������������������������������� 243
Templating Engine �������������������������������������������������������������������������������������������������������� 243
Handlebars ������������������������������������������������������������������������������������������������������������������� 245
Installation ������������������������������������������������������������������������������������������������������������������������������������������ 245
Working with Handlebars ��������������������������������������������������������������������������������������������� 246
A Basic Expression ����������������������������������������������������������������������������������������������������������������������������� 247
The Role of <script> ��������������������������������������������������������������������������������������������������������������������������� 249
Compiling a Template ������������������������������������������������������������������������������������������������������������������������� 249
Giving Context to Our Template ���������������������������������������������������������������������������������������������������������� 250
Multiple Placeholders ������������������������������������������������������������������������������������������������������������������������� 251
Complex JSON Structures ������������������������������������������������������������������������������������������������������������������ 253
Block Expressions������������������������������������������������������������������������������������������������������������������������������� 255
Block Expressions and Arrays ������������������������������������������������������������������������������������������������������������ 256
■ Contents
xiii
Applying Logic to Logic-less Templates ����������������������������������������������������������������������� 259
Helpers ����������������������������������������������������������������������������������������������������������������������������������������������� 260
Summary ��������������������������������������������������������������������������������������������������������������������� 266
Key Points from This Chapter ������������������������������������������������������������������������������������������������������������� 266
Chapter 14: Putting It All Together ■ �������������������������������������������������������������������� 269
Twitter �������������������������������������������������������������������������������������������������������������������������� 269
Twitter Apps ���������������������������������������������������������������������������������������������������������������������������������������� 269
Keys and Access Tokens ��������������������������������������������������������������������������������������������������������������������� 272
Public Stream ������������������������������������������������������������������������������������������������������������������������������������� 274
Your User Access Token ���������������������������������������������������������������������������������������������������������������������� 274
#Trending �������������������������������������������������������������������������������������������������������������������������������������������� 276
Node�js ������������������������������������������������������������������������������������������������������������������������� 276
Twitter Module ������������������������������������������������������������������������������������������������������������������������������������ 276
Incorporating the Twitter Module �������������������������������������������������������������������������������������������������������� 277
Streaming API (Stable) ������������������������������������������������������������������������������������������������������������������������ 277
Statuses/Filter ������������������������������������������������������������������������������������������������������������������������������������ 277
CouchDB ����������������������������������������������������������������������������������������������������������������������� 278
Incorporating the Cradle Module �������������������������������������������������������������������������������������������������������� 278
Creating a View ����������������������������������������������������������������������������������������������������������������������������������� 280
Enabling CORS ������������������������������������������������������������������������������������������������������������������������������������ 282
Ajax ������������������������������������������������������������������������������������������������������������������������������ 284
Requesting Ranges ����������������������������������������������������������������������������������������������������������������������������� 284
Handlebars ������������������������������������������������������������������������������������������������������������������� 287
Summary ���������������������������������������������������������������������������������������������������������������������� 294
Index ��������������������������������������������������������������������������������������������������������������������� 295
xv
About the Author
Ben Smith is an accomplished technical experience director with many years of experience leading web development for well-known digital agencies. His list of contributions to the community has earned him a place as an Adobe Community Professional. It should be apparent from his background as an author, speaker, and a judge for the Favourite Website Awards (FWA) that he is passionate about the Web. He attributes his growth to experimentation and experience.
xvii
About the Technical Reviewer
Victor Sumner is a senior software engineer at D2L Corporation, where he helps to build and maintain an integrated learning platform. As a self-taught developer, he is always interested in emerging technologies and enjoys working on and solving problems that are outside his comfort zone.
When not at the office, Victor has a number of hobbies, including photography, horseback riding, and gaming. He lives in Ontario, Canada, with his wife, Alicia, and their two children.
xix
Acknowledgments
This book could not have been written without a loving and patient wife, an understanding circle of friends, and a great team of editors and reviewers. My sincerest thanks to them all.
xxi
Introduction
Programming is not at all a linear path. Often, you find yourself facing a fork in the road. In choosing one path, you are likely to find, after a period of time, that you go back and travel down the other. While it is assumed that you are familiar with HTML, CSS, and JavaScript, this book makes no further assumptions regarding your experience. Therefore, it attempts to provide a thorough explanation for everything you will read in it.
While JSON is the essence of this book, it is not the sole topic discussed. While that may sound counterproductive, it is a much-needed requirement. JSON can be devised in isolation, but it would serve little purpose. What makes JSON so impactful is that it interacts with the many tools of the developer. For this reason, this book covers a wide range of implementations—from libraries to software.