json at work overview and ecosystem-v2.0

67
JSON at Work - Overview and Ecosystem Tom Marrs Architect

Upload: boulder-java-users-group

Post on 15-Jan-2015

794 views

Category:

Documents


1 download

DESCRIPTION

Slides for Tom Marrs BJUG talk on 2/12/2013. See http://boulderjug.org/2013/01/tuesday-february-12-2013-a-night-with-tom-marrs-covering-json-and-rest.html

TRANSCRIPT

Page 1: Json at work   overview and ecosystem-v2.0

JSON at Work -Overview and Ecosystem

Tom MarrsArchitect

Page 2: Json at work   overview and ecosystem-v2.0

Your ProfileReally?

What else?

How?

Page 3: Json at work   overview and ecosystem-v2.0

What’s The Point?

JSON -

much

more ...

Page 4: Json at work   overview and ecosystem-v2.0

JSON Ecosystem

Schema Search

APIs

Transform REST

Dev Tools

JSON Core

Page 5: Json at work   overview and ecosystem-v2.0

Our Agenda

Page 6: Json at work   overview and ecosystem-v2.0

We’re Not Covering :(-

REST

XML

SOA

Page 7: Json at work   overview and ecosystem-v2.0

JSON Beginnings

www.json.org

2001 - Douglas Crockford

2006 - IETF - RFC 4627

The “x” in AJAX

Lightweight / simple structures

Page 8: Json at work   overview and ecosystem-v2.0

That was Then ...JSON for Speed XML for Integration

Page 9: Json at work   overview and ecosystem-v2.0

JSON Validation

Structure Instance Document [Well-formed]

Semantics Schema [Order, Person]

Page 10: Json at work   overview and ecosystem-v2.0

This is NowJSON for Speed JSON for Integration

Page 11: Json at work   overview and ecosystem-v2.0

JSON.org

Page 12: Json at work   overview and ecosystem-v2.0

JSON Tutorial - on the iPhone

Page 13: Json at work   overview and ecosystem-v2.0

JSON Documents

{ JSON-Data}

Page 14: Json at work   overview and ecosystem-v2.0

JSON Data Structures

Name/Value (or Key/Value) Pairs

Objects

Arrays

Page 15: Json at work   overview and ecosystem-v2.0

JSON Key/Value Pair

{ "firstName": "John"}

Page 16: Json at work   overview and ecosystem-v2.0

JSON Object{

"address" : { "line1" : "555 Main Street", "city" : "Denver", "stateOrProvince" : "CO", "zipOrPostalCode" : "80202", "country" : "USA" }}

Page 17: Json at work   overview and ecosystem-v2.0

JSON Array"clubs" : [ { "number" : 677476, "name" : "Simply Speaking", "city" : "Aurora", "district" : 26 }, { "number" : 999999, "name" : "Wry Toast", "city" : "Denver", "district" : 26 }]

Page 18: Json at work   overview and ecosystem-v2.0

JSON Value Types

Page 19: Json at work   overview and ecosystem-v2.0

Numbers

"age": 29"cost": 299.99"temperature": -10.5"speed_of_light": 1.23e11"speed_of_light": 1.23e+11"speed_of_light": 1.23E11"speed_of_light": 1.23E+11

Page 20: Json at work   overview and ecosystem-v2.0

Booleans

"emailValidated" : true

Page 21: Json at work   overview and ecosystem-v2.0

null

{ "address": { "line1": "555 Main St.", "line2": null, "city": "Bailey", "state": "CO", "postalCode": 80909, "country": "USA" }}

Page 22: Json at work   overview and ecosystem-v2.0

JSON Comments

No

Page 23: Json at work   overview and ecosystem-v2.0

JSON Versions

Never

Page 24: Json at work   overview and ecosystem-v2.0

A Bigger Sample{ "member": { "firstName": "John", "lastName": "Smith", "joined": "2008-12-06", "language": "US English", "reason": "To improve my public speaking skills. To take over the world.", "address": { "line1": "555 Main St.", "city": "Bailey", "state": "CO", "postalCode": 80909, "country": "USA" }, "contact": { "email": "[email protected]", "homePhone": "303-555-1212", "cellPhone": "720-555-1212", "workPhone": "970-555-1212" }, "club": { "number": 677476, "name": "Simply Speaking", "city": "Aurora", "district": 26 } }}

Page 25: Json at work   overview and ecosystem-v2.0

Google JSON Style Guide

Page 26: Json at work   overview and ecosystem-v2.0

JSON is Hot!

Page 27: Json at work   overview and ecosystem-v2.0

JSON Tool Runtimes

GUIs iPhone Apps

IDE Plugins

Browser Plugins

Online

Command Line

JSON Tools

Page 28: Json at work   overview and ecosystem-v2.0

JSON Validators

JSON Validator (Mac)

http://www.jsonlint.com

Page 29: Json at work   overview and ecosystem-v2.0

JSONLint.com

Page 30: Json at work   overview and ecosystem-v2.0

JSON BeautifiersFirefox

JSONView

Chrome

JSONView

JSON SH

Page 31: Json at work   overview and ecosystem-v2.0

JSON Without a Beautifier

Page 32: Json at work   overview and ecosystem-v2.0

JSONView

Page 33: Json at work   overview and ecosystem-v2.0

JSON SH

Page 34: Json at work   overview and ecosystem-v2.0

JSON Modeling ToolsJSON Designer

JSONPad

http://www.jsoneditoronline.org

http://www.jsonschema.net

Overly - Generate Schema

Matic - Generate HTML

Page 35: Json at work   overview and ecosystem-v2.0

JSON Designer - on the iPhone

Page 36: Json at work   overview and ecosystem-v2.0

Model JSON Doc -JSONPad

Page 37: Json at work   overview and ecosystem-v2.0

Model JSON Doc -jsoneditoronline.org

Page 38: Json at work   overview and ecosystem-v2.0

JSON and Java - Jackson

public class Address { private String line1; private String city; private String stateOrProvince; private String zipOrPostalCode; private String country;

public Address() {} public String getLine1() { return line1; } public void setLine1(line1) { this.line1 = line1; } // Remaining getters and setters ...}

Address addrOut = new Address();// Call setters to populate addrOut ...

Page 39: Json at work   overview and ecosystem-v2.0

JSON and Java - Jackson ... Cont’d

import java.io.Writer’import java.io.StringWriter;import org.codehaus.jackson.map.ObjectMapper;

ObjectMapper mapper = new ObjectMapper(); // Reuse this.

// Marshal Address object to JSON String.Writer writer = new StringWriter();mapper.writeValue(writer, addrOut);System.out.println(writer.toString());

// Unmarshal Address object from JSON String.String addrJsonStr = "{" + "\"address\" : {" + "\"line1\" : \"555 Main Street\"," + "\"city\" : \"Denver\"," "\"stateOrProvince\" : \"CO\"," "\"zipOrPostalCode\" : \"80202\"," + "\"country\" : \"USA\"" + "}" +"}";

Address addrIn = mapper.readValue(addrJsonStr, Address.class);

Page 40: Json at work   overview and ecosystem-v2.0

Java-based JSON APIs

API Source

Jackson http://jackson.codehaus.org/

Google GSON http://code.google.com/p/google-json/

SOJO http://sojo.sourceforge.net/

org.json (Douglas Crockford)

http://www.json.org/java

json-lib http://sourceforge.net/projects/json-lib/

json-io http://code.google.com/p/json-io

jsontools http://jsontools.berlios.de/

jsonbeans http://code.google.com/p/jsonbeans/

Page 41: Json at work   overview and ecosystem-v2.0

JSON and HTML5 & JavaScript - AJAX

$.getJSON('http://example/service/addresses/home/1', function(data) { var address = JSON.parse(data); console.log(“Address Line 1 = “ + address.line1); });

Page 42: Json at work   overview and ecosystem-v2.0

JSON and Ruby on Rails

class Person attr_accessor :first_name, :last_name

def initialize(first_name=nil, last_name=nil) @first_name = first_name @last_name = last_name endend

class MyController < ApplicationController def index person = Person.new('John', 'Doe') respond_to do |format| format.html # index.html.erb format.json { render :json => person} end endend

Page 43: Json at work   overview and ecosystem-v2.0

JSON and Ruby

require 'json'

class Address

attr_accessor :line1, :city, :state_or_province, :zip_or_postal_code, :country def initialize(line1=nil, city=nil, state_or_province=nil, zip_or_postal_code=nil, country=nil) @line1 = line1 @city = city @state_or_province = state_or_province @zip_or_postal_code = zip_or_postal_code @country = country end

Page 44: Json at work   overview and ecosystem-v2.0

JSON and Ruby

def to_hash hash = {} self.instance_variables.each do |var| hash[var.to_s.delete("@")] = self.instance_variable_get(var) end hash end def to_json to_hash.to_json end

def from_json!(str) JSON.load(str).each do |var, val| self.instance_variable_set("@#{var}", val) end endend

Page 45: Json at work   overview and ecosystem-v2.0

JSON and Rubyaddr1 = Address.new('555 Main Street', 'Denver', 'CO', '80231', 'US')

puts addr1.to_json # Marshal Address object to JSON string.

# Outputs the following …( "line1": "555 Main Street", "city": "Denver", "state_or_province": "CO", "zip_or_postal_code": "80231","country":"US" }

json_addr = <<END{ "line1" : "999 Broadway", "city" : "Anytown", "state_or_province" : "CA", "zip_or_postal_code" : "90210", "country" : "USA"}END

addr2 = Address.newaddr2.from_json!(json_addr) # Unmarshal Address object from JSON string.

Page 46: Json at work   overview and ecosystem-v2.0

Ruby-based JSON APIs

API Source

Yajl https://github.com/brianmario/yajl-ruby

Oj https://github.com/ohler55/oj

Page 47: Json at work   overview and ecosystem-v2.0

JSON Schema

Defines JSON document structure

http://json-schema.org/

Page 48: Json at work   overview and ecosystem-v2.0

When To Use JSON Schema?

When crossing organizational boundaries

Page 49: Json at work   overview and ecosystem-v2.0

When NOT to use JSON Schema?

Get a life - it’s just a website!

Page 50: Json at work   overview and ecosystem-v2.0

JSON Schema Constructs

Construct Description

type The data type – object, array, string, number, etc.

required true / false

id Data element id

properties Additional validation properties for a data element (e.g., minimum, maximum, etc.)

Page 51: Json at work   overview and ecosystem-v2.0

JSON Schema Validators

JSON Schema Validator Language Source

JSV JavaScript https://github.com/garycourt/JSV

Ruby JSON Schema Validator

Ruby https://github.com/hoxworth/json-schema

json-schema-validator

Java https://github.com/fge/json-schema-validator

php-json-schema (by MIT)

PHP https://github.com/hasbridge/php-json-schema

JSON.Net .NET http://james.newtonking.com/projects/json-net.aspx

Page 52: Json at work   overview and ecosystem-v2.0

JSON Schema Example

Gift Registry Document & Schema

Page 53: Json at work   overview and ecosystem-v2.0

JSON Modeling Flow

ModelJSON

Document

CreateJSON

Schema

GenerateHTML

Document

Page 54: Json at work   overview and ecosystem-v2.0

Create JSON Schema - jsonschema.net

Page 55: Json at work   overview and ecosystem-v2.0

Create JSON Schema - Orderly

Page 56: Json at work   overview and ecosystem-v2.0

Validate JSON Document against JSON Schema

Page 57: Json at work   overview and ecosystem-v2.0

Generate HTML Document from JSON Schema - Matic

Page 58: Json at work   overview and ecosystem-v2.0

RESTing with JSON

Page 59: Json at work   overview and ecosystem-v2.0

Text Search with JSON

JSONQuery

JSONPath

JSONiq

Page 60: Json at work   overview and ecosystem-v2.0

Transforming JSON

JSONT

Page 61: Json at work   overview and ecosystem-v2.0

Our Agenda

Page 62: Json at work   overview and ecosystem-v2.0

What’s The Point?

JSON -

much

more ...

Page 64: Json at work   overview and ecosystem-v2.0

JSON ResourcesJSON Spec - http://tools.ietf.org/html/rfc4627

JSON.org - http://www.json.org

JSONLint - http://www.jsonlint.com

JSON Editor Online - http://jsoneditoronline.org/

Page 66: Json at work   overview and ecosystem-v2.0

JSON ResourcesJSONQuery - https://github.com/jcrosby/jsonquery

JSONPath - http://goessner.net/articles/JsonPath/

JSONiq - http://www.jsoniq.org/

JSONT - http://goessner.net/articles/jsont/

Page 67: Json at work   overview and ecosystem-v2.0

JSON Groups

Google - http://groups.google.com/group/json-schema

Yahoo! - http://tech.groups.yahoo.com/group/json/