json-ld 1.1.7 → 1.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +9 -3
- data/VERSION +1 -1
- data/bin/jsonld +42 -23
- data/lib/json/ld.rb +6 -6
- data/lib/json/ld/api.rb +68 -93
- data/lib/json/ld/compact.rb +15 -15
- data/lib/json/ld/context.rb +47 -22
- data/lib/json/ld/expand.rb +7 -7
- data/lib/json/ld/extensions.rb +2 -2
- data/lib/json/ld/flatten.rb +1 -1
- data/lib/json/ld/format.rb +5 -5
- data/lib/json/ld/frame.rb +5 -5
- data/lib/json/ld/from_rdf.rb +2 -2
- data/lib/json/ld/reader.rb +0 -2
- data/lib/json/ld/resource.rb +4 -4
- data/lib/json/ld/streaming_writer.rb +123 -0
- data/lib/json/ld/to_rdf.rb +6 -6
- data/lib/json/ld/writer.rb +29 -6
- data/spec/api_spec.rb +57 -4
- data/spec/compact_spec.rb +92 -92
- data/spec/context_spec.rb +43 -23
- data/spec/expand_spec.rb +142 -142
- data/spec/flatten_spec.rb +17 -17
- data/spec/format_spec.rb +17 -17
- data/spec/frame_spec.rb +47 -47
- data/spec/from_rdf_spec.rb +25 -25
- data/spec/matchers.rb +9 -9
- data/spec/reader_spec.rb +4 -4
- data/spec/resource_spec.rb +1 -1
- data/spec/spec_helper.rb +14 -9
- data/spec/streaming_writer_spec.rb +142 -0
- data/spec/suite_helper.rb +29 -88
- data/spec/to_rdf_spec.rb +17 -17
- data/spec/writer_spec.rb +43 -20
- metadata +40 -15
@@ -0,0 +1,123 @@
|
|
1
|
+
module JSON::LD
|
2
|
+
##
|
3
|
+
# Streaming writer interface.
|
4
|
+
#
|
5
|
+
# Writes an array of statements serialized in expanded JSON-LD. No provision for turning rdf:first/rest into @list encodings.
|
6
|
+
# @author [Gregg Kellogg](http://greggkellogg.net/)
|
7
|
+
module StreamingWriter
|
8
|
+
##
|
9
|
+
# Write out array start, and note not to prepend node-separating ','
|
10
|
+
# @return [void] `self`
|
11
|
+
def stream_prologue
|
12
|
+
|
13
|
+
# If we were provided a context, or prefixes, use them to compact the output
|
14
|
+
@context = case @options[:context]
|
15
|
+
when nil then nil
|
16
|
+
when Context then @options[:context]
|
17
|
+
else Context.new.parse(@options[:context])
|
18
|
+
end
|
19
|
+
|
20
|
+
debug("prologue") {"context: #{context.inspect}"}
|
21
|
+
if context
|
22
|
+
@output.puts %({"@context": #{context.serialize['@context'].to_json}, "@graph": [)
|
23
|
+
else
|
24
|
+
@output.puts "["
|
25
|
+
end
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# Write a statement, creating a current node definition, if necessary.
|
31
|
+
#
|
32
|
+
# Once a new/first statement is seen, terminate the current node definition and compact if provided a context.
|
33
|
+
#
|
34
|
+
# Also expects all statements in the same context to be contained in a block including all subjects in a block (except for list elements)
|
35
|
+
#
|
36
|
+
# Note that if list elements are not received in order using the same subject and property, this may cause a bad serialization.
|
37
|
+
#
|
38
|
+
# @return [void] `self`
|
39
|
+
def stream_statement(statement)
|
40
|
+
debug("ss") {"state: #{@state.inspect}, stmt: #{statement}"}
|
41
|
+
if @current_graph != statement.context
|
42
|
+
end_graph
|
43
|
+
start_graph(statement.context)
|
44
|
+
end
|
45
|
+
|
46
|
+
# If we're writing a list
|
47
|
+
@current_node_def ||= {'@id' => statement.subject.to_s}
|
48
|
+
|
49
|
+
if statement.subject.to_s != @current_node_def['@id']
|
50
|
+
end_node
|
51
|
+
@current_node_def = {'@id' => statement.subject.to_s}
|
52
|
+
end
|
53
|
+
|
54
|
+
if statement.predicate == RDF.type
|
55
|
+
(@current_node_def['@type'] ||= []) << statement.object.to_s
|
56
|
+
else
|
57
|
+
pd = (@current_node_def[statement.predicate.to_s] ||= [])
|
58
|
+
|
59
|
+
pd << if statement.object.resource?
|
60
|
+
{'@id' => statement.object.to_s}
|
61
|
+
else
|
62
|
+
lit = {"@value" => statement.object.to_s}
|
63
|
+
lit["@type"] = statement.object.datatype.to_s if statement.object.has_datatype?
|
64
|
+
lit["@language"] = statement.object.language.to_s if statement.object.has_language?
|
65
|
+
lit
|
66
|
+
end
|
67
|
+
end
|
68
|
+
self
|
69
|
+
end
|
70
|
+
|
71
|
+
##
|
72
|
+
# Complete open statements
|
73
|
+
# @return [void] `self`
|
74
|
+
def stream_epilogue
|
75
|
+
debug("epilogue") {"state: #{@state.inspect}"}
|
76
|
+
end_graph
|
77
|
+
if context
|
78
|
+
@output.puts "\n]}"
|
79
|
+
else
|
80
|
+
@output.puts "\n]"
|
81
|
+
end
|
82
|
+
self
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def start_graph(resource)
|
88
|
+
debug("start_graph") {"state: #{@state.inspect}, resource: #{resource}"}
|
89
|
+
if resource
|
90
|
+
@output.puts(",") if [:wrote_node, :wrote_graph].include?(@state)
|
91
|
+
@output.puts %({"@id": "#{resource}", "@graph": [)
|
92
|
+
@state = :in_graph
|
93
|
+
end
|
94
|
+
@current_graph = resource
|
95
|
+
end
|
96
|
+
|
97
|
+
def end_graph
|
98
|
+
debug("end_graph") {"state: #{@state.inspect}, ctx: #{@current_graph}"}
|
99
|
+
end_node
|
100
|
+
if @current_graph
|
101
|
+
@output.write %(]})
|
102
|
+
@state = :wrote_graph
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def end_node
|
107
|
+
debug("end_node") {"state: #{@state.inspect}, node: #{@current_node_def.to_json}"}
|
108
|
+
@output.puts(",") if [:wrote_node, :wrote_graph].include?(@state)
|
109
|
+
if @current_node_def
|
110
|
+
node_def = if context
|
111
|
+
compacted = JSON::LD::API.compact(@current_node_def, context, rename_bnodes: false)
|
112
|
+
compacted.delete('@context')
|
113
|
+
compacted
|
114
|
+
else
|
115
|
+
@current_node_def
|
116
|
+
end
|
117
|
+
@output.write node_def.to_json
|
118
|
+
@state = :wrote_node
|
119
|
+
@current_node_def = nil
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
data/lib/json/ld/to_rdf.rb
CHANGED
@@ -18,7 +18,7 @@ module JSON::LD
|
|
18
18
|
active_graph.each do |id, node|
|
19
19
|
# Initialize subject as the IRI or BNode representation of id
|
20
20
|
subject = as_resource(id)
|
21
|
-
debug("graph_to_rdf") {"subject: #{subject.to_ntriples} (id: #{id})"}
|
21
|
+
debug("graph_to_rdf") {"subject: #{subject.to_ntriples rescue 'malformed rdf'} (id: #{id})"}
|
22
22
|
|
23
23
|
# For each property-values in node
|
24
24
|
node.each do |property, values|
|
@@ -27,7 +27,7 @@ module JSON::LD
|
|
27
27
|
# 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
|
28
28
|
values.each do |value|
|
29
29
|
object = as_resource(value)
|
30
|
-
debug("graph_to_rdf") {"type: #{object.to_ntriples}"}
|
30
|
+
debug("graph_to_rdf") {"type: #{object.to_ntriples rescue 'malformed rdf'}"}
|
31
31
|
yield RDF::Statement.new(subject, RDF.type, object)
|
32
32
|
end
|
33
33
|
when /^@/
|
@@ -36,7 +36,7 @@ module JSON::LD
|
|
36
36
|
# Otherwise, property is an IRI or Blank Node identifier
|
37
37
|
# Initialize predicate from property as an IRI or Blank node
|
38
38
|
predicate = as_resource(property)
|
39
|
-
debug("graph_to_rdf") {"predicate: #{predicate.to_ntriples}"}
|
39
|
+
debug("graph_to_rdf") {"predicate: #{predicate.to_ntriples rescue 'malformed rdf'}"}
|
40
40
|
|
41
41
|
# For each item in values
|
42
42
|
values.each do |item|
|
@@ -50,7 +50,7 @@ module JSON::LD
|
|
50
50
|
else
|
51
51
|
# Otherwise, item is a value object or a node definition. Generate object as the result of the Object Converstion algorithm passing item.
|
52
52
|
object = parse_object(item)
|
53
|
-
debug("graph_to_rdf") {"object: #{object.to_ntriples}"}
|
53
|
+
debug("graph_to_rdf") {"object: #{object.to_ntriples rescue 'malformed rdf'}"}
|
54
54
|
# Append a triple composed of subject, prediate, and literal to results.
|
55
55
|
yield RDF::Statement.new(subject, predicate, object)
|
56
56
|
end
|
@@ -77,7 +77,7 @@ module JSON::LD
|
|
77
77
|
datatype ||= RDF::XSD.boolean.to_s
|
78
78
|
when Float, Fixnum
|
79
79
|
# Otherwise, if value is a number, then set value to its canonical lexical form as defined in the section Data Round Tripping. If datatype is null, set it to either xsd:integer or xsd:double, depending on if the value contains a fractional and/or an exponential component.
|
80
|
-
lit = RDF::Literal.new(value, :
|
80
|
+
lit = RDF::Literal.new(value, canonicalize: true)
|
81
81
|
value = lit.to_s
|
82
82
|
datatype ||= lit.datatype
|
83
83
|
else
|
@@ -88,7 +88,7 @@ module JSON::LD
|
|
88
88
|
|
89
89
|
# Initialize literal as an RDF literal using value and datatype. If element has the key @language and datatype is xsd:string, then add the value associated with the @language key as the language of the object.
|
90
90
|
language = item.fetch('@language', nil)
|
91
|
-
RDF::Literal.new(value, :
|
91
|
+
RDF::Literal.new(value, datatype: datatype, language: language)
|
92
92
|
else
|
93
93
|
# Otherwise, value must be a node definition containing only @id whos value is an IRI or Blank Node identifier
|
94
94
|
raise "Expected node reference, got #{item.inspect}" unless item.keys == %w(@id)
|
data/lib/json/ld/writer.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'json/ld/streaming_writer'
|
1
2
|
module JSON::LD
|
2
3
|
##
|
3
4
|
# A JSON-LD parser in Ruby.
|
@@ -10,7 +11,7 @@ module JSON::LD
|
|
10
11
|
# RDF::Writer.for(:jsonld) #=> JSON::LD::Writer
|
11
12
|
# RDF::Writer.for("etc/test.json")
|
12
13
|
# RDF::Writer.for(:file_name => "etc/test.json")
|
13
|
-
# RDF::Writer.for(:
|
14
|
+
# RDF::Writer.for(file_extension: "json")
|
14
15
|
# RDF::Writer.for(:content_type => "application/turtle")
|
15
16
|
#
|
16
17
|
# @example Serializing RDF graph into an JSON-LD file
|
@@ -36,9 +37,9 @@ module JSON::LD
|
|
36
37
|
#
|
37
38
|
# @example Creating @@context prefix definitions in output
|
38
39
|
# JSON::LD::Writer.buffer(
|
39
|
-
# :
|
40
|
+
# prefixes: {
|
40
41
|
# nil => "http://example.com/ns#",
|
41
|
-
# :
|
42
|
+
# foaf: "http://xmlns.com/foaf/0.1/"}
|
42
43
|
# ) do |writer|
|
43
44
|
# graph.each_statement do |statement|
|
44
45
|
# writer << statement
|
@@ -51,6 +52,7 @@ module JSON::LD
|
|
51
52
|
# @see http://json-ld.org/spec/ED/20110507/#the-normalization-algorithm
|
52
53
|
# @author [Gregg Kellogg](http://greggkellogg.net/)
|
53
54
|
class Writer < RDF::Writer
|
55
|
+
include StreamingWriter
|
54
56
|
include Utils
|
55
57
|
format Format
|
56
58
|
|
@@ -89,6 +91,8 @@ module JSON::LD
|
|
89
91
|
# frame to use when serializing.
|
90
92
|
# @option options [Boolean] :unique_bnodes (false)
|
91
93
|
# Use unique bnode identifiers, defaults to using the identifier which the node was originall initialized with (if any).
|
94
|
+
# @option options [Boolean] :stream (false)
|
95
|
+
# Do not attempt to optimize graph presentation, suitable for streaming large graphs.
|
92
96
|
# @yield [writer] `self`
|
93
97
|
# @yieldparam [RDF::Writer] writer
|
94
98
|
# @yieldreturn [void]
|
@@ -99,6 +103,7 @@ module JSON::LD
|
|
99
103
|
options[:base] ||= options[:base_uri] if options.has_key?(:base_uri)
|
100
104
|
super do
|
101
105
|
@repo = RDF::Repository.new
|
106
|
+
@debug = @options[:debug]
|
102
107
|
|
103
108
|
if block_given?
|
104
109
|
case block.arity
|
@@ -124,7 +129,13 @@ module JSON::LD
|
|
124
129
|
# @param [RDF::Statement] statement
|
125
130
|
# @return [void]
|
126
131
|
def write_statement(statement)
|
127
|
-
|
132
|
+
case
|
133
|
+
when @options[:stream]
|
134
|
+
stream_statement(statement)
|
135
|
+
else
|
136
|
+
# Add to repo and output in epilogue
|
137
|
+
@repo.insert(statement)
|
138
|
+
end
|
128
139
|
end
|
129
140
|
|
130
141
|
##
|
@@ -136,7 +147,19 @@ module JSON::LD
|
|
136
147
|
# @raise [NotImplementedError] unless implemented in subclass
|
137
148
|
# @abstract
|
138
149
|
def write_triple(subject, predicate, object)
|
139
|
-
|
150
|
+
write_statement(Statement.new(subject, predicate, object))
|
151
|
+
end
|
152
|
+
|
153
|
+
##
|
154
|
+
# Necessary for streaming
|
155
|
+
# @return [void] `self`
|
156
|
+
def write_prologue
|
157
|
+
case
|
158
|
+
when @options[:stream]
|
159
|
+
stream_prologue
|
160
|
+
else
|
161
|
+
super
|
162
|
+
end
|
140
163
|
end
|
141
164
|
|
142
165
|
##
|
@@ -148,7 +171,7 @@ module JSON::LD
|
|
148
171
|
# @return [void]
|
149
172
|
# @see #write_triple
|
150
173
|
def write_epilogue
|
151
|
-
|
174
|
+
return stream_epilogue if @options[:stream]
|
152
175
|
|
153
176
|
debug("writer") { "serialize #{@repo.count} statements, #{@options.inspect}"}
|
154
177
|
result = API.fromRdf(@repo, @options)
|
data/spec/api_spec.rb
CHANGED
@@ -5,6 +5,59 @@ require 'spec_helper'
|
|
5
5
|
describe JSON::LD::API do
|
6
6
|
before(:each) { @debug = []}
|
7
7
|
|
8
|
+
describe "#initialize" do
|
9
|
+
context "with string input" do
|
10
|
+
let(:context) do
|
11
|
+
JSON::LD::API::RemoteDocument.new("http://example.com/context", %q({
|
12
|
+
"@context": {
|
13
|
+
"xsd": "http://www.w3.org/2001/XMLSchema#",
|
14
|
+
"name": "http://xmlns.com/foaf/0.1/name",
|
15
|
+
"homepage": {"@id": "http://xmlns.com/foaf/0.1/homepage", "@type": "@id"},
|
16
|
+
"avatar": {"@id": "http://xmlns.com/foaf/0.1/avatar", "@type": "@id"}
|
17
|
+
}
|
18
|
+
}))
|
19
|
+
end
|
20
|
+
let(:remote_doc) do
|
21
|
+
JSON::LD::API::RemoteDocument.new("http://example.com/foo", %q({
|
22
|
+
"@id": "",
|
23
|
+
"name": "foo"
|
24
|
+
}), "http://example.com/context")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "loads document with loader and loads context" do
|
28
|
+
expect(JSON::LD::API).to receive(:documentLoader).with("http://example.com/foo", anything).and_return(remote_doc)
|
29
|
+
expect(JSON::LD::API).to receive(:documentLoader).with("http://example.com/context", anything).and_yield(context)
|
30
|
+
JSON::LD::API.new("http://example.com/foo", nil)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "with RDF::Util::File::RemoteDoc input" do
|
35
|
+
let(:context) do
|
36
|
+
JSON::LD::API::RemoteDocument.new("http://example.com/context", %q({
|
37
|
+
"@context": {
|
38
|
+
"xsd": "http://www.w3.org/2001/XMLSchema#",
|
39
|
+
"name": "http://xmlns.com/foaf/0.1/name",
|
40
|
+
"homepage": {"@id": "http://xmlns.com/foaf/0.1/homepage", "@type": "@id"},
|
41
|
+
"avatar": {"@id": "http://xmlns.com/foaf/0.1/avatar", "@type": "@id"}
|
42
|
+
}
|
43
|
+
}))
|
44
|
+
end
|
45
|
+
let(:remote_doc) do
|
46
|
+
RDF::Util::File::RemoteDocument.new(%q({"@id": "", "name": "foo"}),
|
47
|
+
headers: {
|
48
|
+
content_type: 'application/json',
|
49
|
+
link: %(<http://example.com/context>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json")
|
50
|
+
}
|
51
|
+
)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "processes document and retrieves linked context" do
|
55
|
+
expect(JSON::LD::API).to receive(:documentLoader).with("http://example.com/context", anything).and_yield(context)
|
56
|
+
JSON::LD::API.new(remote_doc, nil)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
8
61
|
context "Test Files" do
|
9
62
|
Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), 'test-files/*-input.*'))) do |filename|
|
10
63
|
test = File.basename(filename).sub(/-input\..*$/, '')
|
@@ -18,24 +71,24 @@ describe JSON::LD::API do
|
|
18
71
|
|
19
72
|
context test do
|
20
73
|
it "expands" do
|
21
|
-
options = {:
|
74
|
+
options = {debug: @debug}
|
22
75
|
options[:expandContext] = File.open(context) if context
|
23
76
|
jld = JSON::LD::API.expand(File.open(filename), options)
|
24
77
|
expect(jld).to produce(JSON.load(File.open(expanded)), @debug)
|
25
78
|
end if File.exist?(expanded)
|
26
79
|
|
27
80
|
it "compacts" do
|
28
|
-
jld = JSON::LD::API.compact(File.open(filename), File.open(context), :
|
81
|
+
jld = JSON::LD::API.compact(File.open(filename), File.open(context), debug: @debug)
|
29
82
|
expect(jld).to produce(JSON.load(File.open(compacted)), @debug)
|
30
83
|
end if File.exist?(compacted) && File.exist?(context)
|
31
84
|
|
32
85
|
it "frame" do
|
33
|
-
jld = JSON::LD::API.frame(File.open(filename), File.open(frame), :
|
86
|
+
jld = JSON::LD::API.frame(File.open(filename), File.open(frame), debug: @debug)
|
34
87
|
expect(jld).to produce(JSON.load(File.open(framed)), @debug)
|
35
88
|
end if File.exist?(framed) && File.exist?(frame)
|
36
89
|
|
37
90
|
it "toRdf" do
|
38
|
-
expect(RDF::Repository.load(filename, :
|
91
|
+
expect(RDF::Repository.load(filename, debug: @debug)).to be_equivalent_graph(RDF::Repository.load(ttl), trace: @debug)
|
39
92
|
end if File.exist?(ttl)
|
40
93
|
end
|
41
94
|
end
|
data/spec/compact_spec.rb
CHANGED
@@ -8,79 +8,79 @@ describe JSON::LD::API do
|
|
8
8
|
describe ".compact" do
|
9
9
|
{
|
10
10
|
"prefix" => {
|
11
|
-
:
|
11
|
+
input: {
|
12
12
|
"@id" => "http://example.com/a",
|
13
13
|
"http://example.com/b" => {"@id" => "http://example.com/c"}
|
14
14
|
},
|
15
|
-
:
|
16
|
-
:
|
15
|
+
context: {"ex" => "http://example.com/"},
|
16
|
+
output: {
|
17
17
|
"@context" => {"ex" => "http://example.com/"},
|
18
18
|
"@id" => "ex:a",
|
19
19
|
"ex:b" => {"@id" => "ex:c"}
|
20
20
|
}
|
21
21
|
},
|
22
22
|
"term" => {
|
23
|
-
:
|
23
|
+
input: {
|
24
24
|
"@id" => "http://example.com/a",
|
25
25
|
"http://example.com/b" => {"@id" => "http://example.com/c"}
|
26
26
|
},
|
27
|
-
:
|
28
|
-
:
|
27
|
+
context: {"b" => "http://example.com/b"},
|
28
|
+
output: {
|
29
29
|
"@context" => {"b" => "http://example.com/b"},
|
30
30
|
"@id" => "http://example.com/a",
|
31
31
|
"b" => {"@id" => "http://example.com/c"}
|
32
32
|
}
|
33
33
|
},
|
34
34
|
"integer value" => {
|
35
|
-
:
|
35
|
+
input: {
|
36
36
|
"@id" => "http://example.com/a",
|
37
37
|
"http://example.com/b" => {"@value" => 1}
|
38
38
|
},
|
39
|
-
:
|
40
|
-
:
|
39
|
+
context: {"b" => "http://example.com/b"},
|
40
|
+
output: {
|
41
41
|
"@context" => {"b" => "http://example.com/b"},
|
42
42
|
"@id" => "http://example.com/a",
|
43
43
|
"b" => 1
|
44
44
|
}
|
45
45
|
},
|
46
46
|
"boolean value" => {
|
47
|
-
:
|
47
|
+
input: {
|
48
48
|
"@id" => "http://example.com/a",
|
49
49
|
"http://example.com/b" => {"@value" => true}
|
50
50
|
},
|
51
|
-
:
|
52
|
-
:
|
51
|
+
context: {"b" => "http://example.com/b"},
|
52
|
+
output: {
|
53
53
|
"@context" => {"b" => "http://example.com/b"},
|
54
54
|
"@id" => "http://example.com/a",
|
55
55
|
"b" => true
|
56
56
|
}
|
57
57
|
},
|
58
58
|
"@id" => {
|
59
|
-
:
|
60
|
-
:
|
61
|
-
:
|
59
|
+
input: {"@id" => "http://example.org/test#example"},
|
60
|
+
context: {},
|
61
|
+
output: {}
|
62
62
|
},
|
63
63
|
"@id coercion" => {
|
64
|
-
:
|
64
|
+
input: {
|
65
65
|
"@id" => "http://example.com/a",
|
66
66
|
"http://example.com/b" => {"@id" => "http://example.com/c"}
|
67
67
|
},
|
68
|
-
:
|
69
|
-
:
|
68
|
+
context: {"b" => {"@id" => "http://example.com/b", "@type" => "@id"}},
|
69
|
+
output: {
|
70
70
|
"@context" => {"b" => {"@id" => "http://example.com/b", "@type" => "@id"}},
|
71
71
|
"@id" => "http://example.com/a",
|
72
72
|
"b" => "http://example.com/c"
|
73
73
|
}
|
74
74
|
},
|
75
75
|
"xsd:date coercion" => {
|
76
|
-
:
|
76
|
+
input: {
|
77
77
|
"http://example.com/b" => {"@value" => "2012-01-04", "@type" => RDF::XSD.date.to_s}
|
78
78
|
},
|
79
|
-
:
|
79
|
+
context: {
|
80
80
|
"xsd" => RDF::XSD.to_s,
|
81
81
|
"b" => {"@id" => "http://example.com/b", "@type" => "xsd:date"}
|
82
82
|
},
|
83
|
-
:
|
83
|
+
output: {
|
84
84
|
"@context" => {
|
85
85
|
"xsd" => RDF::XSD.to_s,
|
86
86
|
"b" => {"@id" => "http://example.com/b", "@type" => "xsd:date"}
|
@@ -89,26 +89,26 @@ describe JSON::LD::API do
|
|
89
89
|
}
|
90
90
|
},
|
91
91
|
"@list coercion" => {
|
92
|
-
:
|
92
|
+
input: {
|
93
93
|
"http://example.com/b" => {"@list" => ["c", "d"]}
|
94
94
|
},
|
95
|
-
:
|
96
|
-
:
|
95
|
+
context: {"b" => {"@id" => "http://example.com/b", "@container" => "@list"}},
|
96
|
+
output: {
|
97
97
|
"@context" => {"b" => {"@id" => "http://example.com/b", "@container" => "@list"}},
|
98
98
|
"b" => ["c", "d"]
|
99
99
|
}
|
100
100
|
},
|
101
101
|
"@list coercion (integer)" => {
|
102
|
-
:
|
102
|
+
input: {
|
103
103
|
"http://example.com/term" => [
|
104
104
|
{"@list" => [1]},
|
105
105
|
]
|
106
106
|
},
|
107
|
-
:
|
107
|
+
context: {
|
108
108
|
"term4" => {"@id" => "http://example.com/term", "@container" => "@list"},
|
109
109
|
"@language" => "de"
|
110
110
|
},
|
111
|
-
:
|
111
|
+
output: {
|
112
112
|
"@context" => {
|
113
113
|
"term4" => {"@id" => "http://example.com/term", "@container" => "@list"},
|
114
114
|
"@language" => "de"
|
@@ -117,59 +117,59 @@ describe JSON::LD::API do
|
|
117
117
|
}
|
118
118
|
},
|
119
119
|
"@set coercion" => {
|
120
|
-
:
|
120
|
+
input: {
|
121
121
|
"http://example.com/b" => {"@set" => ["c"]}
|
122
122
|
},
|
123
|
-
:
|
124
|
-
:
|
123
|
+
context: {"b" => {"@id" => "http://example.com/b", "@container" => "@set"}},
|
124
|
+
output: {
|
125
125
|
"@context" => {"b" => {"@id" => "http://example.com/b", "@container" => "@set"}},
|
126
126
|
"b" => ["c"]
|
127
127
|
}
|
128
128
|
},
|
129
129
|
"empty @set coercion" => {
|
130
|
-
:
|
130
|
+
input: {
|
131
131
|
"http://example.com/b" => []
|
132
132
|
},
|
133
|
-
:
|
134
|
-
:
|
133
|
+
context: {"b" => {"@id" => "http://example.com/b", "@container" => "@set"}},
|
134
|
+
output: {
|
135
135
|
"@context" => {"b" => {"@id" => "http://example.com/b", "@container" => "@set"}},
|
136
136
|
"b" => []
|
137
137
|
}
|
138
138
|
},
|
139
139
|
"@type with string @id" => {
|
140
|
-
:
|
140
|
+
input: {
|
141
141
|
"@id" => "http://example.com/",
|
142
142
|
"@type" => "#{RDF::RDFS.Resource}"
|
143
143
|
},
|
144
|
-
:
|
145
|
-
:
|
144
|
+
context: {},
|
145
|
+
output: {
|
146
146
|
"@id" => "http://example.com/",
|
147
147
|
"@type" => "#{RDF::RDFS.Resource}"
|
148
148
|
},
|
149
149
|
},
|
150
150
|
"@type with array @id" => {
|
151
|
-
:
|
151
|
+
input: {
|
152
152
|
"@id" => "http://example.com/",
|
153
153
|
"@type" => ["#{RDF::RDFS.Resource}"]
|
154
154
|
},
|
155
|
-
:
|
156
|
-
:
|
155
|
+
context: {},
|
156
|
+
output: {
|
157
157
|
"@id" => "http://example.com/",
|
158
158
|
"@type" => "#{RDF::RDFS.Resource}"
|
159
159
|
},
|
160
160
|
},
|
161
161
|
"default language" => {
|
162
|
-
:
|
162
|
+
input: {
|
163
163
|
"http://example.com/term" => [
|
164
164
|
"v5",
|
165
165
|
{"@value" => "plain literal"}
|
166
166
|
]
|
167
167
|
},
|
168
|
-
:
|
168
|
+
context: {
|
169
169
|
"term5" => {"@id" => "http://example.com/term", "@language" => nil},
|
170
170
|
"@language" => "de"
|
171
171
|
},
|
172
|
-
:
|
172
|
+
output: {
|
173
173
|
"@context" => {
|
174
174
|
"term5" => {"@id" => "http://example.com/term", "@language" => nil},
|
175
175
|
"@language" => "de"
|
@@ -179,7 +179,7 @@ describe JSON::LD::API do
|
|
179
179
|
},
|
180
180
|
}.each_pair do |title, params|
|
181
181
|
it title do
|
182
|
-
jld = JSON::LD::API.compact(params[:input], params[:context], :
|
182
|
+
jld = JSON::LD::API.compact(params[:input], params[:context], debug: @debug)
|
183
183
|
expect(jld).to produce(params[:output], @debug)
|
184
184
|
end
|
185
185
|
end
|
@@ -187,62 +187,62 @@ describe JSON::LD::API do
|
|
187
187
|
context "keyword aliasing" do
|
188
188
|
{
|
189
189
|
"@id" => {
|
190
|
-
:
|
190
|
+
input: {
|
191
191
|
"@id" => "",
|
192
192
|
"@type" => "#{RDF::RDFS.Resource}"
|
193
193
|
},
|
194
|
-
:
|
195
|
-
:
|
194
|
+
context: {"id" => "@id"},
|
195
|
+
output: {
|
196
196
|
"@context" => {"id" => "@id"},
|
197
197
|
"id" => "",
|
198
198
|
"@type" => "#{RDF::RDFS.Resource}"
|
199
199
|
}
|
200
200
|
},
|
201
201
|
"@type" => {
|
202
|
-
:
|
202
|
+
input: {
|
203
203
|
"@type" => RDF::RDFS.Resource.to_s,
|
204
204
|
"http://example.org/foo" => {"@value" => "bar", "@type" => "http://example.com/type"}
|
205
205
|
},
|
206
|
-
:
|
207
|
-
:
|
206
|
+
context: {"type" => "@type"},
|
207
|
+
output: {
|
208
208
|
"@context" => {"type" => "@type"},
|
209
209
|
"type" => RDF::RDFS.Resource.to_s,
|
210
210
|
"http://example.org/foo" => {"@value" => "bar", "type" => "http://example.com/type"}
|
211
211
|
}
|
212
212
|
},
|
213
213
|
"@language" => {
|
214
|
-
:
|
214
|
+
input: {
|
215
215
|
"http://example.org/foo" => {"@value" => "bar", "@language" => "baz"}
|
216
216
|
},
|
217
|
-
:
|
218
|
-
:
|
217
|
+
context: {"language" => "@language"},
|
218
|
+
output: {
|
219
219
|
"@context" => {"language" => "@language"},
|
220
220
|
"http://example.org/foo" => {"@value" => "bar", "language" => "baz"}
|
221
221
|
}
|
222
222
|
},
|
223
223
|
"@value" => {
|
224
|
-
:
|
224
|
+
input: {
|
225
225
|
"http://example.org/foo" => {"@value" => "bar", "@language" => "baz"}
|
226
226
|
},
|
227
|
-
:
|
228
|
-
:
|
227
|
+
context: {"literal" => "@value"},
|
228
|
+
output: {
|
229
229
|
"@context" => {"literal" => "@value"},
|
230
230
|
"http://example.org/foo" => {"literal" => "bar", "@language" => "baz"}
|
231
231
|
}
|
232
232
|
},
|
233
233
|
"@list" => {
|
234
|
-
:
|
234
|
+
input: {
|
235
235
|
"http://example.org/foo" => {"@list" => ["bar"]}
|
236
236
|
},
|
237
|
-
:
|
238
|
-
:
|
237
|
+
context: {"list" => "@list"},
|
238
|
+
output: {
|
239
239
|
"@context" => {"list" => "@list"},
|
240
240
|
"http://example.org/foo" => {"list" => ["bar"]}
|
241
241
|
}
|
242
242
|
},
|
243
243
|
}.each do |title, params|
|
244
244
|
it title do
|
245
|
-
jld = JSON::LD::API.compact(params[:input], params[:context], :
|
245
|
+
jld = JSON::LD::API.compact(params[:input], params[:context], debug: @debug)
|
246
246
|
expect(jld).to produce(params[:output], @debug)
|
247
247
|
end
|
248
248
|
end
|
@@ -251,14 +251,14 @@ describe JSON::LD::API do
|
|
251
251
|
context "term selection" do
|
252
252
|
{
|
253
253
|
"Uses term with nil language when two terms conflict on language" => {
|
254
|
-
:
|
254
|
+
input: [{
|
255
255
|
"http://example.com/term" => {"@value" => "v1"}
|
256
256
|
}],
|
257
|
-
:
|
257
|
+
context: {
|
258
258
|
"term5" => {"@id" => "http://example.com/term","@language" => nil},
|
259
259
|
"@language" => "de"
|
260
260
|
},
|
261
|
-
:
|
261
|
+
output: {
|
262
262
|
"@context" => {
|
263
263
|
"term5" => {"@id" => "http://example.com/term","@language" => nil},
|
264
264
|
"@language" => "de"
|
@@ -267,15 +267,15 @@ describe JSON::LD::API do
|
|
267
267
|
}
|
268
268
|
},
|
269
269
|
"Uses subject alias" => {
|
270
|
-
:
|
270
|
+
input: [{
|
271
271
|
"@id" => "http://example.com/id1",
|
272
272
|
"http://example.com/id1" => {"@value" => "foo", "@language" => "de"}
|
273
273
|
}],
|
274
|
-
:
|
274
|
+
context: {
|
275
275
|
"id1" => "http://example.com/id1",
|
276
276
|
"@language" => "de"
|
277
277
|
},
|
278
|
-
:
|
278
|
+
output: {
|
279
279
|
"@context" => {
|
280
280
|
"id1" => "http://example.com/id1",
|
281
281
|
"@language" => "de"
|
@@ -285,14 +285,14 @@ describe JSON::LD::API do
|
|
285
285
|
}
|
286
286
|
},
|
287
287
|
"compact-0007" => {
|
288
|
-
:
|
288
|
+
input: ::JSON.parse(%(
|
289
289
|
{"http://example.org/vocab#contains": "this-is-not-an-IRI"}
|
290
290
|
)),
|
291
|
-
:
|
291
|
+
context: ::JSON.parse(%({
|
292
292
|
"ex": "http://example.org/vocab#",
|
293
293
|
"ex:contains": {"@type": "@id"}
|
294
294
|
})),
|
295
|
-
:
|
295
|
+
output: ::JSON.parse(%({
|
296
296
|
"@context": {
|
297
297
|
"ex": "http://example.org/vocab#",
|
298
298
|
"ex:contains": {"@type": "@id"}
|
@@ -305,7 +305,7 @@ describe JSON::LD::API do
|
|
305
305
|
input = params[:input].is_a?(String) ? JSON.parse(params[:input]) : params[:input]
|
306
306
|
ctx = params[:context].is_a?(String) ? JSON.parse(params[:context]) : params[:context]
|
307
307
|
output = params[:output].is_a?(String) ? JSON.parse(params[:output]) : params[:output]
|
308
|
-
jld = JSON::LD::API.compact(input, ctx, :
|
308
|
+
jld = JSON::LD::API.compact(input, ctx, debug: @debug)
|
309
309
|
expect(jld).to produce(output, @debug)
|
310
310
|
end
|
311
311
|
end
|
@@ -314,7 +314,7 @@ describe JSON::LD::API do
|
|
314
314
|
context "@reverse" do
|
315
315
|
{
|
316
316
|
"compact-0033" => {
|
317
|
-
:
|
317
|
+
input: %([
|
318
318
|
{
|
319
319
|
"@id": "http://example.com/people/markus",
|
320
320
|
"@reverse": {
|
@@ -328,11 +328,11 @@ describe JSON::LD::API do
|
|
328
328
|
"http://xmlns.com/foaf/0.1/name": [ { "@value": "Markus Lanthaler" } ]
|
329
329
|
}
|
330
330
|
]),
|
331
|
-
:
|
331
|
+
context: %({
|
332
332
|
"name": "http://xmlns.com/foaf/0.1/name",
|
333
333
|
"isKnownBy": { "@reverse": "http://xmlns.com/foaf/0.1/knows" }
|
334
334
|
}),
|
335
|
-
:
|
335
|
+
output: %({
|
336
336
|
"@context": {
|
337
337
|
"name": "http://xmlns.com/foaf/0.1/name",
|
338
338
|
"isKnownBy": {
|
@@ -352,7 +352,7 @@ describe JSON::LD::API do
|
|
352
352
|
input = params[:input].is_a?(String) ? JSON.parse(params[:input]) : params[:input]
|
353
353
|
ctx = params[:context].is_a?(String) ? JSON.parse(params[:context]) : params[:context]
|
354
354
|
output = params[:output].is_a?(String) ? JSON.parse(params[:output]) : params[:output]
|
355
|
-
jld = JSON::LD::API.compact(input, ctx, :
|
355
|
+
jld = JSON::LD::API.compact(input, ctx, debug: @debug)
|
356
356
|
expect(jld).to produce(output, @debug)
|
357
357
|
end
|
358
358
|
end
|
@@ -372,7 +372,7 @@ describe JSON::LD::API do
|
|
372
372
|
},
|
373
373
|
"foo" => "bar"
|
374
374
|
}
|
375
|
-
jld = JSON::LD::API.compact(input, ctx, :
|
375
|
+
jld = JSON::LD::API.compact(input, ctx, debug: @debug, validate: true)
|
376
376
|
expect(jld).to produce(expected, @debug)
|
377
377
|
end
|
378
378
|
end
|
@@ -389,8 +389,8 @@ describe JSON::LD::API do
|
|
389
389
|
"@context" => "http://example.com/context",
|
390
390
|
"b" => "c"
|
391
391
|
}
|
392
|
-
allow(JSON::LD::API).to receive(:documentLoader).with("http://example.com/context").and_yield(remote_doc)
|
393
|
-
jld = JSON::LD::API.compact(input, "http://example.com/context", :
|
392
|
+
allow(JSON::LD::API).to receive(:documentLoader).with("http://example.com/context", anything).and_yield(remote_doc)
|
393
|
+
jld = JSON::LD::API.compact(input, "http://example.com/context", debug: @debug, validate: true)
|
394
394
|
expect(jld).to produce(expected, @debug)
|
395
395
|
end
|
396
396
|
end
|
@@ -398,17 +398,17 @@ describe JSON::LD::API do
|
|
398
398
|
context "@list" do
|
399
399
|
{
|
400
400
|
"1 term 2 lists 2 languages" => {
|
401
|
-
:
|
401
|
+
input: [{
|
402
402
|
"http://example.com/foo" => [
|
403
403
|
{"@list" => [{"@value" => "en", "@language" => "en"}]},
|
404
404
|
{"@list" => [{"@value" => "de", "@language" => "de"}]}
|
405
405
|
]
|
406
406
|
}],
|
407
|
-
:
|
407
|
+
context: {
|
408
408
|
"foo_en" => {"@id" => "http://example.com/foo", "@container" => "@list", "@language" => "en"},
|
409
409
|
"foo_de" => {"@id" => "http://example.com/foo", "@container" => "@list", "@language" => "de"}
|
410
410
|
},
|
411
|
-
:
|
411
|
+
output: {
|
412
412
|
"@context" => {
|
413
413
|
"foo_en" => {"@id" => "http://example.com/foo", "@container" => "@list", "@language" => "en"},
|
414
414
|
"foo_de" => {"@id" => "http://example.com/foo", "@container" => "@list", "@language" => "de"}
|
@@ -419,7 +419,7 @@ describe JSON::LD::API do
|
|
419
419
|
},
|
420
420
|
}.each_pair do |title, params|
|
421
421
|
it title do
|
422
|
-
jld = JSON::LD::API.compact(params[:input], params[:context], :
|
422
|
+
jld = JSON::LD::API.compact(params[:input], params[:context], debug: @debug)
|
423
423
|
expect(jld).to produce(params[:output], @debug)
|
424
424
|
end
|
425
425
|
end
|
@@ -428,7 +428,7 @@ describe JSON::LD::API do
|
|
428
428
|
context "language maps" do
|
429
429
|
{
|
430
430
|
"compact-0024" => {
|
431
|
-
:
|
431
|
+
input: [
|
432
432
|
{
|
433
433
|
"@id" => "http://example.com/queen",
|
434
434
|
"http://example.com/vocab/label" => [
|
@@ -438,11 +438,11 @@ describe JSON::LD::API do
|
|
438
438
|
]
|
439
439
|
}
|
440
440
|
],
|
441
|
-
:
|
441
|
+
context: {
|
442
442
|
"vocab" => "http://example.com/vocab/",
|
443
443
|
"label" => {"@id" => "vocab:label", "@container" => "@language"}
|
444
444
|
},
|
445
|
-
:
|
445
|
+
output: {
|
446
446
|
"@context" => {
|
447
447
|
"vocab" => "http://example.com/vocab/",
|
448
448
|
"label" => {"@id" => "vocab:label", "@container" => "@language"}
|
@@ -456,7 +456,7 @@ describe JSON::LD::API do
|
|
456
456
|
},
|
457
457
|
}.each_pair do |title, params|
|
458
458
|
it title do
|
459
|
-
jld = JSON::LD::API.compact(params[:input], params[:context], :
|
459
|
+
jld = JSON::LD::API.compact(params[:input], params[:context], debug: @debug)
|
460
460
|
expect(jld).to produce(params[:output], @debug)
|
461
461
|
end
|
462
462
|
end
|
@@ -465,12 +465,12 @@ describe JSON::LD::API do
|
|
465
465
|
context "@graph" do
|
466
466
|
{
|
467
467
|
"Uses @graph given mutliple inputs" => {
|
468
|
-
:
|
468
|
+
input: [
|
469
469
|
{"http://example.com/foo" => ["foo"]},
|
470
470
|
{"http://example.com/bar" => ["bar"]}
|
471
471
|
],
|
472
|
-
:
|
473
|
-
:
|
472
|
+
context: {"ex" => "http://example.com/"},
|
473
|
+
output: {
|
474
474
|
"@context" => {"ex" => "http://example.com/"},
|
475
475
|
"@graph" => [
|
476
476
|
{"ex:foo" => "foo"},
|
@@ -480,7 +480,7 @@ describe JSON::LD::API do
|
|
480
480
|
},
|
481
481
|
}.each_pair do |title, params|
|
482
482
|
it title do
|
483
|
-
jld = JSON::LD::API.compact(params[:input], params[:context], :
|
483
|
+
jld = JSON::LD::API.compact(params[:input], params[:context], debug: @debug)
|
484
484
|
expect(jld).to produce(params[:output], @debug)
|
485
485
|
end
|
486
486
|
end
|
@@ -489,17 +489,17 @@ describe JSON::LD::API do
|
|
489
489
|
context "exceptions" do
|
490
490
|
{
|
491
491
|
"@list containing @list" => {
|
492
|
-
:
|
492
|
+
input: {
|
493
493
|
"http://example.org/foo" => {"@list" => [{"@list" => ["baz"]}]}
|
494
494
|
},
|
495
|
-
:
|
495
|
+
exception: JSON::LD::JsonLdError::ListOfLists
|
496
496
|
},
|
497
497
|
"@list containing @list (with coercion)" => {
|
498
|
-
:
|
498
|
+
input: {
|
499
499
|
"@context" => {"http://example.org/foo" => {"@container" => "@list"}},
|
500
500
|
"http://example.org/foo" => [{"@list" => ["baz"]}]
|
501
501
|
},
|
502
|
-
:
|
502
|
+
exception: JSON::LD::JsonLdError::ListOfLists
|
503
503
|
},
|
504
504
|
}.each do |title, params|
|
505
505
|
it title do
|