@@ -18,7 +18,7 @@ Install with `gem install json-ld`
1818
1919### Expand a Document
2020
21- input = {
21+ input = JSON.parse %( {
2222 "@context": {
2323 "name": "http://xmlns.com/foaf/0.1/name",
2424 "homepage": "http://xmlns.com/foaf/0.1/homepage",
@@ -27,7 +27,7 @@ Install with `gem install json-ld`
2727 "name": "Manu Sporny",
2828 "homepage": "http://manu.sporny.org/",
2929 "avatar": "http://twitter.com/account/profile_image/manusporny"
30- }
30+ })
3131 JSON::LD::API.expand(input) =>
3232
3333 [{
@@ -38,26 +38,26 @@ Install with `gem install json-ld`
3838
3939### Compact a Document
4040
41- input = [{
41+ input = JSON.parse %( [{
4242 "http://xmlns.com/foaf/0.1/name": ["Manu Sporny"],
43- "http://xmlns.com/foaf/0.1/homepage": [" http://manu.sporny.org/"],
44- "http://xmlns.com/foaf/0.1/avatar": [" http://twitter.com/account/profile_image/manusporny"]
45- }]
43+ "http://xmlns.com/foaf/0.1/homepage": [{"@id": " http://manu.sporny.org/"} ],
44+ "http://xmlns.com/foaf/0.1/avatar": [{"@id": " http://twitter.com/account/profile_image/manusporny"} ]
45+ }])
4646
47- context = {
47+ context = JSON.parse(%( {
4848 "@context": {
4949 "name": "http://xmlns.com/foaf/0.1/name",
50- "homepage": " http://xmlns.com/foaf/0.1/homepage",
51- "avatar": " http://xmlns.com/foaf/0.1/avatar"
50+ "homepage": {"@id": " http://xmlns.com/foaf/0.1/homepage", "@type": "@id"} ,
51+ "avatar": {"@id": " http://xmlns.com/foaf/0.1/avatar", "@type": "@id"}
5252 }
53- }
53+ }))['@context']
5454
5555 JSON::LD::API.compact(input, context) =>
5656 {
5757 "@context": {
58- "avatar ": "http://xmlns.com/foaf/0.1/avatar ",
59- "homepage": " http://xmlns.com/foaf/0.1/homepage",
60- "name ": "http://xmlns.com/foaf/0.1/name"
58+ "name ": "http://xmlns.com/foaf/0.1/name ",
59+ "homepage": {"@id": " http://xmlns.com/foaf/0.1/homepage", "@type": "@id"} ,
60+ "avatar": {"@id ": "http://xmlns.com/foaf/0.1/avatar", "@type": "@id"}
6161 },
6262 "avatar": "http://twitter.com/account/profile_image/manusporny",
6363 "homepage": "http://manu.sporny.org/",
@@ -66,7 +66,7 @@ Install with `gem install json-ld`
6666
6767### Frame a Document
6868
69- input = {
69+ input = JSON.parse %( {
7070 "@context": {
7171 "Book": "http://example.org/vocab#Book",
7272 "Chapter": "http://example.org/vocab#Chapter",
@@ -95,9 +95,9 @@ Install with `gem install json-ld`
9595 "description": "An introductory chapter on The Republic.",
9696 "title": "The Introduction"
9797 }]
98- }
98+ })
9999
100- frame = {
100+ frame = JSON.parse %( {
101101 "@context": {
102102 "Book": "http://example.org/vocab#Book",
103103 "Chapter": "http://example.org/vocab#Chapter",
@@ -114,8 +114,9 @@ Install with `gem install json-ld`
114114 "@type": "Chapter"
115115 }
116116 }
117- }
118- JSON::LD.frame(input, frame) =>
117+ })
118+
119+ JSON::LD::API.frame(input, frame) =>
119120 {
120121 "@context": {
121122 "Book": "http://example.org/vocab#Book",
@@ -148,7 +149,7 @@ Install with `gem install json-ld`
148149
149150### Turn JSON-LD into RDF (Turtle)
150151
151- input = {
152+ input = JSON.parse %( {
152153 "@context": {
153154 "": "http://manu.sporny.org/",
154155 "foaf": "http://xmlns.com/foaf/0.1/"
@@ -157,9 +158,12 @@ Install with `gem install json-ld`
157158 "@type": "foaf:Person",
158159 "foaf:name": "Joe Bob",
159160 "foaf:nick": { "@list": [ "joe", "bob", "jaybe" ] }
160- }
161+ })
161162
162- JSON::LD::API.toRDF(input) =>
163+ graph = RDF::Graph.new << JSON::LD::API.toRDF(input)
164+
165+ require 'rdf/turtle'
166+ graph.dump(:ttl, :prefixes => {:foaf => "http://xmlns.com/foaf/0.1/"})
163167 @prefix foaf: <http://xmlns.com/foaf/0.1/> .
164168
165169 <http://example.org/people#joebob> a foaf:Person;
@@ -168,43 +172,50 @@ Install with `gem install json-ld`
168172
169173### Turn RDF into JSON-LD
170174
171- input =
172- @prefix foaf: <http://xmlns.com/foaf/0.1/> .
175+ require 'rdf/turtle'
176+ input = RDF::Graph.new << RDF::Turtle::Reader.new(%(
177+ @prefix foaf: <http://xmlns.com/foaf/0.1/> .
173178
174- <http://manu.sporny.org/#me> a foaf:Person;
175- foaf:knows [ a foaf:Person;
176- foaf:name "Gregg Kellogg"];
177- foaf:name "Manu Sporny" .
179+ <http://manu.sporny.org/#me> a foaf:Person;
180+ foaf:knows [ a foaf:Person;
181+ foaf:name "Gregg Kellogg"];
182+ foaf:name "Manu Sporny" .
183+ ))
178184
179- context =
180- {
181- "@context": {
182- "": "http://manu.sporny.org/",
183- "foaf": "http://xmlns.com/foaf/0.1/"
184- }
185- }
186-
187- JSON::LD::API::fromRDF(input, context) =>
188- {
185+ context = JSON.parse %({
189186 "@context": {
190187 "": "http://manu.sporny.org/",
191188 "foaf": "http://xmlns.com/foaf/0.1/"
192- },
193- "@id": ":#me",
194- "@type": "foaf:Person",
195- "foaf:name": "Manu Sporny",
196- "foaf:knows": {
197- "@type": "foaf:Person",
198- "foaf:name": "Gregg Kellogg"
199189 }
200- }
190+ })
191+
192+ compacted = nil
193+ JSON::LD::API::fromRDF(input) do |expanded|
194+ compacted = JSON::LD::API.compact(expanded, context['@context'])
195+ end
196+ compacted =>
197+ [
198+ {
199+ "@id": "_:g70265766605380",
200+ "@type": ["http://xmlns.com/foaf/0.1/Person"],
201+ "http://xmlns.com/foaf/0.1/name": [{"@value": "Gregg Kellogg"}]
202+ },
203+ {
204+ "@id": "http://manu.sporny.org/#me",
205+ "@type": ["http://xmlns.com/foaf/0.1/Person"],
206+ "http://xmlns.com/foaf/0.1/knows": [{"@id": "_:g70265766605380"}],
207+ "http://xmlns.com/foaf/0.1/name": [{"@value": "Manu Sporny"}]
208+ }
209+ ]
201210
202211## RDF Reader and Writer
203212{JSON::LD} also acts as a normal RDF reader and writer, using the standard RDF.rb reader/writer interfaces:
204213
205214 graph = RDF::Graph.load("etc/doap.jsonld", :format => :jsonld)
206215 graph.dump(:jsonld, :standard_prefixes => true)
207216
217+ ` RDF::GRAPH#dump ` can also take a ` :context ` option to use a separately defined context
218+
208219## Documentation
209220Full documentation available on [ RubyDoc] ( http://rubydoc.info/gems/json-ld/file/README.md )
210221
0 commit comments