www.it-ebooks.info
Beginning JSON Ben Smith
www.it-ebooks.info
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 Spahr Lead Editor: Ben Renow-Clarke Technical Reviewer: Victor Sumner Editorial 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 Ricketts Copy Editor: Michael G. Laraque Compositor: SPi Global Indexer: SPi Global Artist: 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/.
www.it-ebooks.info
To my wife, for her patience with the late evenings and stay-at-home weekends, as well as for her constant encouragement
www.it-ebooks.info
Contents at a Glance About the Author About the Technical Reviewer Acknowledgments Introduction Chapter 1: JavaScript Basics Chapter 2: Special Objects Chapter 3: String Manipulation Chapter 4: Introducing JSON Chapter 5: Creating JSON Chapter 6: Parsing JSON Chapter 7: Persisting JSON: I Chapter 8: Data Interchange Chapter 9: X-Origin Resources Chapter 10: Serving JSON Chapter 11: Posting JSON Chapter 12: Persisting JSON: II Chapter 13: Working with Templates Chapter 14: Putting It All Together Index
www.it-ebooks.info
Contents About the Author About the Technical Reviewer Acknowledgments Introduction Chapter 1: JavaScript Basics JavaScript History JavaScript Essentials Values Primitive Types
Expressions Operators
Statements Line Terminators Control Statements Block Statements Truthy/Falsy Loop Statements
Declarations Variables
Functions Summary Key Points from This Chapter
Chapter 2: Special Objects Objects Objects Are Collections Built-in Objects Object Creating Objects Access Notation www.it-ebooks.info
Array Object Literals Designing Literals The Object Literal The Array Literal
Summary Key Points from This Chapter
Chapter 3: String Manipulation String Concatenation The String object Creating String objects The Interface of the String object length toString charAt indexOf lastIndexOf match replace slice substr split toUpperCase toLowerCase
The Implicit String object Summary Key Points from This Chapter
Chapter 4: Introducing JSON History JSON Grammar Composite Structures JSON Values
JSON Tokens www.it-ebooks.info
Summary Key Points from This Chapter
Chapter 5: Creating JSON The Serialization Process—Demystified The JSON Object stringify
Summary Key Points from This Chapter
Chapter 6: Parsing JSON JSON.parse eval reviver
Summary Key Points from This Chapter
Chapter 7: Persisting JSON: I HTTP Cookie Syntax
document.cookie Web Storage Web Storage Interface
Summary Key Points from This Chapter
Chapter 8: Data Interchange Hypertext Transfer Protocol HTTP-Request HTTP Response
Ajax XMLHttpRequest Interface Global Aspects The Request Aspect The Response Aspect Obtaining the Response
www.it-ebooks.info
Summary Key Points from This Chapter
Chapter 9: X-Origin Resources Same-Origin Policy Circumventing Same-Origin Policy CORS The Proxy JSONP
Summary Key Points from This Chapter
Chapter 10: Serving JSON Node.JS Windows Installation Mac Installation
Building an HTTP Server Node HTTP Web Server Node API Modules
The HTTP Module http.createServer http.IncomingMessage http.ServerResponse http.Server
CORS-Enabled Server JSONP Server The Proxy Server http.request http.Stream http.ClientRequest
Summary Key Points from This Chapter
Chapter 11: Posting JSON Request Entity Body www.it-ebooks.info
HTML Form POST Processing a JSON POST
Preflight Request OPTIONS Request Method What Determines Preflight
Summary Key Points from This Chapter
Chapter 12: Persisting JSON: II CouchDB Windows Installation Mac Installation
Working with CouchDB Futon Creating Views Creating Our First Custom View
Connecting Node and CouchDB Cradle for Node Working with Databases Cradle Database API Creating Documents via Cradle Creating Design Documents via Cradle
Summary Key Points from This Chapter
Chapter 13: Working with Templates Templating Engine Handlebars Installation
Working with Handlebars A Basic Expression The Role of