Update expansion algorithm and test suite to keep @graph if not only-property at top-level object
If @graph is the only property in the document's top level object, it MUST be dropped in expansion and compaction. In all other cases (which includes @graph at the document's top level object when there are other properties other than @context at the same level), @graph MUST NOT be dropped in expansion and compaction.
This closes #99.
--- a/spec/latest/json-ld-api/index.html Tue Apr 10 15:10:30 2012 -0700
+++ b/spec/latest/json-ld-api/index.html Wed Apr 11 21:46:48 2012 +0800
@@ -1179,15 +1179,13 @@
<p>The algorithm takes three input variables: an <tref>active context</tref>, an <tref>active property</tref>,
and an <em>element</em> to be expanded. To begin, the <tref>active context</tref> is set to the <tref>initial
context</tref>, <tref>active property</tref> is set to <tref>null</tref>, and <em>element</em> is set to the <tref>JSON-LD
- input</tref>. If the <tref>JSON-LD input</tref> is not in the form of an <tref>array</tref> it is wrapped in an array.</p>
-
+ input</tref>.</p>
<ol class="algorithm">
<li>If <em>element</em> is an <tref>array</tref>, process each entry in <em>element</em> recursively
using this algorithm, passing copies of the <tref>active context</tref> and <tref>active property</tref>.
If the expanded entry is null, drop it. If it's an array, merge it's entries with <em>element</em>'s entries
unless the <tref>active property</tref> equals <code>@list</code> or has a <code>@container</code> set to <code>@list</code>;
in that case throw an exception as lists of lists are not allowed.</li>
-
<li>Otherwise, if <em>element</em> is an object
<ol class="algorithm">
<li>If <em>element</em> has a <code>@context</code> property, update the <tref>active context</tref> according to
@@ -1232,16 +1230,19 @@
</li>
<li>Otherwise, if <em>element</em> has an <code>@type</code> property and it's value is not in the form of an
<tref>array</tref>, convert it to an <tref>array</tref>.</li>
- <li>If <em>element</em> has an <code>@set</code>, <code>@list</code>, or <code>@graph</code> property, it MUST be the only property.
- Set <em>element</em> to the value of <code>@set</code> or <code>@graph</code>; leave <code>@list</code> untouched.
- <p class="issue">Whether <code>@graph</code> should always be dropped or not is currently being discussed in <a href="https://github.com/json-ld/json-ld.org/issues/99">ISSUE-99</a></p>
- </li>
+ <li>If <em>element</em> has an <code>@set</code> or <code>@list</code> property, it MUST be the only property.
+ Set <em>element</em> to the value of <code>@set</code>; leave <code>@list</code> untouched.</li>
<li>If <em>element</em> has just a <code>@language</code> property, set <em>element</em> to <tref>null</tref>.</li>
</ol>
</li>
<li>Otherwise, expand <em>element</em> according to the <a href="#value-expansion">Value Expansion</a> rules,
passing copies of the <tref>active context</tref> and <tref>active property</tref>.</li>
</ol>
+
+<p>If, after the algorithm outlined above is run, the resulting <em>element</em> is an <tref>JSON object</tref> with just a <code>@graph</code>
+ property, <em>element</em> is set to the value of <code>@graph</code>'s value. Finally, if <em>element</em> is a <tref>JSON object</tref>,
+ it is wrapped into an <tref>array</tref>.</p>
+
</section>
</section>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test-suite/tests/expand-0020-in.jsonld Wed Apr 11 21:46:48 2012 +0800
@@ -0,0 +1,51 @@
+{
+ "@context": {
+ "authored": {
+ "@id": "http://example.org/vocab#authored",
+ "@type": "@id"
+ },
+ "contains": {
+ "@id": "http://example.org/vocab#contains",
+ "@type": "@id"
+ },
+ "contributor": "http://purl.org/dc/elements/1.1/contributor",
+ "description": "http://purl.org/dc/elements/1.1/description",
+ "name": "http://xmlns.com/foaf/0.1/name",
+ "title": {
+ "@id": "http://purl.org/dc/elements/1.1/title"
+ }
+ },
+ "@graph": [
+ {
+ "@id": "http://example.org/test#jane",
+ "name": "Jane",
+ "authored": {
+ "@graph": [
+ {
+ "@id": "http://example.org/test#chapter1",
+ "description": "Fun",
+ "title": "Chapter One"
+ },
+ {
+ "@id": "http://example.org/test#chapter2",
+ "description": "More fun",
+ "title": "Chapter Two"
+ }
+ ]
+ }
+ },
+ {
+ "@id": "http://example.org/test#john",
+ "name": "John"
+ },
+ {
+ "@id": "http://example.org/test#library",
+ "contains": {
+ "@id": "http://example.org/test#book",
+ "contains": "http://example.org/test#chapter",
+ "contributor": "Writer",
+ "title": "My Book"
+ }
+ }
+ ]
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test-suite/tests/expand-0020-out.jsonld Wed Apr 11 21:46:48 2012 +0800
@@ -0,0 +1,37 @@
+[
+ {
+ "@id": "http://example.org/test#jane",
+ "http://xmlns.com/foaf/0.1/name": [ "Jane" ],
+ "http://example.org/vocab#authored": [
+ {
+ "@graph": [
+ {
+ "@id": "http://example.org/test#chapter1",
+ "http://purl.org/dc/elements/1.1/description": [ "Fun" ],
+ "http://purl.org/dc/elements/1.1/title": [ "Chapter One" ]
+ },
+ {
+ "@id": "http://example.org/test#chapter2",
+ "http://purl.org/dc/elements/1.1/description": [ "More fun" ],
+ "http://purl.org/dc/elements/1.1/title": [ "Chapter Two" ]
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "@id": "http://example.org/test#john",
+ "http://xmlns.com/foaf/0.1/name": [ "John" ]
+ },
+ {
+ "@id": "http://example.org/test#library",
+ "http://example.org/vocab#contains": [
+ {
+ "@id": "http://example.org/test#book",
+ "http://example.org/vocab#contains": [ { "@id": "http://example.org/test#chapter" } ],
+ "http://purl.org/dc/elements/1.1/contributor": [ "Writer" ],
+ "http://purl.org/dc/elements/1.1/title": [ "My Book" ]
+ }
+ ]
+ }
+]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test-suite/tests/expand-0021-in.jsonld Wed Apr 11 21:46:48 2012 +0800
@@ -0,0 +1,52 @@
+{
+ "@context": {
+ "authored": {
+ "@id": "http://example.org/vocab#authored",
+ "@type": "@id"
+ },
+ "contains": {
+ "@id": "http://example.org/vocab#contains",
+ "@type": "@id"
+ },
+ "contributor": "http://purl.org/dc/elements/1.1/contributor",
+ "description": "http://purl.org/dc/elements/1.1/description",
+ "name": "http://xmlns.com/foaf/0.1/name",
+ "title": {
+ "@id": "http://purl.org/dc/elements/1.1/title"
+ }
+ },
+ "title": "My first graph",
+ "@graph": [
+ {
+ "@id": "http://example.org/test#jane",
+ "name": "Jane",
+ "authored": {
+ "@graph": [
+ {
+ "@id": "http://example.org/test#chapter1",
+ "description": "Fun",
+ "title": "Chapter One"
+ },
+ {
+ "@id": "http://example.org/test#chapter2",
+ "description": "More fun",
+ "title": "Chapter Two"
+ }
+ ]
+ }
+ },
+ {
+ "@id": "http://example.org/test#john",
+ "name": "John"
+ },
+ {
+ "@id": "http://example.org/test#library",
+ "contains": {
+ "@id": "http://example.org/test#book",
+ "contains": "http://example.org/test#chapter",
+ "contributor": "Writer",
+ "title": "My Book"
+ }
+ }
+ ]
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test-suite/tests/expand-0021-out.jsonld Wed Apr 11 21:46:48 2012 +0800
@@ -0,0 +1,42 @@
+[
+ {
+ "http://purl.org/dc/elements/1.1/title": [ "My first graph" ],
+ "@graph": [
+ {
+ "@id": "http://example.org/test#jane",
+ "http://xmlns.com/foaf/0.1/name": [ "Jane" ],
+ "http://example.org/vocab#authored": [
+ {
+ "@graph": [
+ {
+ "@id": "http://example.org/test#chapter1",
+ "http://purl.org/dc/elements/1.1/description": [ "Fun" ],
+ "http://purl.org/dc/elements/1.1/title": [ "Chapter One" ]
+ },
+ {
+ "@id": "http://example.org/test#chapter2",
+ "http://purl.org/dc/elements/1.1/description": [ "More fun" ],
+ "http://purl.org/dc/elements/1.1/title": [ "Chapter Two" ]
+ }
+ ]
+ }
+ ]
+ },
+ {
+ "@id": "http://example.org/test#john",
+ "http://xmlns.com/foaf/0.1/name": [ "John" ]
+ },
+ {
+ "@id": "http://example.org/test#library",
+ "http://example.org/vocab#contains": [
+ {
+ "@id": "http://example.org/test#book",
+ "http://example.org/vocab#contains": [ { "@id": "http://example.org/test#chapter" } ],
+ "http://purl.org/dc/elements/1.1/contributor": [ "Writer" ],
+ "http://purl.org/dc/elements/1.1/title": [ "My Book" ]
+ }
+ ]
+ }
+ ]
+ }
+]
--- a/test-suite/tests/expand-manifest.jsonld Tue Apr 10 15:10:30 2012 -0700
+++ b/test-suite/tests/expand-manifest.jsonld Wed Apr 11 21:46:48 2012 +0800
@@ -99,5 +99,15 @@
"name": "remove @value = null",
"input": "expand-0019-in.jsonld",
"expect": "expand-0019-out.jsonld"
+ }, {
+ "@type": ["test:TestCase", "jld:ExpandTest"],
+ "name": "do not remove @graph if not at top-level",
+ "input": "expand-0020-in.jsonld",
+ "expect": "expand-0020-out.jsonld"
+ }, {
+ "@type": ["test:TestCase", "jld:ExpandTest"],
+ "name": "do not remove @graph at top-level if not only property",
+ "input": "expand-0021-in.jsonld",
+ "expect": "expand-0021-out.jsonld"
}]
}