-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathindex.html
More file actions
4317 lines (4006 loc) · 230 KB
/
index.html
File metadata and controls
4317 lines (4006 loc) · 230 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<title>JSON-LD 1.1 Processing Algorithms and API</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="https://www.w3.org/Tools/respec/respec-w3c-common" class="remove"></script>
<script type="text/javascript" src="../common/common.js" class="remove"></script>
<script type="text/javascript" class="remove">
//<![CDATA[
var respecConfig = {
// extend the bibliography entries
localBiblio: jsonld.localBiblio,
// specification status (e.g. WD, LCWD, NOTE, etc.). If in doubt use ED.
specStatus: "CG-DRAFT",
// if you wish the publication date to be other than today, set this
//publishDate: "2013-11-05",
copyrightStart: "2010",
// the specification's short name, as in http://www.w3.org/TR/short-name/
shortName: "json-ld-api",
// if there is a previously published draft, uncomment this and set its YYYY-MM-DD date
// and its maturity status
prevVersion: "https://www.w3.org/TR/2014/REC-json-ld-api-20140116/",
previousPublishDate: "2014-01-16",
previousMaturity: "REC",
previousDiffURI: "http://www.w3.org/TR/2014/REC-json-ld-api-20140116/",
// if there a publicly available Editor's Draft, this is the link
edDraftURI: "http://json-ld.org/spec/latest/json-ld-api/",
testSuiteURI: "http://json-ld.org/test-suite/",
// if you want to have extra CSS, append them to this list
// it is recommended that the respec.css stylesheet be kept
// extraCSS: [],
issueBase: "https://github.com/json-ld/json-ld.org/issues/",
githubAPI: "https://api.github.com/repos/json-ld/json-ld.org",
includePermalinks: true,
noRecTrack: false,
testSuiteURIkey: "http://json-ld.org/test-suite/",
postProcess: [internalizeTermListReferences],
// editors, add as many as you like
// only "name" is required
editors: [
{ name: "Markus Lanthaler", url: "http://www.markus-lanthaler.com/",
company: "Graz University of Technology", companyURL: "http://www.tugraz.at/" },
{ name: "Gregg Kellogg", url: "http://greggkellogg.net/",
company: "Kellogg Associates", companyURL: "http://kellogg-assoc.com/" },
{ name: "Manu Sporny", url: "http://manu.sporny.org/",
company: "Digital Bazaar", companyURL: "http://digitalbazaar.com/" }
],
// authors, add as many as you like.
// This is optional, uncomment if you have authors as well as editors.
// only "name" is required. Same format as editors.
authors: [
{ name: "Dave Longley", url: "http://digitalbazaar.com/",
company: "Digital Bazaar", companyURL: "http://digitalbazaar.com/"},
{ name: "Gregg Kellogg", url: "http://greggkellogg.net/",
company: "Kellogg Associates", companyURL: "http://kellogg-assoc.com/" },
{ name: "Markus Lanthaler", url: "http://www.markus-lanthaler.com/",
company: "Graz University of Technology", companyURL: "http://www.tugraz.at/" },
{ name: "Manu Sporny", url: "http://digitalbazaar.com/",
company: "Digital Bazaar", companyURL: "http://digitalbazaar.com/" }
],
bugTracker: {
open: "https://github.com/json-ld/json-ld.org/issues?utf8=✓&q=is%3Aissue%20label%3Aapi%20is%3Aopen%20milestone%3A%22JSON-LD%201.1%22%20",
new: "https://github.com/json-ld/json-ld.org/issues/new"
},
otherLinks: [{
key: "Version control",
data: [{
value: "Github Repository",
href: "https://github.com/json-ld/json-ld.org"
}]
}],
// name of the WG
wg: "JSON for Linking Data W3C Community Group",
// URI of the public WG page
wgURI: "http://www.w3.org/community/json-ld/",
// name (with the @w3c.org) of the public mailing to which comments are due
wgPublicList: "public-linked-json",
// URI of the patent status for this WG, for Rec-track documents
// !!!! IMPORTANT !!!!
// This is important for Rec-track documents, do not copy a patent URI from a random
// document unless you know what you're doing. If in doubt ask your friendly neighbourhood
// Team Contact.
wgPatentURI: "http://www.w3.org/2004/01/pp-impl/46168/status",
maxTocLevel: 2,
//alternateFormats: [ {uri: "diff-20130910.html", label: "diff to previous version"} ]
};
//]]>
</script>
<style type="text/css">
.highlight {
font-weight: bold;
color: #0a3;
}
.comment {
color: #999;
}
.error a {
color: #ff4500;
border-bottom: 1px dotted #ff4500;
text-decoration: none;
}
ol.algorithm {
counter-reset: numsection;
list-style-type: none;
}
ol.algorithm li {
margin: 0.5em 0;
}
ol.algorithm li:before {
font-weight: bold;
counter-increment: numsection;
content: counters(numsection, ".") ") ";
}
.changed {
background-color: rgb(215, 238, 197);
}
.changed:hover {
color: green;
background-color: inherit;
}
</style>
</head>
<body>
<section id="abstract">
<p>This specification defines a set of algorithms for programmatic transformations
of JSON-LD documents. Restructuring data according to the defined transformations
often dramatically simplifies its usage. Furthermore, this document proposes
an Application Programming Interface (API) for developers implementing the
specified algorithms.</p>
</section>
<section id="sotd">
<p>This document has been developed by the
<a href="http://www.w3.org/community/json-ld/">JSON for Linking Data W3C Community Group</a>.
The document has been transferred to the RDF Working Group for review, improvement,
and publication along the Recommendation track. The specification has undergone
significant development, review, and changes during the course of several years.</p>
<!--<p>There are several independent
<a href="http://json-ld.org/test-suite/reports/">interoperable implementations</a> of
this specification, a test suite [[JSON-LD-TESTS]] and a
<a href="http://json-ld.org/playground/">live JSON-LD editor</a> that is capable
of demonstrating the features described in this document.</p> -->
</section>
<section class="informative">
<h1>Introduction</h1>
<p>This document is a detailed specification of the JSON-LD processing algorithms.
The document is primarily intended for the following audiences:</p>
<ul>
<li>Software developers who want to implement the algorithms to transform
JSON-LD documents.</li>
<li>Web authors and developers who want a very detailed view of how
a <a>JSON-LD Processor</a> operates.</li>
<li>Developers who want an overview of the proposed JSON-LD API.</li>
</ul>
<p>To understand the basics in this specification you must first be familiar with
JSON, which is detailed in [[!RFC7159]]. You must also understand the
JSON-LD syntax defined in [[!JSON-LD]], which is the base syntax used by all
of the algorithms in this document. To understand the API and how it is
intended to operate in a programming environment, it is useful to have working
knowledge of the JavaScript programming language [[ECMA-262]] and
WebIDL [[WebIDL-2]]. To understand how JSON-LD maps to RDF, it is helpful to be
familiar with the basic RDF concepts [[RDF11-CONCEPTS]].</p>
<section>
<h2>Contributing</h2>
<p>There are a number of ways that one may participate in the development of
this specification:</p>
<ul>
<li>Technical discussion typically occurs on the public mailing list:
<a href="http://lists.w3.org/Archives/Public/public-linked-json/">public-linked-json@w3.org</a></li>
<!--<li><a href="http://json-ld.org/minutes/">Public teleconferences</a> are held
on Tuesdays at 1500UTC on the second and fourth week of each month.</li> -->
<li>The <a href="http://webchat.freenode.net/?channels=json-ld">#json-ld</a>
IRC channel is available for real-time discussion on irc.freenode.net.</li>
</ul>
</section>
<section>
<h3>Terminology</h3>
<div data-include="../common/terms.html"
data-oninclude="restrictReferences"></div>
</section>
<section>
<h2>Typographical conventions</h2>
<div data-include="../common/typographical-conventions.html"></div>
</section>
</section> <!-- end of Introduction -->
<section class="informative">
<h1>Features</h1>
<p>The JSON-LD Syntax specification [[!JSON-LD]] defines a syntax to
express Linked Data in JSON. Because there is more than one way to
express Linked Data using this syntax, it is often useful to be able to
transform JSON-LD documents so that they may be more easily consumed by
specific applications.</p>
<p>JSON-LD uses <a>contexts</a> to allow Linked Data
to be expressed in a way that is specifically tailored to a particular
person or application. By providing a <a>context</a>,
JSON data can be expressed in a way that is a natural fit for a particular
person or application whilst also indicating how the data should be
understood at a global scale. In order for people or applications to
share data that was created using a <a>context</a> that is different
from their own, a JSON-LD processor must be able to transform a document
from one <a>context</a> to another. Instead of requiring JSON-LD
processors to write specific code for every imaginable
<a>context</a> switching scenario, it is much easier to specify a
single algorithm that can remove any <a>context</a>. Similarly,
another algorithm can be specified to subsequently apply any
<a>context</a>. These two algorithms represent the most basic
transformations of JSON-LD documents. They are referred to as
<a>expansion</a> and <a>compaction</a>, respectively.</p>
<p>There are four major types of transformation that are discussed in this
document: expansion, compaction, flattening, and RDF serialization/deserialization.</p>
<section class="informative">
<h2>Expansion</h2>
<p>The algorithm that removes <a>context</a> is
called <dfn data-lt="expanded|expanding">expansion</dfn>. Before performing any other
transformations on a JSON-LD document, it is easiest to
remove any <a>context</a> from it and to make data structures
more regular.</p>
<p>To get an idea of how context and data structuring affects the same data,
here is an example of JSON-LD that uses only <a>terms</a>
and is fairly compact:</p>
<pre class="example" data-transform="updateExample"
title="Sample JSON-LD document">
<!--
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"homepage": {
"@id": "http://xmlns.com/foaf/0.1/homepage",
"@type": "@id"
}
},
"@id": "http://me.markus-lanthaler.com/",
"name": "Markus Lanthaler",
"homepage": "http://www.markus-lanthaler.com/"
}
-->
</pre>
<p>The next input example uses one <a>IRI</a> to express a property
and an <a>array</a> to encapsulate another, but
leaves the rest of the information untouched.</p>
<pre class="example" data-transform="updateExample"
title="Sample JSON-LD document using an IRI instead of a term to express a property">
<!--
{
"@context": {
****"website": "http://xmlns.com/foaf/0.1/homepage"****
},
"@id": "http://me.markus-lanthaler.com/",
"****http://xmlns.com/foaf/0.1/name****": "Markus Lanthaler",
****"website"****: ****{ "@id":**** "http://www.markus-lanthaler.com/" ****}****
}
-->
</pre>
<p>Note that both inputs are valid JSON-LD and both represent the same
information. The difference is in their <a>context</a> information
and in the data structures used. A JSON-LD processor can remove
<a>context</a> and ensure that the data is more regular by employing
<a>expansion</a>.</p>
<p><a>Expansion</a> has two important goals: removing any contextual
information from the document, and ensuring all values are represented
in a regular form. These goals are accomplished by expanding all properties
to <a>absolute IRIs</a> and by expressing all
values in <a>arrays</a> in
<a>expanded form</a>. <a>Expanded form</a> is the most verbose
and regular way of expressing of values in JSON-LD; all contextual
information from the document is instead stored locally with each value.
Running the <a href="#expansion-algorithm">Expansion algorithm</a>
(<a data-link-for="JsonLdProcessor">expand</a>)
operation) against the above examples results in the following output:</p>
<pre class="example" data-transform="updateExample"
title="Expanded sample document">
<!--
[
{
"@id": "http://me.markus-lanthaler.com/",
"http://xmlns.com/foaf/0.1/name": [
{ "@value": "Markus Lanthaler" }
],
"http://xmlns.com/foaf/0.1/homepage": [
{ "@id": "http://www.markus-lanthaler.com/" }
]
}
]
-->
</pre>
<p>Note that in the output above all <a>context</a> definitions have
been removed, all <a>terms</a> and
<a>compact IRIs</a> have been expanded to absolute
<a>IRIs</a>, and all
<a>JSON-LD values</a> are expressed in
<a>arrays</a> in <a>expanded form</a>. While the
output is more verbose and difficult for a human to read, it establishes a
baseline that makes JSON-LD processing easier because of its very regular
structure.</p>
</section> <!-- end of Expansion -->
<section class="informative">
<h2>Compaction</h2>
<p>While <a>expansion</a> removes <a>context</a> from a given
input, <dfn data-lt="compact|compacting|compacted|compacts">compaction</dfn>'s primary function is to
perform the opposite operation: to express a given input according to
a particular <a>context</a>. <a>Compaction</a> applies a
<a>context</a> that specifically tailors the way information is
expressed for a particular person or application. This simplifies applications
that consume JSON or JSON-LD by expressing the data in application-specific
terms, and it makes the data easier to read by humans.</p>
<p><a>Compaction</a> uses a developer-supplied <a>context</a> to
shorten <a>IRIs</a> to <a>terms</a> or
<a>compact IRIs</a> and
<a>JSON-LD values</a> expressed in
<a>expanded form</a> to simple values such as <a>strings</a>
or <a>numbers</a>.</p>
<p>For example, assume the following expanded JSON-LD input document:</p>
<pre class="example" data-transform="updateExample"
title="Expanded sample document">
<!--
[
{
"@id": "http://me.markus-lanthaler.com/",
"http://xmlns.com/foaf/0.1/name": [
{ "@value": "Markus Lanthaler" }
],
"http://xmlns.com/foaf/0.1/homepage": [
{ "@id": "http://www.markus-lanthaler.com/" }
]
}
]
-->
</pre>
<p>Additionally, assume the following developer-supplied JSON-LD
<a>context</a>:</p>
<pre class="example" data-transform="updateExample"
title="JSON-LD context">
<!--
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"homepage": {
"@id": "http://xmlns.com/foaf/0.1/homepage",
"@type": "@id"
}
}
}
-->
</pre>
<p>Running the <a href="#compaction-algorithm">Compaction Algorithm</a>
(<a data-link-for="JsonLdProcessor">compact</a>)
operation) given the context supplied above against the JSON-LD input
document provided above would result in the following output:</p>
<pre class="example" data-transform="updateExample"
title="Compacted sample document">
<!--
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"homepage": {
"@id": "http://xmlns.com/foaf/0.1/homepage",
"@type": "@id"
}
},
"@id": "http://me.markus-lanthaler.com/",
"name": "Markus Lanthaler",
"homepage": "http://www.markus-lanthaler.com/"
}
-->
</pre>
<p>Note that all <a>IRIs</a> have been compacted to
<a>terms</a> as specified in the <a>context</a>,
which has been injected into the output. While compacted output is
useful to humans, it is also used to generate structures that are easy to
program against. Compaction enables developers to map any expanded document
into an application-specific compacted document. While the context provided
above mapped <code>http://xmlns.com/foaf/0.1/name</code> to <code>name</code>, it
could also have been mapped to any other term provided by the developer.</p>
</section> <!-- end of Compaction -->
<section class="informative">
<h2>Flattening</h2>
<p>While expansion ensures that a document is in a uniform structure,
flattening goes a step further to ensure that the shape of the data
is deterministic. In expanded documents, the properties of a single
<a>node</a> may be spread across a number of different
<a>JSON objects</a>. By flattening a
document, all properties of a <a>node</a> are collected in a single
<a>JSON object</a> and all <a>blank nodes</a>
are labeled with a <a>blank node identifier</a>. This may drastically
simplify the code required to process JSON-LD data in certain applications.</p>
<p>For example, assume the following JSON-LD input document:</p>
<pre class="example" data-transform="updateExample"
title="Sample JSON-LD document">
<!--
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"knows": "http://xmlns.com/foaf/0.1/knows"
},
"@id": "http://me.markus-lanthaler.com/",
"name": "Markus Lanthaler",
"knows": [
{
"name": "Dave Longley"
}
]
}
-->
</pre>
<p>Running the <a href="#flattening-algorithm">Flattening Algorithm</a>
(<a data-link-for="JsonLdProcessor">flatten</a>)
operation) with a context set to <a>null</a> to prevent compaction
returns the following document:</p>
<pre class="example" data-transform="updateExample"
title="Flattened sample document in expanded form">
<!--
[
{
"@id": "_:t0",
"http://xmlns.com/foaf/0.1/name": [
{ "@value": "Dave Longley" }
]
},
{
"@id": "http://me.markus-lanthaler.com/",
"http://xmlns.com/foaf/0.1/name": [
{ "@value": "Markus Lanthaler" }
],
"http://xmlns.com/foaf/0.1/knows": [
{ "@id": "_:t0" }
]
}
]
-->
</pre>
<p>Note how in the output above all properties of a <a>node</a> are collected in a
single <a>JSON object</a> and how the <a>blank node</a> representing
"Dave Longley" has been assigned the <a>blank node identifier</a>
<code>_:t0</code>.</p>
<p>To make it easier for humans to read or for certain applications to
process it, a flattened document can be compacted by passing a context. Using
the same context as the input document, the flattened and compacted document
looks as follows:</p>
<pre class="example" data-transform="updateExample"
title="Flattened and compacted sample document">
<!--
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"knows": "http://xmlns.com/foaf/0.1/knows"
},
"@graph": [
{
"@id": "_:t0",
"name": "Dave Longley"
},
{
"@id": "http://me.markus-lanthaler.com/",
"name": "Markus Lanthaler",
"knows": { "@id": "_:t0" }
}
]
}
-->
</pre>
<p>Please note that the result of flattening and <a>compacting</a> a document
is always a <a>JSON object</a> which contains an <code>@graph</code>
member that represents the <a>default graph</a>.</p>
</section> <!-- end of Flattening -->
<section class="informative">
<h2>RDF Serialization/Deserialization</h2>
<p>JSON-LD can be used to serialize RDF data as described in
[[RDF11-CONCEPTS]]. This ensures that data can be round-tripped to and from
any RDF syntax without any loss in fidelity.</p>
<p>For example, assume the following RDF input serialized in Turtle [[TURTLE]]:</p>
<pre class="example" data-transform="updateExample"
title="Sample Turtle document">
<!--
<http://me.markus-lanthaler.com/> <http://xmlns.com/foaf/0.1/name> "Markus Lanthaler" .
<http://me.markus-lanthaler.com/> <http://xmlns.com/foaf/0.1/homepage> <http://www.markus-lanthaler.com/> .
-->
</pre>
<p>Using the <a href="#serialize-rdf-as-json-ld-algorithm">Serialize RDF as JSON-LD algorithm</a>
a developer could transform this document into expanded JSON-LD:</p>
<pre class="example" data-transform="updateExample"
title="Sample Turtle document converted to JSON-LD">
<!--
[
{
"@id": "http://me.markus-lanthaler.com/",
"http://xmlns.com/foaf/0.1/name": [
{ "@value": "Markus Lanthaler" }
],
"http://xmlns.com/foaf/0.1/homepage": [
{ "@id": "http://www.markus-lanthaler.com/" }
]
}
]
-->
</pre>
<p>Note that the output above could easily be compacted using the technique outlined
in the previous section. It is also possible to deserialize the JSON-LD document back
to RDF using the <a href="#deserialize-json-ld-to-rdf-algorithm">Deserialize JSON-LD to RDF algorithm</a>.</p>
</section> <!-- end of RDF Serialization/Deserialization -->
</section> <!-- end of Features section -->
<section id="conformance">
<p>There are two classes of products that can claim conformance to this
specification: <a>JSON-LD Processors</a>,
and <a>RDF Serializers/Deserializers</a>.</p>
<p>A conforming <a>JSON-LD Processor</a> is a system which can perform the
<a href="#expansion-algorithm">Expansion</a>, <a href="#compaction-algorithm">Compaction</a>,
and <a href="#flattening-algorithm">Flattening</a> operations
<span class="changed">in a manner consistent with
the algorithms defined in this specification</span>.</p>
<p><a>JSON-LD Processors</a> MUST NOT
attempt to correct malformed <a>IRIs</a> or language tags;
however, they MAY issue validation warnings. IRIs are not modified other
than conversion between <a data-lt="relative IRI">relative</a> and
<a>absolute IRIs</a>.</p>
<p>A conforming <dfn data-lt="RDF Serializers/Deserializers">RDF Serializer/Deserializer</dfn> is a system that can
<a href="#deserialize-json-ld-to-rdf-algorithm">deserialize JSON-LD to RDF</a> and
<a href="#serialize-rdf-as-json-ld-algorithm">serialize RDF as JSON-LD</a> as
defined in this specification.</p>
<p>The algorithms in this specification are generally written with more concern for clarity
than efficiency. Thus, <a>JSON-LD Processors</a> may
implement the algorithms given in this specification in any way desired,
so long as the end result is indistinguishable from the result that would
be obtained by the specification's algorithms.</p>
<p class="note">Implementers can partially check their level of conformance to
this specification by successfully passing the test cases of the JSON-LD test
suite [[JSON-LD-TESTS]]. Note, however, that passing all the tests in the test
suite does not imply complete conformance to this specification. It only implies
that the implementation conforms to aspects tested by the test suite.</p>
</section> <!-- end of Conformance section -->
<section>
<h1>Context Processing Algorithms</h1>
<section>
<h2>Context Processing Algorithm</h2>
<p>When processing a JSON-LD data structure, each processing rule is applied
using information provided by the <a>active context</a>. This
section describes how to produce an <a>active context</a>.</p>
<p>The <a>active context</a> contains the active
<a>term definitions</a> which specify how
properties and values have to be interpreted as well as the current <a>base IRI</a>,
the <a>vocabulary mapping</a> and the <a>default language</a>. Each
<a>term definition</a> consists of an <dfn data-lt="IRI mappings">IRI mapping</dfn>, a boolean
flag <dfn data-lt="reverse properties">reverse property</dfn>, an optional <dfn data-lt="type mappings">type mapping</dfn>
or <dfn data-lt="language mappings">language mapping</dfn>,
<span class="changed">an optional context</span>,
and an optional <dfn data-lt="container mappings">container mapping</dfn>.
A <a>term definition</a> can not only be used to map a <a>term</a>
to an IRI, but also to map a <a>term</a> to a <a>keyword</a>,
in which case it is referred to as a <dfn data-lt="keyword aliases">keyword alias</dfn>.</p>
<p>When processing, the <a>active context</a> is initialized
without any <a>term definitions</a>,
<a>vocabulary mapping</a>, or <a>default language</a>.
If a <a>local context</a> is encountered during processing, a new
<a>active context</a> is created by cloning the existing
<a>active context</a>. Then the information from the
<a>local context</a> is merged into the new <a>active context</a>.
Given that <a>local contexts</a> may contain
references to remote contexts, this includes their retrieval.</p>
<section class="informative">
<h3>Overview</h3>
<p>First we prepare a new <a>active context</a> <em>result</em> by cloning
the current <a>active context</a>. Then we normalize the form of the passed
<a>local context</a> to an <a>array</a>.
<a>Local contexts</a> may be in the form of a
<a>JSON object</a>, a <a>string</a>, or an <a>array</a> containing
a combination of the two. Finally we process each <a>context</a> contained
in the <a>local context</a> <a>array</a> as follows.</p>
<p>If <a>context</a> is a <a>string</a>, it represents a reference to
a remote context. We dereference the remote context and replace <a>context</a>
with the value of the <code>@context</code> key of the top-level object in the
retrieved JSON-LD document. If there's no such key, an
<a data-link-for="JsonLdErrorCode">invalid remote context</a>
has been detected. Otherwise, we process <a>context</a> by recursively using
this algorithm ensuring that there is no cyclical reference.</p>
<p>If <a>context</a> is a <a>JSON object</a>, we first update the
<a>base IRI</a>, the <a>vocabulary mapping</a>, and the
<a>default language</a> by processing three specific keywords:
<code>@base</code>, <code>@vocab</code>, and <code>@language</code>.
These are handled before any other keys in the <a>local context</a> because
they affect how the other keys are processed. Please note that <code>@base</code> is
ignored when processing remote contexts.</p>
<p>Then, for every other key in <a>local context</a>, we update
the <a>term definition</a> in <em>result</em>. Since
<a>term definitions</a> in a <a>local context</a>
may themselves contain <a>terms</a> or
<a>compact IRIs</a>, we may need to recurse.
When doing so, we must ensure that there is no cyclical dependency,
which is an error. After we have processed any
<a>term definition</a> dependencies,
we update the current <a>term definition</a>,
which may be a <a>keyword alias</a>.</p>
<p>Finally, we return <em>result</em> as the new <a>active context</a>.</p>
</section>
<section>
<h3>Algorithm</h3>
<p>This algorithm specifies how a new <a>active context</a> is updated
with a <a>local context</a>. The algorithm takes three input variables:
an <a>active context</a>, a <a>local context</a>, and an <a>array</a>
<em>remote contexts</em> which is used to detect cyclical context inclusions.
If <em>remote contexts</em> is not passed, it is initialized to an empty
<a>array</a>.</p>
<ol class="algorithm">
<li>Initialize <em>result</em> to the result of cloning
<a>active context</a>.</li>
<li>If <a>local context</a> is not an <a>array</a>,
set it to an <a>array</a> containing only
<a>local context</a>.</li>
<li>
For each item <em>context</em> in <a>local context</a>:
<ol class="algorithm">
<li>If <em>context</em> is <code>null</code>, set <em>result</em> to a
newly-initialized <a>active context</a> and continue with the
next <em>context</em>. The <a>base IRI</a> of the
<a>active context</a> is set to the IRI of the currently being processed
document (which might be different from the currently being processed context),
if available; otherwise to <code>null</code>. If set, the
<a data-link-for="JsonLdOptions">base</a>
option of a JSON-LD API Implementation overrides the <code>base IRI</code>.</li>
<li>If <em>context</em> is a <a>string</a>,
<ol class="algorithm">
<li>Set <em>context</em> to the result of resolving <em>value</em> against
the base IRI which is established as specified in
<cite><a href="http://tools.ietf.org/html/rfc3986#section-5.1">section 5.1 Establishing a Base URI</a></cite>
of [[!RFC3986]]. Only the basic algorithm in
<cite><a href="http://tools.ietf.org/html/rfc3986#section-5.2">section 5.2</a></cite>
of [[!RFC3986]] is used; neither
<cite><a href="http://tools.ietf.org/html/rfc3986#section-6.2.2">Syntax-Based Normalization</a></cite> nor
<cite><a href="http://tools.ietf.org/html/rfc3986#section-6.2.3">Scheme-Based Normalization</a></cite>
are performed. Characters additionally allowed in IRI
references are treated in the same way that unreserved
characters are treated in URI references, per
<cite><a href="http://tools.ietf.org/html/rfc3987#section-6.5">section 6.5</a></cite>
of [[!RFC3987]].</li>
<li>If <em>context</em> is in the <em>remote contexts</em> array, a
<a data-link-for="JsonLdErrorCode">recursive context inclusion</a>
error has been detected and processing is aborted;
otherwise, add <em>context</em> to <em>remote contexts</em>.</li>
<li>Dereference <em>context</em>. If <em>context</em> cannot be dereferenced, a
<a data-link-for="JsonLdErrorCode">loading remote context failed</a>
error has been detected and processing is aborted. If the dereferenced document has no
top-level <a>JSON object</a> with an <code>@context</code> member, an
<a data-link-for="JsonLdErrorCode">invalid remote context</a>
has been detected and processing is aborted; otherwise,
set <em>context</em> to the value of that member.</li>
<li>Set <em>result</em> to the result of recursively calling this algorithm,
passing <em>result</em> for <a>active context</a>,
<em>context</em> for <a>local context</a>, and <em>remote contexts</em>.</li>
<li>Continue with the next <em>context</em>.</li>
</ol>
</li>
<li>If <em>context</em> is not a <a>JSON object</a>, an
<a data-link-for="JsonLdErrorCode">invalid local context</a>
error has been detected and processing is aborted.</li>
<li>If <em>context</em> has an <code>@base</code> key and <em>remote contexts</em> is empty, i.e., the currently
being processed context is not a remote context:
<ol class="algorithm">
<li>Initialize <em>value</em> to the value associated with the
<code>@base</code> key.</li>
<li>If <em>value</em> is <code>null</code>, remove the
<a>base IRI</a> of <em>result</em>.</li>
<li>Otherwise, if <em>value</em> is an <a>absolute IRI</a>,
the <a>base IRI</a> of <em>result</em> is set to <em>value</em>.</li>
<li>Otherwise, if <em>value</em> is a <a>relative IRI</a> and
the <a>base IRI</a> of <em>result</em> is not <code>null</code>,
set the <a>base IRI</a> of <em>result</em> to the result of
resolving <em>value</em> against the current <a>base IRI</a>
of <em>result</em>.</li>
<li>Otherwise, an
<a data-link-for="JsonLdErrorCode">invalid base IRI</a>
error has been detected and processing is aborted.</li>
</ol>
</li>
<li>If <em>context</em> has an <code>@vocab</code> key:
<ol class="algorithm">
<li>Initialize <em>value</em> to the value associated with the
<code>@vocab</code> key.</li>
<li>If <em>value</em> is <a>null</a>, remove
any <a>vocabulary mapping</a> from <em>result</em>.</li>
<li>Otherwise, if <em>value</em> is an <a>absolute IRI</a>
or <a>blank node identifier</a>, the <a>vocabulary mapping</a>
of <em>result</em> is set to <em>value</em>. If it is not an
<a>absolute IRI</a> or <a>blank node identifier</a>, an
<a data-link-for="JsonLdErrorCode">invalid vocab mapping</a>
error has been detected and processing is aborted.</li>
</ol>
</li>
<li>If <em>context</em> has an <code>@language</code> key:
<ol class="algorithm">
<li>Initialize <em>value</em> to the value associated with the
<code>@language</code> key.</li>
<li>If <em>value</em> is <code>null</code>, remove
any <a>default language</a> from <em>result</em>.</li>
<li>Otherwise, if <em>value</em> is <a>string</a>, the
<a>default language</a> of <em>result</em> is set to
lowercased <em>value</em>. If it is not a <a>string</a>, an
<a data-link-for="JsonLdErrorCode">invalid default language</a>
error has been detected and processing is aborted.</li>
</ol>
</li>
<li>Create a <a>JSON object</a> <em>defined</em> to use to keep
track of whether or not a <a>term</a> has already been defined
or currently being defined during recursion.</li>
<li>For each <em>key</em>-<em>value</em> pair in <em>context</em> where
<em>key</em> is not <code>@base</code>, <code>@vocab</code>, or
<code>@language</code>, invoke the
<a href="#create-term-definition">Create Term Definition algorithm</a>,
passing <em>result</em> for <a>active context</a>,
<em>context</em> for <a>local context</a>, <em>key</em>,
and <em>defined</em>.</li>
</ol>
</li>
<li>Return <em>result</em>.</li>
</ol>
</section>
</section> <!-- end of Context Processing -->
<section>
<h2>Create Term Definition</h2>
<p>This algorithm is called from the
<a href="#context-processing-algorithm">Context Processing algorithm</a>
to create a <a>term definition</a> in the <a>active context</a>
for a <a>term</a> being processed in a <a>local context</a>.</p>
<section class="informative">
<h3>Overview</h3>
<p><a>term definitions</a> are created by
parsing the information in the given <a>local context</a> for the
given <a>term</a>. If the given <a>term</a> is a
<a>compact IRI</a>, it may omit an <a>IRI mapping</a> by
depending on its <a>prefix</a> having its own
<a>term definition</a>. If the <a>prefix</a> is
a key in the <a>local context</a>, then its <a>term definition</a>
must first be created, through recursion, before continuing. Because a
<a>term definition</a> can depend on other
<a>term definitions</a>, a mechanism must
be used to detect cyclical dependencies. The solution employed here
uses a map, <em>defined</em>, that keeps track of whether or not a
<a>term</a> has been defined or is currently in the process of
being defined. This map is checked before any recursion is attempted.</p>
<p>After all dependencies for a <a>term</a> have been defined, the rest of
the information in the <a>local context</a> for the given
<a>term</a> is taken into account, creating the appropriate
<a>IRI mapping</a>, <a>container mapping</a>, and
<a>type mapping</a> or <a>language mapping</a> for the
<a>term</a>.</p>
</section>
<section>
<h3>Algorithm</h3>
<p>The algorithm has four required inputs which are:
an <a>active context</a>, a <a>local context</a>,
a <em>term</em>, and a map <em>defined</em>.</p>
<ol class="algorithm">
<li>If <em>defined</em> contains the key <em>term</em> and the associated
value is <code>true</code> (indicating that the
<a>term definition</a> has already been created), return. Otherwise,
if the value is <code>false</code>, a
<a data-link-for="JsonLdErrorCode">cyclic IRI mapping</a>
error has been detected and processing is aborted.</li>
<li>Set the value associated with <em>defined</em>'s <em>term</em> key to
<code>false</code>. This indicates that the <a>term definition</a>
is now being created but is not yet complete.</li>
<li>Since <a>keywords</a> cannot be overridden,
<em>term</em> must not be a <a>keyword</a>. Otherwise, a
<a data-link-for="JsonLdErrorCode">keyword redefinition</a>
error has been detected and processing is aborted.</li>
<li>Remove any existing <a>term definition</a> for <em>term</em> in
<a>active context</a>.</li>
<li>Initialize <em>value</em> to a copy of the value associated with the key
<em>term</em> in <a>local context</a>.</li>
<li>If <em>value</em> is <code>null</code> or <em>value</em>
is a <a>JSON object</a> containing the key-value pair
<code>@id</code>-<code>null</code>, set the
<a>term definition</a> in <a>active context</a> to
<code>null</code>, set the value associated with <em>defined</em>'s
key <em>term</em> to <code>true</code>, and return.</li>
<li>Otherwise, if <em>value</em> is a <a>string</a>, convert it
to a <a>JSON object</a> consisting of a single member whose
key is <code>@id</code> and whose value is <em>value</em>.</li>
<li>Otherwise, <em>value</em> must be a <a>JSON object</a>, if not, an
<a data-link-for="JsonLdErrorCode">invalid term definition</a>
error has been detected and processing is aborted.</li>
<li>Create a new <a>term definition</a>, <em>definition</em>.</li>
<li>If <em>value</em> contains the key <code>@type</code>:
<ol class="algorithm">
<li>Initialize <em>type</em> to the value associated with the
<code>@type</code> key, which must be a <a>string</a>. Otherwise, an
<a data-link-for="JsonLdErrorCode">invalid type mapping</a>
error has been detected and processing is aborted.</li>
<li>Set <em>type</em> to the result of using the
<a href="#iri-expansion">IRI Expansion algorithm</a>, passing
<a>active context</a>, <em>type</em> for <em>value</em>,
<code>true</code> for <em>vocab</em>,
<code>false</code> for <em>document relative</em>,
<a>local context</a>, and <em>defined</em>. If the expanded <em>type</em> is
neither <code>@id</code>, nor <code>@vocab</code>, nor an <a>absolute IRI</a>, an
<a data-link-for="JsonLdErrorCode">invalid type mapping</a>
error has been detected and processing is aborted.</li>
<li>Set the <a>type mapping</a> for <em>definition</em> to <em>type</em>.</li>
</ol>
</li>
<li>If <em>value</em> contains the key <code>@reverse</code>:
<ol class="algorithm">
<li>If <em>value</em> contains <code>@id</code> or <code>@nest</code>, members, an
<a data-link-for="JsonLdErrorCode">invalid reverse property</a>
error has been detected and processing is aborted.</li>
<li>If the value associated with the <code>@reverse</code> key
is not a <a>string</a>, an
<a data-link-for="JsonLdErrorCode">invalid IRI mapping</a>
error has been detected and processing is aborted.</li>
<li>Otherwise, set the <a>IRI mapping</a> of <em>definition</em> to the
result of using the <a href="#iri-expansion">IRI Expansion algorithm</a>,
passing <a>active context</a>, the value associated with
the <code>@reverse</code> key for <em>value</em>, <code>true</code>
for <em>vocab</em>, <code>false</code> for <em>document relative</em>,
<a>local context</a>, and <em>defined</em>. If the result
is neither an <a>absolute IRI</a> nor a <a>blank node identifier</a>,
i.e., it contains no colon (<code>:</code>), an
<a data-link-for="JsonLdErrorCode">invalid IRI mapping</a>
error has been detected and processing is aborted.</li>
<li>If <em>value</em> contains an <code>@container</code> member,
set the <a>container mapping</a> of <em>definition</em>
to its value; if its value is neither <code>@set</code>, nor
<code>@index</code>, nor <code>null</code>, an
<a data-link-for="JsonLdErrorCode">invalid reverse property</a>
error has been detected (reverse properties only support set- and
index-containers) and processing is aborted.</li>
<li>Set the <a>reverse property</a> flag of <em>definition</em>
to <code>true</code>.</li>
<li>Set the <a>term definition</a> of <em>term</em> in
<a>active context</a> to <em>definition</em> and the
value associated with <em>defined</em>'s key <em>term</em> to
<code>true</code> and return.</li>
</ol>
</li>
<li>Set the <a>reverse property</a> flag of <em>definition</em>
to <code>false</code>.</li>
<li>If <em>value</em> contains the key <code>@id</code> and its value
does not equal <em>term</em>:
<ol class="algorithm">
<li>If the value associated with the <code>@id</code> key is not a <a>string</a>, an
<a data-link-for="JsonLdErrorCode">invalid IRI mapping</a>
error has been detected and processing is aborted.</li>
<li>Otherwise, set the <a>IRI mapping</a> of <em>definition</em> to the
result of using the <a href="#iri-expansion">IRI Expansion algorithm</a>, passing
<a>active context</a>, the value associated with the <code>@id</code> key for
<em>value</em>, <code>true</code> for <em>vocab</em>,
<code>false</code> for <em>document relative</em>,
<a>local context</a>, and <em>defined</em>. If the resulting
<a>IRI mapping</a> is neither a <a>keyword</a>, nor an
<a>absolute IRI</a>, nor a <a>blank node identifier</a>, an
<a data-link-for="JsonLdErrorCode">invalid IRI mapping</a>
error has been detected and processing is aborted; if it equals <code>@context</code>, an
<a data-link-for="JsonLdErrorCode">invalid keyword alias</a>
error has been detected and processing is aborted.</li>
</ol>
</li>
<li>
Otherwise if the <em>term</em> contains a colon (<code>:</code>):
<ol class="algorithm">
<li>If <em>term</em> is a <a>compact IRI</a> with a
<a>prefix</a> that is a key in <a>local context</a>
a dependency has been found. Use this algorithm recursively passing
<a>active context</a>, <a>local context</a>, the
<a>prefix</a> as <em>term</em>, and <em>defined</em>.</li>
<li>If <em>term</em>'s <a>prefix</a> has a
<a>term definition</a> in <a>active context</a>, set
the <a>IRI mapping</a> of <em>definition</em> to the result of
concatenating the value associated with the <a>prefix</a>'s
<a>IRI mapping</a> and the <em>term</em>'s <em>suffix</em>.</li>
<li>Otherwise, <em>term</em> is an <a>absolute IRI</a> or
<a>blank node identifier</a>. Set the <a>IRI mapping</a>
of <em>definition</em> to <em>term</em>.</li>
</ol>
</li>
<li>Otherwise, if <a>active context</a> has a
<a>vocabulary mapping</a>, the <a>IRI mapping</a>
of <em>definition</em> is set to the result of concatenating the value
associated with the <a>vocabulary mapping</a> and <em>term</em>.
If it does not have a <a>vocabulary mapping</a>, an
<a data-link-for="JsonLdErrorCode">invalid IRI mapping</a>
error been detected and processing is aborted.</li>
<li>If <em>value</em> contains the key <code>@container</code>:
<ol class="algorithm">
<li>Initialize <em>container</em> to the value associated with the
<code>@container</code> key, which must be either
<code>@list</code>, <code>@set</code>, <code>@index</code>,
or <code>@language</code>. Otherwise, an
<a data-link-for="JsonLdErrorCode">invalid container mapping</a>
has been detected and processing is aborted.</li>
<li>Set the <a>container mapping</a> of <em>definition</em> to
<em>container</em>.</li>
</ol>
</li>
<li class="changed">If <em>value</em> contains the key <code>@context</code>:
<ol class="algorithm">
<li>Initialize <em>context</em> to the value associated with the
<code>@context</code> key, which is treated as a <a>local context</a>.</li>