JSON at Work

[Pages:367]JSON at Work

PRACTICAL DATA INTEGRATION FOR THE WEB

Tom Marrs

JSON at Work

Practical Data Integration for the Web

Tom Marrs

Beijing Boston Farnham Sebastopol Tokyo

JSON at Work

by Tom Marrs

Copyright ? 2017 Vertical Slice, Inc. All rights reserved.

Printed in the United States of America.

Published by O'Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.

O'Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (). For more information, contact our corporate/insti- tutional sales department: 800-998-9938 or corporate@.

Editor: Meg Foley Production Editor: Nicholas Adams Copyeditor: Sharon Wilkey Proofreader: Charles Roumeliotis

Indexer: Ellen Troutman-Zaig Interior Designer: David Futato Cover Designer: Randy Comer Illustrator: Rebecca Demarest

July 2017:

First Edition

Revision History for the First Edition 2017-06-16: First Release

See for release details.

The O'Reilly logo is a registered trademark of O'Reilly Media, Inc. JSON at Work, the cover image, and related trade dress are trademarks of O'Reilly Media, Inc. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.

978-1-449-35832-7 [LSI]

To everyone who produces or consumes JSON data with web/mobile applications, REST APIs, and messaging systems--I hope this makes your job easier.

To the unsung JSON community that produces JSON-based tools and libraries for the rest of us--thank you for all your hard work to make JSON useful and meaningful.

Table of Contents

Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

Part I. JSON Overview and Platforms

1. JSON Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

JSON Is a Standard

3

A Brief Sample

4

Why JSON?

6

Core JSON

8

JSON Data Types

8

JSON Value Types

11

JSON Versions

14

JSON Comments

14

JSON File and MIME Type

14

JSON Style Guidelines

15

Our Example--MyConference

17

Our Technical Stack

17

Our Architectural Style--noBackEnd

17

Model JSON Data with JSON Editor Online

18

Generate Sample JSON Data with JSON Generator

20

Create and Deploy a Stub API

20

What We Covered?

24

What's Next?

24

2. JSON in JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

Node.js Setup

25

JSON Serialization/Deserialization with JSON.stringify() and JSON.parse() 26

v

The JSON Stringifier/Parser Object

26

JSON Serialization with Simple JavaScript Data Types

26

JSON Serialization with an Object and toJSON()

29

JSON Deserialization Using eval()

30

JSON Deserialization with an Object and JSON.parse()

31

JavaScript Objects and JSON

32

Node REPL

33

Where to Learn More About JavaScript Objects

35

Unit Testing with a Stub API

35

Unit Test Style--TDD and BDD

35

Just Enough Unit Testing with Mocha and Chai

36

Setting Up the Unit Test

36

Unirest

36

Test Data

37

Speakers Unit Test

37

Building a Small Web Application

39

Yeoman

39

Iteration 1--Generate a Web Application with Yeoman

41

Iteration 2--Make an HTTP Call with jQuery

45

Iteration 3--Consume Speaker Data from a Stub API and Use a Template 49

How to Go Deeper with JavaScript

54

What We Covered

55

What's Next?

55

3. JSON in Ruby on Rails. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

Ruby on Rails Setup

57

Ruby JSON Gems

58

JSON Serialization/Deserialization with MultiJson

58

The MultiJson Object

58

JSON Serialization/Deserialization with Simple Ruby Data Types

59

JSON Deserialization with Objects and MultiJson

61

A Word on Camel Casing and JSON

63

JSON Serialization with Objects and ActiveSupport

64

JSON Deserialization with Objects and ActiveSupport

65

Unit Testing with a Stub API

66

Just Enough Unit Testing with Minitest

66

Setting Up the Unit Test

67

Test Data

68

JSON and Minitest Testing with APIs

68

Speakers Unit Test

68

Further Reading on Ruby and Minitest

72

What Is Missing in the Unit Tests?

72

vi | Table of Contents

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download