Verbale definitivo elezioni R.S.U.

JSON
Vittorio Maniezzo – Università di Bologna
1
JSON (adapted from wikipedia)
JSON (JavaScript Object Notation), is an open standard format
that uses human-readable text to store data objects consisting
of attribute–value pairs. It is an alternative to XML.
JSON is a language-independent data format (even JavaScript).
Code for parsing and generating JSON data is available in a
large variety of programming languages.
JSON was originally specified by Douglas Crockford. It is
currently described by two competing standards, RFC 7159
and ECMA-404.
The MIME type for JSON is application/json.
The JSON filename extension is .json.
Vittorio Maniezzo – Università di Bologna
2
1
Why JSON
W.r.t. to XML, which was used before, json benefits are:
• Structured data format like XML
• High signal to noise ratio: it does away with extra characters which
are not conclusive to the data (angle brackets and slashes in XML)
• Compact data format
• Simple parsing rules, the processing of data is easy and fast
• Versionless: JSON has no version number, no revisions to the JSON
grammar are anticipated, JSON is very stable.
Good for the following scenarios:
• Data exchange between same or different platforms like Java,
.NET services over the wire.
• Data storage: ex. MongoDB (www.mongodb.org) uses JSON as an
internal storage format.
Vittorio Maniezzo – Università di Bologna
3
Properties of JSON
• It's simultaneously human- and machine-readable format.
• It has support for Unicode, allowing almost any
information in any human language to be communicated;
• It’s a self-documenting format that describes structure
and field names as well as specific values.
• It has strict syntax and parsing requirements that allow
the necessary parsing algorithms to remain simple,
efficient, and consistent;
• It has the ability to represent the most general computer
science data structures: records, lists and trees.
Vittorio Maniezzo – Università di Bologna
4
2
Beyond the web
JSON started as the "serialized object notation" for JavaScript and has
become the data structure of the Web. It's a simple data format that
allows programmers to store and communicate sets of values, lists,
and key-value mappings across systems.
As JSON adoption has grown, database vendors have sprung up
offering JSON-centric document databases. Now, increasingly,
traditional relational-style databases are integrating JSON features.
With a nice combination of simplicity and "just enough structure,"
JSON has quickly expanded beyond the Web into applications and
services. For example, JSON is displacing the more complex XML
format as the serialization format for exchanging data between
applications.
Vittorio Maniezzo – Università di Bologna
5
JSON example
{
"Esempio0": { "cap":20,
"val":[3,2,3,1,2],
"Q“ :[7,2,9,3,1]
},
"Esempio1": { "cap":190,
"val":[50,50,64,46,50,5],
"Q“ :[56,59,80,64,75,17]
},
"Esempio2": { "cap":100,
"val":[40,35,18,4,10,2],
"Q“ :[100,50,45,20,10,5]
}
}
Vittorio Maniezzo – Università di Bologna
6
3
Values (adapted from www.json.org)
Strings
Numbers
Booleans
Objects
Arrays
null
Vittorio Maniezzo – Università di Bologna
7
Strings
Sequence of 0 or more Unicode
characters
No separate character type: a
character is represented as a
string with a length of 1
Wrapped in "double quotes"
Backslash escapement
Vittorio Maniezzo – Università di Bologna
8
4
Numbers
Integer
Real
Scientific
No octal or hex
No NaN or Infinity, only null
null 
a value that isn't anything
Vittorio Maniezzo – Università di Bologna
9
Object
Objects are unordered containers of key/value pairs
Objects are wrapped in { }
, separates key/value pairs
: separates keys and values
Keys are strings
Values are JSON values
Ex.: {“nome”:”pippo”,”anno”:1932 }
Vittorio Maniezzo – Università di Bologna
10
5
Array
Arrays are ordered sequences of values
Arrays are wrapped in []
, separates values
Ex:
["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"]
[ [0, -1, 0],
[1, 0, 0],
[0, 0, 1]
]
Vittorio Maniezzo – Università di Bologna
11
Arrays vs Objects
Use objects when the key names are arbitrary strings.
Use arrays when the key names are sequential integers.
Don't get confused by the term Associative Array.
Vittorio Maniezzo – Università di Bologna
12
6
Using JSON in JavaScript
JSON encoded data  JavaScript object
• var myObject = eval('(' + myJSONtext + ')');
• var myObject = JSON.parse(myJSONtext);
JavaScript value  JSON encoded data
• var myJSONText = JSON.stringify(myObject);
Vittorio Maniezzo – Università di Bologna
13
Using JSON with XmlHttpRequest
For sending JSON encoded data to the server use HTTP POST
method and send the JSON encoded data in the body of the
request
// xmlhttp is an XmlHttpRequest object
xmlhttp.setRequestHeader('Content-type',
'application/x-www-form-urlencoded;charset=UTF-8;');
xmlhttp.send('jsondata=' + escape(myJSONText));
Handling JSON encoded data from the server
• Server should set the content type to "text/plain"
• In the handler function of xmlhttp object, read
xmlhttp.responseText
Vittorio Maniezzo – Università di Bologna
14
7
Using JSON in C++
Need a library to serialize / deserialize data. Many available on the
web (list increasing daily):
• fastJSON (arguably the fastest, http://fastjson.codeplex.com/)
• supereasyjson (arguably the easiest to use,
http://sourceforge.net/projects/supereasyjson/)
• ThorsSerializer
• JsonBox
• json-cpp
• CAJUN
• libjson
• rapidjson
• jsoncons
• JSON++
• Microsoft REST
• JSON Spirit
• picojson
Vittorio Maniezzo – Università di Bologna
15
Using JSON in c#
Directly supported within the framework (since framework 3.5), via namespaces
System.Runtime.Serialization and System.Runtime.Serialization.Json, using an
instance of DataContractJsonSerializer (somewhat awkward to me):
Serialization:
DataContractJsonSerializer js = new
DataContractJsonSerializer(typeof(Person));
MemoryStream stream = new
MemoryStream(Encoding.UTF8.GetBytes(source));
Person p = (Person)js.ReadObject(stream);
Deserialization:
DataContractJsonSerializer js = new
DataContractJsonSerializer(typeof(Person));
MemoryStream stream = new MemoryStream();
js.WriteObject(stream, p);
JSON.NET, free library (available via NuGet)
Serialization: string js = JsonConvert.SerializeObject(person);
Deserialization: Person p =
JsonConvert.DeserializeObject<Person>(source);
Vittorio Maniezzo – Università di Bologna
16
8
Siti utili
http://www.json.org/
by D. Crockford
http://en.wikipedia.org/wiki/JSON
http://jsoneditoronline.org/
one out thousands
http://sourceforge.net/projects/supereasyjson/
http://james.newtonking.com/json
Vittorio Maniezzo – Università di Bologna
JSON.net
17
9