json-ld 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +56 -45
- data/VERSION +1 -1
- data/lib/json/ld/to_rdf.rb +5 -5
- data/spec/suite_helper.rb +41 -0
- data/spec/suite_to_rdf_spec.rb +1 -49
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YTQ0NzdhNmE4YWFlYzBjYzVkOWVlOGJjNGJjNDdiY2ZmN2RhYzdhMg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NWNkMTAxNWU0NGQxOGQ5NjlmMTFkZmFiZDY3MWRiZDFkMWE2YWMwNQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZGExMzc0OThkODZlNDJiOWI2ZDRmOGU3Y2RkNTY5YWVjNzcyZmM1N2M3NzY0
|
10
|
+
OGM5YzcyYzM5NzUxMTNiZDE1NDVhYmZhMzNhN2ZkYzkwZWY5YTJkMDA2NjRm
|
11
|
+
ZjA0M2ViODM0YTk0MTQzMDk2MmE2MTg2NzQ1Y2MyNzc2N2Q3NjA=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MzljNjBiZDdlYWZjZjM4YmI5MmZhZDQ0ZTI3N2EyYjU2MWIzNjBlZTk5ZjI1
|
14
|
+
YjM2YzkyMmEyNDU2NmEyNTMwNzE3NGE4YTczMTMzYWE2MDA3MzI5ZDhkNWEw
|
15
|
+
Y2MwMjllM2FjNTRiYTRhMzRlMGVjYWIwYzg4Zjc0NzJiNGMxZmU=
|
data/README.md
CHANGED
@@ -18,7 +18,7 @@ Install with `gem install json-ld`
|
|
18
18
|
|
19
19
|
### Expand a Document
|
20
20
|
|
21
|
-
input = {
|
21
|
+
input = JSON.parse %({
|
22
22
|
"@context": {
|
23
23
|
"name": "http://xmlns.com/foaf/0.1/name",
|
24
24
|
"homepage": "http://xmlns.com/foaf/0.1/homepage",
|
@@ -27,7 +27,7 @@ Install with `gem install json-ld`
|
|
27
27
|
"name": "Manu Sporny",
|
28
28
|
"homepage": "http://manu.sporny.org/",
|
29
29
|
"avatar": "http://twitter.com/account/profile_image/manusporny"
|
30
|
-
}
|
30
|
+
})
|
31
31
|
JSON::LD::API.expand(input) =>
|
32
32
|
|
33
33
|
[{
|
@@ -38,26 +38,26 @@ Install with `gem install json-ld`
|
|
38
38
|
|
39
39
|
### Compact a Document
|
40
40
|
|
41
|
-
input = [{
|
41
|
+
input = JSON.parse %([{
|
42
42
|
"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
|
+
}])
|
46
46
|
|
47
|
-
context = {
|
47
|
+
context = JSON.parse(%({
|
48
48
|
"@context": {
|
49
49
|
"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"}
|
52
52
|
}
|
53
|
-
}
|
53
|
+
}))['@context']
|
54
54
|
|
55
55
|
JSON::LD::API.compact(input, context) =>
|
56
56
|
{
|
57
57
|
"@context": {
|
58
|
-
|
59
|
-
|
60
|
-
|
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"}
|
61
61
|
},
|
62
62
|
"avatar": "http://twitter.com/account/profile_image/manusporny",
|
63
63
|
"homepage": "http://manu.sporny.org/",
|
@@ -66,7 +66,7 @@ Install with `gem install json-ld`
|
|
66
66
|
|
67
67
|
### Frame a Document
|
68
68
|
|
69
|
-
input = {
|
69
|
+
input = JSON.parse %({
|
70
70
|
"@context": {
|
71
71
|
"Book": "http://example.org/vocab#Book",
|
72
72
|
"Chapter": "http://example.org/vocab#Chapter",
|
@@ -95,9 +95,9 @@ Install with `gem install json-ld`
|
|
95
95
|
"description": "An introductory chapter on The Republic.",
|
96
96
|
"title": "The Introduction"
|
97
97
|
}]
|
98
|
-
}
|
98
|
+
})
|
99
99
|
|
100
|
-
frame = {
|
100
|
+
frame = JSON.parse %({
|
101
101
|
"@context": {
|
102
102
|
"Book": "http://example.org/vocab#Book",
|
103
103
|
"Chapter": "http://example.org/vocab#Chapter",
|
@@ -114,8 +114,9 @@ Install with `gem install json-ld`
|
|
114
114
|
"@type": "Chapter"
|
115
115
|
}
|
116
116
|
}
|
117
|
-
}
|
118
|
-
|
117
|
+
})
|
118
|
+
|
119
|
+
JSON::LD::API.frame(input, frame) =>
|
119
120
|
{
|
120
121
|
"@context": {
|
121
122
|
"Book": "http://example.org/vocab#Book",
|
@@ -148,7 +149,7 @@ Install with `gem install json-ld`
|
|
148
149
|
|
149
150
|
### Turn JSON-LD into RDF (Turtle)
|
150
151
|
|
151
|
-
input = {
|
152
|
+
input = JSON.parse %({
|
152
153
|
"@context": {
|
153
154
|
"": "http://manu.sporny.org/",
|
154
155
|
"foaf": "http://xmlns.com/foaf/0.1/"
|
@@ -157,9 +158,12 @@ Install with `gem install json-ld`
|
|
157
158
|
"@type": "foaf:Person",
|
158
159
|
"foaf:name": "Joe Bob",
|
159
160
|
"foaf:nick": { "@list": [ "joe", "bob", "jaybe" ] }
|
160
|
-
}
|
161
|
+
})
|
161
162
|
|
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/"})
|
163
167
|
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
|
164
168
|
|
165
169
|
<http://example.org/people#joebob> a foaf:Person;
|
@@ -168,36 +172,41 @@ Install with `gem install json-ld`
|
|
168
172
|
|
169
173
|
### Turn RDF into JSON-LD
|
170
174
|
|
171
|
-
|
172
|
-
|
175
|
+
require 'rdf/turtle'
|
176
|
+
input = RDF::Graph.new << RDF::Turtle::Reader.new(%(
|
177
|
+
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
|
173
178
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
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
|
+
))
|
178
184
|
|
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 %({
|
189
186
|
"@context": {
|
190
187
|
"": "http://manu.sporny.org/",
|
191
188
|
"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"
|
199
189
|
}
|
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
|
+
]
|
201
210
|
|
202
211
|
## RDF Reader and Writer
|
203
212
|
{JSON::LD} also acts as a normal RDF reader and writer, using the standard RDF.rb reader/writer interfaces:
|
@@ -205,6 +214,8 @@ Install with `gem install json-ld`
|
|
205
214
|
graph = RDF::Graph.load("etc/doap.jsonld", :format => :jsonld)
|
206
215
|
graph.dump(:jsonld, :standard_prefixes => true)
|
207
216
|
|
217
|
+
`RDF::GRAPH#dump` can also take a `:context` option to use a separately defined context
|
218
|
+
|
208
219
|
## Documentation
|
209
220
|
Full documentation available on [RubyDoc](http://rubydoc.info/gems/json-ld/file/README.md)
|
210
221
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.5
|
data/lib/json/ld/to_rdf.rb
CHANGED
@@ -20,8 +20,8 @@ module JSON::LD
|
|
20
20
|
# For each id-node in active_graph
|
21
21
|
active_graph.each do |id, node|
|
22
22
|
# Initialize subject as the IRI or BNode representation of id
|
23
|
-
subject = as_resource(id
|
24
|
-
debug("graph_to_rdf") {"subject: #{subject.to_ntriples}"}
|
23
|
+
subject = as_resource(id)
|
24
|
+
debug("graph_to_rdf") {"subject: #{subject.to_ntriples} (id: #{id})"}
|
25
25
|
|
26
26
|
# For each property-values in node
|
27
27
|
node.each do |property, values|
|
@@ -29,7 +29,7 @@ module JSON::LD
|
|
29
29
|
when '@type'
|
30
30
|
# If property is @type, construct triple as an RDF Triple composed of id, rdf:type, and object from values where id and object are represented either as IRIs or Blank Nodes
|
31
31
|
results += values.map do |value|
|
32
|
-
object = as_resource(value
|
32
|
+
object = as_resource(value)
|
33
33
|
debug("graph_to_rdf") {"type: #{object.to_ntriples}"}
|
34
34
|
RDF::Statement.new(subject, RDF.type, object)
|
35
35
|
end
|
@@ -38,7 +38,7 @@ module JSON::LD
|
|
38
38
|
else
|
39
39
|
# Otherwise, property is an IRI or Blank Node identifier
|
40
40
|
# Initialize predicate from property as an IRI or Blank node
|
41
|
-
predicate = as_resource(property
|
41
|
+
predicate = as_resource(property)
|
42
42
|
debug("graph_to_rdf") {"predicate: #{predicate.to_ntriples}"}
|
43
43
|
|
44
44
|
# For each item in values
|
@@ -104,7 +104,7 @@ module JSON::LD
|
|
104
104
|
# Otherwise, value must be a node definition containing only @id whos value is an IRI or Blank Node identifier
|
105
105
|
raise "Expected node reference, got #{item.inspect}" unless item.keys == %w(@id)
|
106
106
|
# Return value associated with @id as an IRI or Blank node
|
107
|
-
as_resource(item['@id']
|
107
|
+
as_resource(item['@id'])
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
data/spec/suite_helper.rb
CHANGED
@@ -60,6 +60,8 @@ end
|
|
60
60
|
module Fixtures
|
61
61
|
module SuiteTest
|
62
62
|
SUITE = RDF::URI("http://json-ld.org/test-suite/")
|
63
|
+
TEST_IRI_BASE = RDF::URI("http://example/").freeze
|
64
|
+
|
63
65
|
class Manifest < JSON::LD::Resource
|
64
66
|
def self.open(file)
|
65
67
|
#puts "open: #{file}"
|
@@ -109,6 +111,45 @@ module Fixtures
|
|
109
111
|
end
|
110
112
|
|
111
113
|
def trace; @debug.join("\n"); end
|
114
|
+
|
115
|
+
# Don't use NQuads writer so that we don't escape Unicode
|
116
|
+
def to_quad(thing)
|
117
|
+
case thing
|
118
|
+
when RDF::URI
|
119
|
+
TEST_IRI_BASE.join(thing).canonicalize.to_ntriples
|
120
|
+
when RDF::Node
|
121
|
+
escaped(thing)
|
122
|
+
when RDF::Literal::Double
|
123
|
+
thing.canonicalize.to_ntriples
|
124
|
+
when RDF::Literal
|
125
|
+
v = quoted(escaped(thing.value))
|
126
|
+
case thing.datatype
|
127
|
+
when nil, "http://www.w3.org/2001/XMLSchema#string", "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"
|
128
|
+
# Ignore these
|
129
|
+
else
|
130
|
+
v += "^^#{to_quad(thing.datatype)}"
|
131
|
+
end
|
132
|
+
v += "@#{thing.language}" if thing.language
|
133
|
+
v
|
134
|
+
when RDF::Statement
|
135
|
+
thing.to_quad.map {|r| to_quad(r)}.compact.join(" ") + " .\n"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
##
|
140
|
+
# @param [String] string
|
141
|
+
# @return [String]
|
142
|
+
def quoted(string)
|
143
|
+
"\"#{string}\""
|
144
|
+
end
|
145
|
+
|
146
|
+
##
|
147
|
+
# @param [String, #to_s] string
|
148
|
+
# @return [String]
|
149
|
+
def escaped(string)
|
150
|
+
string.to_s.gsub('\\', '\\\\').gsub("\t", '\\t').
|
151
|
+
gsub("\n", '\\n').gsub("\r", '\\r').gsub('"', '\\"')
|
152
|
+
end
|
112
153
|
end
|
113
154
|
end
|
114
155
|
end
|
data/spec/suite_to_rdf_spec.rb
CHANGED
@@ -15,7 +15,7 @@ describe JSON::LD do
|
|
15
15
|
:base => t.base,
|
16
16
|
:debug => t.debug
|
17
17
|
).map do |statement|
|
18
|
-
to_quad(statement)
|
18
|
+
t.to_quad(statement)
|
19
19
|
end
|
20
20
|
|
21
21
|
sorted_expected = t.expect.readlines.uniq.sort.join("")
|
@@ -31,52 +31,4 @@ describe JSON::LD do
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
34
|
-
|
35
|
-
# Don't use NQuads writer so that we don't escape Unicode
|
36
|
-
def to_quad(thing)
|
37
|
-
case thing
|
38
|
-
when RDF::URI
|
39
|
-
"<#{escaped(thing.to_s)}>"
|
40
|
-
when RDF::Node
|
41
|
-
escaped(thing.to_s)
|
42
|
-
when RDF::Literal::Double
|
43
|
-
case
|
44
|
-
when thing.object.nan?, thing.object.infinite?, thing.object.zero?
|
45
|
-
thing.canonicalize.to_ntriples
|
46
|
-
else
|
47
|
-
i, f, e = ('%.15E' % thing.object.to_f).split(/[\.E]/)
|
48
|
-
f.sub!(/0*$/, '') # remove any trailing zeroes
|
49
|
-
f = '0' if f.empty? # ...but there must be a digit to the right of the decimal point
|
50
|
-
e.sub!(/^\+?0+(\d)$/, '\1') # remove the optional leading '+' sign and any extra leading zeroes
|
51
|
-
%("#{i}.#{f}E#{e}"^^<http://www.w3.org/2001/XMLSchema#double>)
|
52
|
-
end
|
53
|
-
when RDF::Literal
|
54
|
-
v = quoted(escaped(thing.value))
|
55
|
-
case thing.datatype
|
56
|
-
when nil, "http://www.w3.org/2001/XMLSchema#string", "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"
|
57
|
-
# Ignore these
|
58
|
-
else
|
59
|
-
v += "^^<#{thing.datatype}>"
|
60
|
-
end
|
61
|
-
v += "@#{thing.language}" if thing.language
|
62
|
-
v
|
63
|
-
when RDF::Statement
|
64
|
-
thing.to_quad.map {|r| to_quad(r)}.compact.join(" ") + " .\n"
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
##
|
69
|
-
# @param [String] string
|
70
|
-
# @return [String]
|
71
|
-
def quoted(string)
|
72
|
-
"\"#{string}\""
|
73
|
-
end
|
74
|
-
|
75
|
-
##
|
76
|
-
# @param [String] string
|
77
|
-
# @return [String]
|
78
|
-
def escaped(string)
|
79
|
-
string.gsub('\\', '\\\\').gsub("\t", '\\t').
|
80
|
-
gsub("\n", '\\n').gsub("\r", '\\r').gsub('"', '\\"')
|
81
|
-
end
|
82
34
|
end unless ENV['CI']
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-ld
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gregg Kellogg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rdf
|