Upload
boulder-java-users-group
View
792
Download
1
Tags:
Embed Size (px)
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
Citation preview
JSON at Work -Overview and Ecosystem
Tom MarrsArchitect
Your ProfileReally?
What else?
How?
What’s The Point?
JSON -
much
more ...
JSON Ecosystem
Schema Search
APIs
Transform REST
Dev Tools
JSON Core
Our Agenda
We’re Not Covering :(-
REST
XML
SOA
JSON Beginnings
www.json.org
2001 - Douglas Crockford
2006 - IETF - RFC 4627
The “x” in AJAX
Lightweight / simple structures
That was Then ...JSON for Speed XML for Integration
JSON Validation
Structure Instance Document [Well-formed]
Semantics Schema [Order, Person]
This is NowJSON for Speed JSON for Integration
JSON.org
JSON Tutorial - on the iPhone
JSON Documents
{ JSON-Data}
JSON Data Structures
Name/Value (or Key/Value) Pairs
Objects
Arrays
JSON Key/Value Pair
{ "firstName": "John"}
JSON Object{
"address" : { "line1" : "555 Main Street", "city" : "Denver", "stateOrProvince" : "CO", "zipOrPostalCode" : "80202", "country" : "USA" }}
JSON Array"clubs" : [ { "number" : 677476, "name" : "Simply Speaking", "city" : "Aurora", "district" : 26 }, { "number" : 999999, "name" : "Wry Toast", "city" : "Denver", "district" : 26 }]
JSON Value Types
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
Booleans
"emailValidated" : true
null
{ "address": { "line1": "555 Main St.", "line2": null, "city": "Bailey", "state": "CO", "postalCode": 80909, "country": "USA" }}
JSON Comments
No
JSON Versions
Never
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 } }}
Google JSON Style Guide
JSON is Hot!
JSON Tool Runtimes
GUIs iPhone Apps
IDE Plugins
Browser Plugins
Online
Command Line
JSON Tools
JSON Validators
JSON Validator (Mac)
http://www.jsonlint.com
JSONLint.com
JSON BeautifiersFirefox
JSONView
Chrome
JSONView
JSON SH
JSON Without a Beautifier
JSONView
JSON SH
JSON Modeling ToolsJSON Designer
JSONPad
http://www.jsoneditoronline.org
http://www.jsonschema.net
Overly - Generate Schema
Matic - Generate HTML
JSON Designer - on the iPhone
Model JSON Doc -JSONPad
Model JSON Doc -jsoneditoronline.org
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 ...
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);
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/
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); });
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
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
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
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.
Ruby-based JSON APIs
API Source
Yajl https://github.com/brianmario/yajl-ruby
Oj https://github.com/ohler55/oj
JSON Schema
Defines JSON document structure
http://json-schema.org/
When To Use JSON Schema?
When crossing organizational boundaries
When NOT to use JSON Schema?
Get a life - it’s just a website!
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.)
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
JSON Schema Example
Gift Registry Document & Schema
JSON Modeling Flow
ModelJSON
Document
CreateJSON
Schema
GenerateHTML
Document
Create JSON Schema - jsonschema.net
Create JSON Schema - Orderly
Validate JSON Document against JSON Schema
Generate HTML Document from JSON Schema - Matic
RESTing with JSON
Text Search with JSON
JSONQuery
JSONPath
JSONiq
Transforming JSON
JSONT
Our Agenda
What’s The Point?
JSON -
much
more ...
Questions?Tom Marrs
Architect
http://www.linkedin.com/in/TomMarrs
Twitter: @TomMarrs
Skype: t.marrs
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/
JSON ResourcesJSON SH - http://jsonsh.com/
JSON Schema Generator - http://jsonschema.net
JSON Schema Validator - http://json-schema-validator.herokuapp.com/
JSON Schema Spec - http://tools.ietf.org/html/draft-zyp-json-schema-03
Google JSON Style Guide - http://google-styleguide.googlecode.com/svn/trunk/jsoncstyleguide.xml
JSON ResourcesJSONQuery - https://github.com/jcrosby/jsonquery
JSONPath - http://goessner.net/articles/JsonPath/
JSONiq - http://www.jsoniq.org/
JSONT - http://goessner.net/articles/jsont/
JSON Groups
Google - http://groups.google.com/group/json-schema
Yahoo! - http://tech.groups.yahoo.com/group/json/