enos.itcollege.ee

Download Report

Transcript enos.itcollege.ee

!Markup languages and
RESTful'ness
Mait Poska
Structure
•
•
•
JSON
YAML
RESTful'ness
!Markup language JSON
JSON stands for Javascript Object
Notation.
Alternative to XML since 2002.
Used for AJAX, RESTful HTTP requests
and elsewhere.
JSON is...
•
•
•
•
...
...
...
...
•
No element names
o Based on structure
... complete standard. No versions.
Language Independent
Text-based
Light-weight
Easy to parse
o
JSON is basically a hash map.
JSON is...
... a serialization format ...
... BUT ...
•
•
•
... functions can't be serialized
... there are no invisible structures
... there are no cyclical/recurring
structures
JSON Value types
JSON can hold following datatypes:
•
•
•
•
•
Strings
Numbers
Booleans
Objects
Arrays
JSON - String type
• String data is wrapped between
•
•
"double quotes"
Backslash escaping
Character is a string with
a length of 1
o Character is encoding dependent
JSON - Number type
• Integer
• Real
• Scientific
•
•
0
1
4
254
23.1 0.1 0.0004
20E+2 12.5E-2
No octal or hex values
No NaN or Infinity
o Use null instead
0x10
JSON - Boolean type
• true
• false
JSON - "Nothing" type
• null
JSON - Object type
• Key/value Hash map
• Unordered
• Keys are strings
• Values are JSON values
JSON - Array type
• Ordered
• Values are JSON values
• No indexing
•
o Indexing must be implemented in
used language.
Arrays are wrapped in [ ] brackets
JSON Example
[{"IsMember" : true, "Name" : "John", "Age" : 24},
{"IsMember" : false, "Name" : "Paul", "Age" : 44},
{"IsMember" : true, "Name" : "George", "Age" : 12}]
{ "store" : {
"book" : [
{ "author" : "Nigel Rees", "category" : "reference", "price" : 8.95,
"title" : "Sayings of the Century"},
{ "author" : "Evelyn Waugh", "category" : "fiction", "price" : 12.99,
"title" : "Sword of Honour"},
{ "author" : "Herman Melville", "category" : "fiction", "isbn" : "0-553-21311-3",
"price" : 8.99, "title" : "Moby Dick"},
{ "author" : "J. R. R. Tolkien", "category" : "fiction", "isbn" : "0-395-19395-8",
"price" : 22.99, "title" : "The Lord of the Rings"}
]
}
}
JSON vs XML
<menu id="file" value="File">
<popup>
<menuitem value="New" onclick="CreateNewDoc()" />
<menuitem value="Open" onclick="OpenDoc()" />
{"menu": {
"id": "file",
"value": "File",
<menuitem value="Close" onclick="CloseDoc()" />
</popup>
</menu>
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
JSONP
• Cross-domain requests
• Used via GET method
RAW HTTP Request:
GET http://testtest/test.json?callback=callback1319854793396&method=color HTTP/1.1
Host: testtest
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko)
Chrome/15.0.874.106 Safari/535.2
Accept: */*
Referer: http://localhost:8888/jquery/Test.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
JSONP
RAW HTTP Response:
HTTP/1.1 200 OK
Date: Sat, 29 Oct 2011 02:21:24 GMT
Server: Apache/1.3.33 (Unix) mod_ssl/2.8.22 OpenSSL/0.9.7d SE/0.5.3
Last-Modified: Fri, 28 Oct 2011 17:48:47 GMT
ETag: "166a2402-10-4eaaeaff"
Accept-Ranges: bytes
Content-Length: 37
Content-Type: text/plain
Connection: close
callback1319854793396{"red" : "#f00"}
!Markup language YAML
YAML stands for:
"YAML Ain't a Markup Language"
or "YAML Ain't Markup Language"
"YAML is a human friendly data
serialization standard for all
programming languages."
YAML
The design goals for YAML are, in decreasing priority:
1. YAML is easily readable by humans.
2. YAML data is portable between programming
languages.
3. YAML matches the native data structures of agile
languages.
4. YAML has a consistent model to support generic tools.
5. YAML supports one-pass processing.
6. YAML is expressive and extensible.
7. YAML is easy to implement and use.
Source: http://www.yaml.org/spec/1.2/spec.html
YAML
• Formatted text based
• Elements nesting with sub-alignment
• From some aspects very similar to JSON
o
•
o
As well key/value hasmap
Human readibility
Supports various data types serializing
Still no function serialization because of textbasedness
DO NOT USE TABS FOR ALIGNING!!!
YAML - Light overview
Data types
integers:
miscellaneous:
canonical: 12345
null:
decimal: +12345
booleans: [true, false]
octal: 0o14
string: '012345'
hexadecimal: 0xC
floating point:
canonical: 1.2345e+6
exponential: 12.345e+06
fixed: 123.45
negative infinity: -.inf
not a number: .NaN
YAML - Light overview
Data types
timestamps:
canonical: 2001-12-15T02:59:43.1Z
iso8601: 2001-12-14t21:59:43.10-05:00
spaced: 2001-12-14 21:59:43.10 -5
date: 2002-12-14
explicit tag example:
picture: !!binary |
R0lGODlhDAAMAIQAAP//9/X
17unp5WZmZgAAAOfn515eXv
Pz7Y6OjuDg4J+fn5OTk6enp
56enmleECcgggoBADs=
as well !!str,
!!seq and !!map
can be used
YAML - Light overview
Commenting
# A comment for following data
foo:
# Some comment
# and more
foo1: false
foo2:
null value
YAML - Light overview
Structures
# The Presidents
# of Estonia
--- Lennart Meri
- Arnold Rüütel
- Toomas Hendrik Ilves
--- # IT gods
- Bill Gates
- Steve Jobs
# Complex mapping
--name: Peeter
age: 25
sex: male
--name: Krõõt
age: 89
sex: female
YAML - Light overview
Scalars
multiline:
# ASCII Art
--- |
\//||\/||
// || ||__
--- >
Mark McGwire's
year was crippled
by a knee injury.
New lines are new line
New lines become
spaces
YAML - Light overview
Collections
items:
name:
name:
name:
name:
Item nr 1
Item nr 2
Item nr 3
Item nr 4
YAML - Light overview
Complex example - log file
---
---
Time: 2001-11-23 15:01:42 -5
Date: 2001-11-23 15:03:17 -5
User: ed
User: ed
Warning:
Fatal:
This is an error message
for the log file
---
Unknown variable "bar"
Stack:
- file: TopClass.cs
Time: 2001-11-23 15:02:31 -5
line: 23
User: ed
code: |
Warning:
A slightly different error
message.
var x = new MoreObject("345\n");
- file: MoreClass.cs
line: 58
code: |foo = bar
YAML - Light overview
YAML can get very complex.
Because of its complexity, very good
tool for configurations.
To get full overview of language's
possibilities look into YAML
specification:
http://www.yaml.org/spec/1.2/spec.html
YAML .NET interpreters
•
•
•
YamlDotNet
o
http://www.aaubry.net/yamldotnet.aspx
YAML 1.1 library
o
http://yaml-net-parser.sourceforge.net/
(in-progress) YAML 1.1 implementation
o
http://www.codeplex.com/yaml/
RESTful
REST stands for Representational state
transfer.
Most of the web uses RESTful methods.
Can be used for web sites and as well for web
services.
REST representations
Data is represented in two main formats:
XML
JSON
•
•
Still, other serialization methods can be used.
REST is not a standard
BUT uses many standards:
HTTP
URL
JSON/XML/JPG/PDF/etc
(Resource representations)
•
•
•
text/xml, text/html, image/gif,
image/jpeg, etc (Resource Types, MIME
Types)
REST constraints
• Client–server
• Stateless
• Cacheable
• Layered system
• Code on demand (optional)
• Uniform interface
RESTful resource naming
REST uses URI to identify resources
•
•
•
http://localhost/items/
http://localhost/item/ISBN-0011
http://localhost/item/ISBN-0011/owners
•
•
•
http://localhost/owners
http://localhost/owner/4
http://localhost/owner/John/items
RESTful request example
Source: http://1.bp.blogspot.com/_duvAWoBVn0/T0PkzehjvSI/AAAAAAAAAqM/Aec4NgIu6ow/s1600/REST-API-architecture-clientserver-model.png
RESTful actions
Actions that can be performed on resources:
HTTP POST
HTTP GET
HTTP PUT
HTTP DELETE
•
•
•
•
Strong notation to use singular and plural for
resources addressing.
RESTful - action POST
• Issuing a POST request creates resource or
resources on server side.
•
POST http://localhost/item
o
•
{"name": "testName",
"type": "testType",
"value": "testValue"}
POST http://localhost/items
o
[{"name": "testName", "type": "testType", "value":
"testValue"}, {"name": "testName", "type":
"testType", "value": "testValue"}]
RESTful - action GET
• Issuing a GET request transfers the data
from the server to the client in some
representation
•
•
•
GET http://localhost/items
o
Get all items
GET http://localhost/item/3
o
Get item with identificator 3
GET http://localhost/item/3/owners
o
Get all owners of the item
RESTful - action PUT
• Issuing a PUT request updates the data of
related item
•
PUT http://localhost/item/3
o
•
{"name": "testName",
"type": "testType",
"value": "testValue"}
PUT http://localhost/items/3/4/5/6
o
{"value": "testValue"}
RESTful - action DELETE
• Issuing a DELETE request deletes the
related item or items from resources
•
•
•
DELETE http://localhost/item/3
DELETE http://localhost/items
o [3,4,5,6,7,8]
DELETE http://localhost/items/3/4/5/6/7
These RESTful methods can be implemented
by ourselves. So numerous possibilities.
RESTful methods
Two additional request methods: PUT, DELETE
Addressing\Request action
POST
GET
PUT
DELETE
http://localhost/resources/
Create
collection of
items
Get list of
items
Replace
items
collection
Delete
collection of
items
http://localhost/resources/item##
Create item
Get item
Update item
Delete item
Shoot me with questions!