Json En-2x2 PDF
Json En-2x2 PDF
●
ECMAScript is standardized JavaScript.
●
The current version is the 10th edition.
Péter Jeszenszky – Ecma International, ECMAScript 2019 Language
Faculty of Informatics, University of Debrecen Specification, Standard ECMA-262, 10th Edition,
[email protected] June 2019.
http://www.ecma-international.org/publications/sta
Last modified: November 25, 2019 ndards/Ecma-262.htm
●
The next version currently under development
is ECMAScript 2020.
– ECMAScript 2020 Language Specification
https://tc39.github.io/ecma262/ 3
●
– The JavaScript engine of Chromium.
Chakra: the JavaScript engine developed for the Microsoft Edge web browser.
●
It can be used for client-side and server-side
– Its core is available as free and open source software from early 2016 (license: MIT License).
https://github.com/Microsoft/ChakraCore application development.
– See also: Microsoft Edge’s JavaScript engine to go open-source
●
https://blogs.windows.com/msedgedev/2015/12/05/open-source-chakra-core/
Nashorn (programming language: Java, license: GPLv2) http://openjdk.java.net/projects/nashorn/
●
Its package ecosystem, npm, is the largest
●
– JavaScript engine developed by Oracle that is included in JDK 8, JDK 9, and JDK 10 (see the package javax.script).
GraalVM Community Edition (CE) (programming language: Java; license: GPL v2) https://www.graalvm.org/
ecosystem of open source libraries in the
https://github.com/oracle/graal
– Available on Linux and macOS systems.
world.
See: https://www.graalvm.org/docs/reference-manual/languages/js/
See: http://www.modulecounts.com/
–
–
●
…
5 7
9 11
Compatibility History
●
ECMAScript compatibility of implementations: ●
Discovered and popularized by Douglas Crockford.
– ECMAScript 2016+ compatibility table – Was “discovered” in 2001.
http://kangax.github.io/compat-table/es2016plus/ – Crockford originally used JSON for communication
between JavaScript clients and Java servers.
– IEEE Computer Society, Discovering JavaScript Object
Notation with Douglas Crockford, 28 March 2012.
https://www.youtube.com/watch?v=kc8BAR7SHJI
●
„I don’t claim to have invented it, because it already existed in
nature. I just saw it, recognized the value of it, gave it a name,
and a description, and showed its benefits. But I did not invent
it. I don’t claim to be the first person to have discovered it.”
●
Source:
10 https://inkdroid.org/2012/04/30/lessons-of-json/ 12
File Properties JSON vs. ECMAScript
● File extension: .json ●
JSON is based on the syntax of ECMAScript,
but is not fully compatible with it.
● IANA media type: application/json
– Further information:
●
https://developer.mozilla.org/en-US/docs/Web/JavaScri
pt/Reference/Global_Objects/JSON
●
Magnus Holm, JSON: The JavaScript subset that isn't,
2014.
http://timelessrepo.com/json-isnt-a-javascript-subset
13 15
14 16
JSON vs. XML (2) JSON vs. XML (4)
●
Common characteristics of JSON and XML: ●
Example:
– Simplicity (undoubtedly JSON is the winner) – XML:
<file>
– Can be read and written easily by humans <uri>http://www.w3.org/Icons/w3c_home.png</uri>
<size>1936</size>
– Can be generated and processed easily by computer <contentType>image/png</contentType>
programs (undoubtedly JSON is the winner) <lastModified>2006-07-24T14:58:33Z</lastModified>
</file>
– Interoperability – JSON:
– Open standard {
"file": {
– Self-describing data representation "uri": "http://www.w3.org/Icons/w3c_home.png",
"size": 1936,
– Universal data exchange format "contentType": "image/png",
"lastModified": "2006-07-24T14:58:33Z"
}
17 } 19
21 23
29 31
30 32
Objects (4) Character Encoding
●
Example: ●
RFC 8259:
{
"artist": "Porcupine Tree", – JSON text exchanged between systems be
"title": "Fear of a Blank Planet",
"year": 2007,
encoded using UTF-8.
"tracks": [
{
"title": "Fear of a Blank Planet",
"length": 448
},
{
"title": "My Ashes",
"length": 307
},
{
"title": "Anesthetize",
"length": 1062
},
...
33 35
]
}
34 36
JSON-P: Java API for JSON
Editors (1)
Processing
●
Free and open source software: ●
JSR 374: Java API for JSON Processing 1.1 (Final Release)
– Atom (platform: Linux, macOS, Windows; license: MIT License) https://atom.io/ https://www.jcp.org/en/jsr/detail?id=374
https://github.com/atom/atom
●
Recommended package: Pretty JSON https://atom.io/packages/pretty-json – Was introduced in Java EE 7 (JSR 353).
– Eclipse (platform: Linux, macOS, Windows; license: Eclipse Public License 2.0)
https://www.eclipse.org/
– See the javax.json, javax.json.spi and javax.json.stream
●
Recommended plug-ins: packages.
JavaScript Development Tools (JSDT) https://www.eclipse.org/webtools/jsdt/
Implementations:
–
●
– JSON Editor Plugin https://marketplace.eclipse.org/content/json-editor-plugin https://github.com/boothen/Json-Eclipse-Plugin
– JSON Editor (programming language: JavaScript; license: Apache License 2.0)
https://github.com/josdejong/jsoneditor/
– Reference implementation provided as part of the GlassFish application
●
Online: http://jsoneditoronline.org/
server: https://javaee.github.io/jsonp/
– Notepad++ (platform: Windows; license: GPLv2) https://notepad-plus-plus.org/ ●
License: GPLv2/Common Development and Distribution License (CDDL) Version 1.1
https://github.com/notepad-plus-plus/notepad-plus-plus ● Available in the Maven Central Repository: org.glassfish:javax.json:*.
●
Recommended plugin: JSToolNpp http://www.sunjw.us/jstool/npp/ https://github.com/sunjw/jstoolnpp
– Visual Studio Code (platform: Linux, macOS, Windows; license: MIT License)
– Apache Johnzon (license: Apache License 2.0) http://johnzon.apache.org/
https://code.visualstudio.com/ https://github.com/Microsoft/vscode ● Available in the Maven Central Repository: org.apache.johnzon:johnzon:*.
●
See: Editing JSON with Visual Studio Code https://code.visualstudio.com/docs/languages/json
37 39
https://jcp.org/en/jsr/detail?id=367
– <oXygen/> XML Editor (platform: Linux, macOS, – Was introduced in Java EE 8.
Windows) https://www.oxygenxml.com/ – See the javax.json.bind, javax.json.bind.adapter,
javax.json.bind.annotation, javax.json.bind.config,
●
See: https://www.oxygenxml.com/xml_editor/json_editor.html javax.json.bind.serializer and javax.json.bind.spi packages.
Web page: http://json-b.net/
IntelliJ IDEA (platform: Linux, macOS, Windows)
●
–
Implementations:
https://www.jetbrains.com/idea/
●
– org.eclipse:yasson:*.
https://www.altova.com/xmlspy-xml-editor – Apache Johnzon (license: Apache License 2.0) http://johnzon.apache.org/johnzon-jsonb/
● Available in the Maven Central Repository: org.apache.johnzon:johnzon:*.
●
See: https://www.altova.com/xmlspy-xml-editor/json_editor
38 40
Libraries Gson (2)
●
See: http://json.org/ ●
Example:
– C++:
import java.math.BigDecimal;
●
nlohmann/json (license: MIT License) https://github.com/nlohmann/json import java.net.URL;
●
RapidJSON (license: MIT License) https://github.com/miloyip/rapidjson import java.util.List;
– Java:
●
Genson (license: Apache License 2.0) https://owlike.github.io/genson/ public class Movie {
●
Gson (license: Apache License 2.0) https://github.com/google/gson
●
Jackson (license: Apache License 2.0) private String title;
https://github.com/FasterXML/jackson private int year;
– Python: the json module is part of the standard library private BigDecimal rating;
https://docs.python.org/3/library/json.html private int votes;
private List<String> genres;
– R: private URL url;
●
jsonlite (license: MIT License) // ...
https://cran.r-project.org/web/packages/jsonlite/
●
rjson (license: GPLv2) https://cran.r-project.org/web/packages/rjson/ 41 } 43
import java.io.IOException;
●
Example: using a custom type adapter import java.time.ZoneId;
@Override
– See: java.time.LocalDate public void write(JsonWriter out, ZoneId value) throws IOException {
if (value == null) {
https://docs.oracle.com/en/java/javase/11/docs/api out.nullValue();
/java.base/java/time/LocalDate.html } else {
out.value(value.toString());
}
}
Gson gson = new Gson();
@Override
String s = gson.toJson(LocalDate.now()); public ZoneId read(JsonReader in) throws IOException {
System.out.println(s); if (in.peek() == JsonToken.NULL) {
in.nextNull();
// {"year":2019,"month":3,"day":15} return null;
}
return LocalDate.parse(in.nextString());
}
}
53 55
58 60
Applications (3) Applications (5)
●
Web APIs: ●
Storing configuration data (continued):
– Facebook APIs – AWS CloudFormation
https://developers.facebook.com/docs/apis-and-sd https://aws.amazon.com/cloudformation/
ks – WebExtensions
– Flickr API https://www.flickr.com/services/api/ https://developer.mozilla.org/en-US/docs/Mozilla/A
– Nominatim API dd-ons/WebExtensions
https://nominatim.org/release-docs/develop/api/Ov ● See: manifest.json
erview/ https://developer.mozilla.org/en-US/docs/Mozilla/Add-on
s/WebExtensions/manifest.json
– reddit API https://www.reddit.com/dev/api – …
– …
61 63
62 64
JSON Schema (2) JSON Schema (4)
●
Related specification: ●
JSON document: an information resource described by
the application/json media type, i.e., a JSON value.
– Paul C. Bryan (ed.), Kris Zyp, Mark Nottingham (ed.),
JavaScript Object Notation (JSON) Pointer, RFC ●
Instance: a JSON document to which a schema is
6901, April 2013. https://tools.ietf.org/html/rfc6901 applied.
●
Defines a syntax for identifying a specific value inside a ●
JSON schema:
JSON document. – A JSON document used to describe an instance.
– Examples:
● /country
– An object or a boolean.
● /places/0
– Can be nested one into another.
● /places/0/longitude ●
The outermost schema is called the root schema, the others are called
●
See: JSON Pointer Tester http://www.jsonpointer.com/ subschemas.
– IANA media type: application/schema+json
65 67
66 68
JSON Schema (6) JSON Schema (8)
●
Meta-schema: a schema that itself describes ●
Implementations: http://json-schema.org/implementations.html
C++:
a schema.
–
●
json-schema (license: Boost Software License 1.0)
https://github.com/KayEss/json-schema
– Example: – Java:
●
JSON Schema meta-schema: ●
json-schema (license: Apache License 2.0) https://github.com/everit-org/json-schema
– JavaScript:
– http://json-schema.org/draft/2019-09/schema ●
Ajv (license: MIT License) https://ajv.js.org/ https://github.com/epoberezkin/ajv
– http://json-schema.org/draft-07/schema – .NET:
●
Json.NET Schema (license: AGPLv3) http://www.newtonsoft.com/jsonschema
– Web interface: https://www.jsonschemavalidator.net/
●
Manatee.Json (license: MIT License) https://github.com/gregsdennis/Manatee.Json
– Python:
●
jsonschema (license: MIT License) https://github.com/Julian/jsonschema
69 71
70 72
JSON Schema (10) JSON Schema Examples (1)
Schema:
Associating JSON schemas with JSON Schema:
● ●
●
– true – false
documents: – {} – { "not": {} }
Valid instances: Valid instances:
JSON schema specifications do not provide any
● ●
–
42 None
means.
– –
– true ●
Invalid instances:
– Implementation specific solutions: – null – 42
– ["Gargamel", "Azrael"] – true
●
<oXygen/> XML Editor { "firstName": "Douglas",
– – null
https://www.oxygenxml.com/doc/versions/21.1/ug-editor "lastName": "Crockford" } ["Gargamel", "Azrael"]
/topics/json-associating-schema-directly-in-doc.html – …
–
– { "firstName": "Douglas",
●
Visual Studio Code: ●
Invalid instances: "lastName": "Crockford" }
https://code.visualstudio.com/docs/languages/json#_jso – None – …
n-schemas-and-settings
73 75
{
"type": "string"
– Michael Droettboom, Understanding JSON }
Schema, 2019. ●
Valid instances:
https://json-schema.org/understanding-json-schem – ""
a/index.html – "Hello, world!\n"
– …
●
Invalid instances:
– 42
– true
– null
– ["Gargamel", "Azrael"]
– { "firstName": "Douglas", "lastName": "Crockford" }
– …
74 76
JSON Schema Examples (3) JSON Schema Examples (5)
Schema:
Schema: Valid instances:
●
● ●
{
"type": "array",
"items": { "type": "string" }
{ – []
"type": "array",
} "items": { – ["Pi=",
●
Valid instances: "type": ["number", "string"]
– [] } 3.141593, "\n"]
}
– ["Hello, world!\n"] – …
– ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
– …
●
Invalid instances:
– "Hello, world!\n"
– 3.141593
– true
– null
– { "firstName": "Douglas", "lastName": "Crockford" }
– … 77 79
78 80
JSON Schema Examples (7) JSON Schema Examples (9)
●
Schema: ●
Schema:
{
{ "$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/draft-07/schema#", "type": "object",
"type": "object", "properties": {
"properties": { "artist": { "type": "string" },
"uri": { "type": "string", "format": "uri" }, "title": { "type": "string" },
"size": { "year": { "type": "integer" },
"type": "integer", "minimum": 0 }, "tracks": {
"type": "array",
"contentType": { "items": {
"type": "string", "type": "object",
"pattern": "^[a-z0-9][a-z0-9!#$&\\-^_]*/[a-z0-9][a-z0-9!#$&\\-^_]*$" "properties": {
}, "title": { "type": "string" },
"lastModified": { "length": { "type": "integer", "minimum": 0 }
"type": "string", "format": "date-time" } },
"required": ["title", "length"],
},
"additionalProperties": false
"required": ["uri", "size", "contentType", "lastModified"] },
} "minItems": 1
}
},
"required": ["artist", "title", "year", "tracks"],
81 "additionalProperties": false 83
}
90 92
XML–JSON Conversion (2) Query Languages
●
JSON-java (programming language: Java; ●
There exist a number of custom languages, I personally find the
following of them promising:
license: JSON License) – JSONiq http://www.jsoniq.org/
https://github.com/stleary/JSON-java ●
Declarative functional language based on XQuery for querying and processing JSON.
●
Implementation:
– The JSONObject() method of the XML class – Zorba – The NoSQL Query Processor (programming language: C++; platform: Linux, macOS,
Windows; license: Apache License 2.0) http://www.zorba.io/
converts an XML document to an equivalent JSON – jq (programming language: C; platform: Linux, macOS, Windows; license:
object. Expat License) https://stedolan.github.io/jq/
●
Command line JSON processor.
●
XSLTJSON (programming language: XSLT – JMESPath http://jmespath.org/
2.0; license: New BSD License) ●
Implementations: Go, Java, JavaScript, Lua, .NET, PHP, Python, Ruby, Rust (license:
MIT License)
https://github.com/bramstein/xsltjson – JSONata (license: MIT License) http://jsonata.org/
●
Query and transformation language inspired by the semantics of XPath 3.1 path
expressions.
●
JavaScript implementation.
93 95
94 96