On Using Json LD To Create Evolvable Restful Services
On Using Json LD To Create Evolvable Restful Services
directed graph that potentially contains cycles while its native data via an HTTP link header, provides a smooth upgrade path for
model is a tree. existing infrastructure as it allows most of the functionality with-
out having to change the contents of an existing document at all.
Figure 1 shows JSON-LD’s data model, a Linked Data graph.
Nodes in the graph are called subjects or objects and edges are Listing 1 contains a simple JSON-LD document that describes a
called properties (predicates in RDF). A subject is a node with at person based on the FOAF vocabulary [27]. First, a prefix for the
least one outgoing edge whereas an object is a node with at least FOAF vocabulary is defined (line 3) in the embedded context
one incoming edge. This implies that a node can be a subject and (lines 2-10) to abbreviate the long concept IRIs. Then, in
an object at the same time. To be unambiguously identifiable and lines 4-7, the three JSON properties title, name, and homepage
referenceable, a subject should be labeled with an IRI. This is not are mapped to concepts in the FOAF vocabulary. Additionally,
a strict requirement though; JSON-LD also supports unlabeled the value of the homepage property is specified to be of the type
nodes. Even though such nodes do not fulfill the requirements of @id, i.e., it is specified to be an IRI (line 8). Finally, in line 11, the
Linked Data, they are supported as they allow certain use cases person described in the document is unambiguously identified by
which require just locally referenceable data. The same applies to an IRI to make it possible to reference this person in other docu-
properties (edges): if they are labeled with an IRI they are refe- ments. The same mechanism allows JSON-LD documents con-
renceable from other documents and thus Linked Data; otherwise taining more information to be transcluded which enables clients
they are just traditional JSON properties that only have a meaning to discover new data by simply following those links; this
in the specific document they are used. The situation is slightly principle is known as Follow Your Nose [28]. By having all data
different for objects. If an object is labeled by an IRI, it is called semantically annotated as in the example, a machine client can be
an object; if it is labeled by something that is not an IRI, e.g. a programmed to “understand” that the document is about a person
number, it is denoted as a value, i.e., a literal in RDF. (line 12) and to figure out which properties specify the person’s
title (and in which language it is; lines 13-16), name (line 17) and
Given the reluctance of average Web developers to use semantic
the homepage of the organization it works for (line 18). A
technologies, huge efforts have been put into JSON-LD so that
JSON-LD publisher is free to choose between using terms that are
developers do not have to be knowledgeable about other semantic
mapped to concept IRIs in a vocabulary via a context as in the
Web technologies. All a developer needs to know is JSON and
example and using these IRIs directly in the document. Since this
two keywords (@context and @id) to use JSON-LD’s basic
flexibility results in variability that makes it more difficult to
functionality. Since JSON-LD is 100% compatible with plain old
JSON, developers can continue to use their existing tools and
libraries. This is especially important for enterprises as it allows 1 [ {
2 "@id": "http://me.markus-lanthaler.com",
them to add meaning to their JSON documents in a way that is not 3 "@type":"http://xmlns.com/foaf/0.1/Person",
disruptive to their operations and is transparent to their current 4 "http://xmlns.com/foaf/0.1/title": [
customers. At the same time JSON-LD is expressive enough to 5 {"@value":"Dipl.Ing.", "@language":"de"},
support all major RDF concepts. 6 {"@value":"MSc", "@language": "en"}
7 ],
The basic idea of JSON-LD is to create a description of the data in 8 "http://xmlns.com/foaf/0.1/name":
the form of a so called context. It links, similarly to SEREDASj 9 [ "Markus Lanthaler" ],
description documents, objects and their properties in a JSON 10 "http://xmlns.com/foaf/0.1/ ↵
document to concepts in an ontology. Furthermore, it allows workplaceHomepage":
values to be type-coerced and language tagged. A context can 11 [ { "@id": "http://www.tugraz.at/" } ]
12 } ]
either be directly embedded in a JSON-LD document or put into a
separate file and referenced from different documents. This, and
the fact that plain old JSON documents can reference a context Listing 2. The expanded form of the document in Listing 1
process the data, JSON-LD specifies two special document 1 @prefix rdf: <http://www.w3.org/1999/02/22-
forms: expanded and compacted. rdf-syntax-ns#> .
2 @prefix foaf: <http://xmlns.com/foaf/0.1/> .
The expanded form (Listing 2) is a JSON-LD document where all 3
terms and prefixes have been expanded into full IRIs and all type 4 <http://me.markus-lanthaler.com>
and language coercions are defined inline so that the context can 5 rdf:type foaf:Person ;
6 foaf:title
be eliminated from the document without losing any information.
7 "Dipl.Ing."@de ,
It can thus be seen as an explicit version of the document. To 8 "MSc"@en ;
assure that the resulting expanded document is easy to work with, 9 foaf:name "Markus Lanthaler" ;
also all properties that allow multiple values are converted to 10 foaf:workplaceHomepage
array form. This is necessary as different properties in a JSON-LD 11 <http://www.tugraz.at/> .
document might map to the same IRI that requires their values to
be merged. The more or less reverse process is compaction. It Listing 3. Triples extracted from Listing 1 converted to Turtle
takes a JSON-LD document and applies a user-specified context
to generate the most compact representation of a document, i.e.,
all full IRIs are translated to short terms (as specified in the transition table and the initial state to understand the communi-
supplied context) and all array values with a single entry are cation which is often not available or not practical. This also
unwrapped from that array form. Compacting Listing 2 with the makes it difficult or virtually impossible to recover from partial
context used in Listing 1 would result in a document equal to failures in such distributed systems.
Listing 1. Please note, however, that compaction is not always the To solve these issues and assure evolvability, the use of hyper-
exact inverse operation for expansion – it is, e.g., impossible to media is a core tenet of the REST architectural style. It refers to
split properties that have been merged to the same full IRI in the use of hypermedia controls in resource representations as a
expansion. Since expansion and compaction can be used together, way of navigating the state machine of an application. “A REST
applications can use them to harmonize data representations by API should be entered with no prior knowledge beyond the initial
translating between different contexts. For greater flexibility, URI (bookmark) and set of standardized media types. […] From
JSON-LD also defines a framing API method [29] which allows a that point on, all application state transitions must be driven by
developer to transform a document into a form that is convenient client selection of server-provided choices that are present in the
to process for a specific application. The developer defines a received representations or implied by the user’s manipulation of
frame, i.e., a template, which is then used to restructure the data those representations.” [30] While the human Web is unques-
contained in an arbitrary JSON-LD document into the desired tionably based on this type of interaction and state control-flow
form. This allows the developer to subsequently work with the where very little is known a priori, machine-to-machine commu-
framed document just as with any other JSON document which nication is often based on static contracts and out-of-band
means that usually all existing JSON tools and workflows can be knowledge resulting in tight coupling. Such approaches might
retained. work in the short term but are condemned to break in the long
Conversion of a JSON-LD document, especially one in the term since assumptions about server resources will break even-
expanded form, to RDF triples is straightforward. A subject, tually as resources evolve over time. Parastatidis et al. [31] define
which could also be used as an object in another triple, is defined the set of legal interactions necessary to achieve a specific,
by @id. All other JSON-LD properties are converted to application-dependent goal as the domain application protocol of
predicates. Finally, literal values are either taken directly from a a service. The protocol defines the interaction rules between the
property’s value, or created by taking the value of @value and different participants. Consequently, the application state is a
adding language (@language) and/or type information (@type). snapshot of the system at an instant in time. This coincides with
The example in Listing 1 could thus be converted to, e.g., a Turtle Fielding’s definition [1] of application state which defines it as the
document as shown in Listing 3. “pending requests, the topology of connected components (some
of which may be filtering buffered data), the active requests on
those connectors, the data flow of representations in response to
4. EVOLVABLE RESTFUL SERVICES those requests, and the processing of those representations as they
WITH JSON-LD are received by the user agent.” Accordingly, the overall system
As mentioned in the introduction, the Hypermedia as the Engine state consists of the application state and the server state. By using
of Application State (HATEOAS) constraint is one of the least the notion of a domain application protocol the phrase
understood constraints, and thus seldom implemented correctly. “hypermedia as the engine of application state” can now be
Annoyed by the fact that a lot of services claim to be RESTful explained as the use of hypermedia controls to advertise valid
regardless of violating the hypermedia constraint, Fielding [30] state transitions at runtime instead of agreeing on static contracts
made it very clear that hypermedia is a fundamental requirement at design time. Changes in the domain application protocol can
but since the term REST is so widely misused, there are efforts in thus be dynamically communicated to clients. This brings some of
the community to look for an alternative term, such as the human Web’s adaptivity to the Web of machines and allows
Hypermedia API, to denote truly RESTful services. the building of loosely coupled and evolvable systems. Rather
than requiring an understanding of a specific IRI structure, clients
A lot of systems, regardless of claiming to be RESTful, rely only need to understand the semantics or business context in
heavily on implicit state control-flow which is characteristic of the which a link appears [31].
RPC-style. The allowed messages and how they have to be inter-
preted depends on previously exchanged messages and thus in The creation of a truly RESTful service can be dramatically sim-
which implicit state the system is in. Third parties or interme- plified by the use of a well-defined, generic media type with
diaries trying to interpret the conversation need the full state inherent hypermedia support. Developers can then fully con-
Management Node process to be further streamlined as it enables the creation of
unit reusable client libraries.