json-ld 3.2.3 → 3.2.5
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/VERSION +1 -1
- data/lib/json/ld/api.rb +807 -764
- data/lib/json/ld/compact.rb +304 -304
- data/lib/json/ld/conneg.rb +179 -161
- data/lib/json/ld/context.rb +2080 -1945
- data/lib/json/ld/expand.rb +745 -666
- data/lib/json/ld/extensions.rb +14 -13
- data/lib/json/ld/flatten.rb +257 -247
- data/lib/json/ld/format.rb +202 -194
- data/lib/json/ld/frame.rb +525 -502
- data/lib/json/ld/from_rdf.rb +224 -166
- data/lib/json/ld/html/nokogiri.rb +123 -121
- data/lib/json/ld/html/rexml.rb +151 -147
- data/lib/json/ld/reader.rb +107 -100
- data/lib/json/ld/resource.rb +224 -205
- data/lib/json/ld/streaming_reader.rb +574 -507
- data/lib/json/ld/streaming_writer.rb +93 -92
- data/lib/json/ld/to_rdf.rb +171 -167
- data/lib/json/ld/utils.rb +270 -264
- data/lib/json/ld/version.rb +24 -14
- data/lib/json/ld/writer.rb +334 -311
- data/lib/json/ld.rb +103 -96
- metadata +78 -209
- data/spec/api_spec.rb +0 -132
- data/spec/compact_spec.rb +0 -3482
- data/spec/conneg_spec.rb +0 -373
- data/spec/context_spec.rb +0 -2036
- data/spec/expand_spec.rb +0 -4496
- data/spec/flatten_spec.rb +0 -1203
- data/spec/format_spec.rb +0 -115
- data/spec/frame_spec.rb +0 -2498
- data/spec/from_rdf_spec.rb +0 -1005
- data/spec/matchers.rb +0 -20
- data/spec/rdfstar_spec.rb +0 -25
- data/spec/reader_spec.rb +0 -883
- data/spec/resource_spec.rb +0 -76
- data/spec/spec_helper.rb +0 -281
- data/spec/streaming_reader_spec.rb +0 -237
- data/spec/streaming_writer_spec.rb +0 -145
- data/spec/suite_compact_spec.rb +0 -22
- data/spec/suite_expand_spec.rb +0 -36
- data/spec/suite_flatten_spec.rb +0 -34
- data/spec/suite_frame_spec.rb +0 -29
- data/spec/suite_from_rdf_spec.rb +0 -22
- data/spec/suite_helper.rb +0 -411
- data/spec/suite_html_spec.rb +0 -22
- data/spec/suite_http_spec.rb +0 -35
- data/spec/suite_remote_doc_spec.rb +0 -22
- data/spec/suite_to_rdf_spec.rb +0 -30
- data/spec/support/extensions.rb +0 -44
- data/spec/test-files/test-1-compacted.jsonld +0 -10
- data/spec/test-files/test-1-context.jsonld +0 -7
- data/spec/test-files/test-1-expanded.jsonld +0 -5
- data/spec/test-files/test-1-input.jsonld +0 -10
- data/spec/test-files/test-1-rdf.ttl +0 -8
- data/spec/test-files/test-2-compacted.jsonld +0 -20
- data/spec/test-files/test-2-context.jsonld +0 -7
- data/spec/test-files/test-2-expanded.jsonld +0 -16
- data/spec/test-files/test-2-input.jsonld +0 -20
- data/spec/test-files/test-2-rdf.ttl +0 -14
- data/spec/test-files/test-3-compacted.jsonld +0 -11
- data/spec/test-files/test-3-context.jsonld +0 -8
- data/spec/test-files/test-3-expanded.jsonld +0 -10
- data/spec/test-files/test-3-input.jsonld +0 -11
- data/spec/test-files/test-3-rdf.ttl +0 -8
- data/spec/test-files/test-4-compacted.jsonld +0 -10
- data/spec/test-files/test-4-context.jsonld +0 -7
- data/spec/test-files/test-4-expanded.jsonld +0 -6
- data/spec/test-files/test-4-input.jsonld +0 -10
- data/spec/test-files/test-4-rdf.ttl +0 -5
- data/spec/test-files/test-5-compacted.jsonld +0 -13
- data/spec/test-files/test-5-context.jsonld +0 -7
- data/spec/test-files/test-5-expanded.jsonld +0 -9
- data/spec/test-files/test-5-input.jsonld +0 -13
- data/spec/test-files/test-5-rdf.ttl +0 -7
- data/spec/test-files/test-6-compacted.jsonld +0 -10
- data/spec/test-files/test-6-context.jsonld +0 -7
- data/spec/test-files/test-6-expanded.jsonld +0 -10
- data/spec/test-files/test-6-input.jsonld +0 -10
- data/spec/test-files/test-6-rdf.ttl +0 -6
- data/spec/test-files/test-7-compacted.jsonld +0 -23
- data/spec/test-files/test-7-context.jsonld +0 -4
- data/spec/test-files/test-7-expanded.jsonld +0 -20
- data/spec/test-files/test-7-input.jsonld +0 -23
- data/spec/test-files/test-7-rdf.ttl +0 -14
- data/spec/test-files/test-8-compacted.jsonld +0 -34
- data/spec/test-files/test-8-context.jsonld +0 -11
- data/spec/test-files/test-8-expanded.jsonld +0 -24
- data/spec/test-files/test-8-frame.jsonld +0 -18
- data/spec/test-files/test-8-framed.jsonld +0 -25
- data/spec/test-files/test-8-input.jsonld +0 -30
- data/spec/test-files/test-8-rdf.ttl +0 -15
- data/spec/test-files/test-9-compacted.jsonld +0 -20
- data/spec/test-files/test-9-context.jsonld +0 -13
- data/spec/test-files/test-9-expanded.jsonld +0 -14
- data/spec/test-files/test-9-input.jsonld +0 -12
- data/spec/to_rdf_spec.rb +0 -1551
- data/spec/writer_spec.rb +0 -427
data/lib/json/ld/from_rdf.rb
CHANGED
@@ -1,195 +1,253 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
1
|
# frozen_string_literal: true
|
3
|
-
require 'rdf/nquads'
|
4
|
-
|
5
|
-
module JSON::LD
|
6
|
-
module FromRDF
|
7
|
-
include Utils
|
8
|
-
|
9
|
-
##
|
10
|
-
# Generate a JSON-LD array representation from an array of `RDF::Statement`.
|
11
|
-
# Representation is in expanded form
|
12
|
-
#
|
13
|
-
# @param [Array<RDF::Statement>, RDF::Enumerable] dataset
|
14
|
-
# @param [Boolean] useRdfType (false)
|
15
|
-
# If set to `true`, the JSON-LD processor will treat `rdf:type` like a normal property instead of using `@type`.
|
16
|
-
# @param [Boolean] useNativeTypes (false) use native representations
|
17
|
-
#
|
18
|
-
# @return [Array<Hash>] the JSON-LD document in normalized form
|
19
|
-
def from_statements(dataset, useRdfType: false, useNativeTypes: false)
|
20
|
-
default_graph = {}
|
21
|
-
graph_map = {'@default' => default_graph}
|
22
|
-
referenced_once = {}
|
23
|
-
|
24
|
-
value = nil
|
25
2
|
|
26
|
-
|
27
|
-
compound_literal_subjects = {}
|
28
|
-
|
29
|
-
# Create a map for node to object representation
|
30
|
-
|
31
|
-
# For each statement in dataset
|
32
|
-
dataset.each do |statement|
|
33
|
-
#log_debug("statement") { statement.to_nquads.chomp}
|
3
|
+
require 'rdf/nquads'
|
34
4
|
|
35
|
-
|
5
|
+
module JSON
|
6
|
+
module LD
|
7
|
+
module FromRDF
|
8
|
+
include Utils
|
9
|
+
|
10
|
+
##
|
11
|
+
# Generate a JSON-LD array representation from an array of `RDF::Statement`.
|
12
|
+
# Representation is in expanded form
|
13
|
+
#
|
14
|
+
# @param [Array<RDF::Statement>, RDF::Enumerable] dataset
|
15
|
+
# @param [Boolean] useRdfType (false)
|
16
|
+
# If set to `true`, the JSON-LD processor will treat `rdf:type` like a normal property instead of using `@type`.
|
17
|
+
# @param [Boolean] useNativeTypes (false) use native representations
|
18
|
+
# @param extendedRepresentation (false)
|
19
|
+
# Use the extended internal representation for native types.
|
20
|
+
#
|
21
|
+
# @return [Array<Hash>] the JSON-LD document in normalized form
|
22
|
+
def from_statements(dataset, useRdfType: false, useNativeTypes: false, extendedRepresentation: false)
|
23
|
+
default_graph = {}
|
24
|
+
graph_map = { '@default' => default_graph }
|
25
|
+
referenced_once = {}
|
26
|
+
|
27
|
+
value = nil
|
28
|
+
|
29
|
+
# Create an entry for compound-literal node detection
|
30
|
+
compound_literal_subjects = {}
|
31
|
+
|
32
|
+
# Create a map for node to object representation
|
33
|
+
|
34
|
+
# For each statement in dataset
|
35
|
+
dataset.each do |statement|
|
36
|
+
# log_debug("statement") { statement.to_nquads.chomp}
|
37
|
+
|
38
|
+
name = if statement.graph_name
|
39
|
+
@context.expand_iri(statement.graph_name,
|
40
|
+
base: @options[:base]).to_s
|
41
|
+
else
|
42
|
+
'@default'
|
43
|
+
end
|
36
44
|
|
37
|
-
|
38
|
-
|
39
|
-
|
45
|
+
# Create a graph entry as needed
|
46
|
+
node_map = graph_map[name] ||= {}
|
47
|
+
compound_literal_subjects[name] ||= {}
|
40
48
|
|
41
|
-
|
49
|
+
default_graph[name] ||= { '@id' => name } unless name == '@default'
|
42
50
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
51
|
+
subject = if statement.subject.statement?
|
52
|
+
resource_representation(statement.subject, useNativeTypes, extendedRepresentation)['@id'].to_json_c14n
|
53
|
+
else
|
54
|
+
statement.subject.to_s
|
55
|
+
end
|
56
|
+
node = node_map[subject] ||= resource_representation(statement.subject, useNativeTypes,
|
57
|
+
extendedRepresentation)
|
47
58
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
59
|
+
# If predicate is rdf:datatype, note subject in compound literal subjects map
|
60
|
+
if @options[:rdfDirection] == 'compound-literal' && statement.predicate == RDF_DIRECTION
|
61
|
+
compound_literal_subjects[name][subject] ||= true
|
62
|
+
end
|
52
63
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
64
|
+
# If object is an IRI, blank node identifier, or statement, and node map does not have an object member, create one and initialize its value to a new JSON object consisting of a single member @id whose value is set to object.
|
65
|
+
unless statement.object.literal?
|
66
|
+
object = if statement.object.statement?
|
67
|
+
resource_representation(statement.object, useNativeTypes, extendedRepresentation)['@id'].to_json_c14n
|
68
|
+
else
|
69
|
+
statement.object.to_s
|
70
|
+
end
|
71
|
+
node_map[object] ||=
|
72
|
+
resource_representation(statement.object, useNativeTypes, extendedRepresentation)
|
73
|
+
end
|
61
74
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
75
|
+
# If predicate equals rdf:type, and object is an IRI or blank node identifier, append object to the value of the @type member of node. If no such member exists, create one and initialize it to an array whose only item is object. Finally, continue to the next RDF triple.
|
76
|
+
if statement.predicate == RDF.type && statement.object.resource? && !useRdfType
|
77
|
+
merge_value(node, '@type', statement.object.to_s)
|
78
|
+
next
|
79
|
+
end
|
67
80
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
81
|
+
# Set value to the result of using the RDF to Object Conversion algorithm, passing object, rdfDirection, and use native types.
|
82
|
+
value = resource_representation(statement.object, useNativeTypes, extendedRepresentation)
|
83
|
+
|
84
|
+
merge_value(node, statement.predicate.to_s, value)
|
85
|
+
|
86
|
+
# If object is a blank node identifier or rdf:nil, it might represent the a list node:
|
87
|
+
if statement.object == RDF.nil
|
88
|
+
# Append a new JSON object consisting of three members, node, property, and value to the usages array. The node member is set to a reference to node, property to predicate, and value to a reference to value.
|
89
|
+
object = node_map[statement.object.to_s]
|
90
|
+
merge_value(object, :usages, {
|
91
|
+
node: node,
|
92
|
+
property: statement.predicate.to_s,
|
93
|
+
value: value
|
94
|
+
})
|
95
|
+
elsif referenced_once.key?(statement.object.to_s)
|
96
|
+
referenced_once[statement.object.to_s] = false
|
97
|
+
elsif statement.object.node?
|
98
|
+
referenced_once[statement.object.to_s] = {
|
99
|
+
node: node,
|
100
|
+
property: statement.predicate.to_s,
|
101
|
+
value: value
|
102
|
+
}
|
103
|
+
end
|
90
104
|
end
|
91
|
-
end
|
92
105
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
if
|
111
|
-
|
106
|
+
# For each name and graph object in graph map:
|
107
|
+
graph_map.each do |name, graph_object|
|
108
|
+
# If rdfDirection is compound-literal, check referenced_once for entries from compound_literal_subjects
|
109
|
+
compound_literal_subjects.fetch(name, {}).each_key do |cl|
|
110
|
+
node = referenced_once[cl][:node]
|
111
|
+
next unless node.is_a?(Hash)
|
112
|
+
|
113
|
+
property = referenced_once[cl][:property]
|
114
|
+
value = referenced_once[cl][:value]
|
115
|
+
cl_node = graph_map[name].delete(cl)
|
116
|
+
next unless cl_node.is_a?(Hash)
|
117
|
+
|
118
|
+
node[property].select do |v|
|
119
|
+
next unless v['@id'] == cl
|
120
|
+
|
121
|
+
v.delete('@id')
|
122
|
+
v['@value'] = cl_node[RDF.value.to_s].first['@value']
|
123
|
+
if (langs = cl_node[RDF_LANGUAGE.to_s])
|
124
|
+
lang = langs.first['@value']
|
125
|
+
unless /^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$/.match?(lang)
|
126
|
+
warn "i18n datatype language must be valid BCP47: #{lang.inspect}"
|
127
|
+
end
|
128
|
+
v['@language'] = lang
|
112
129
|
end
|
113
|
-
v['@
|
130
|
+
v['@direction'] = cl_node[RDF_DIRECTION.to_s].first['@value']
|
114
131
|
end
|
115
|
-
v['@direction'] = cl_node[RDF.to_uri.to_s + 'direction'].first['@value']
|
116
132
|
end
|
117
|
-
end
|
118
133
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
134
|
+
nil_var = graph_object.fetch(RDF.nil.to_s, {})
|
135
|
+
|
136
|
+
# For each item usage in the usages member of nil, perform the following steps:
|
137
|
+
nil_var.fetch(:usages, []).each do |usage|
|
138
|
+
node = usage[:node]
|
139
|
+
property = usage[:property]
|
140
|
+
head = usage[:value]
|
141
|
+
list = []
|
142
|
+
list_nodes = []
|
143
|
+
|
144
|
+
# If property equals rdf:rest, the value associated to the usages member of node has exactly 1 entry, node has a rdf:first and rdf:rest property, both of which have as value an array consisting of a single element, and node has no other members apart from an optional @type member whose value is an array with a single item equal to rdf:List, node represents a well-formed list node. Continue with the following steps:
|
145
|
+
# log_debug("list element?") {node.to_json(JSON_STATE) rescue 'malformed json'}
|
146
|
+
while property == RDF.rest.to_s &&
|
147
|
+
blank_node?(node) &&
|
148
|
+
referenced_once[node['@id']] &&
|
149
|
+
node.keys.none? { |k| !["@id", '@type', :usages, RDF.first.to_s, RDF.rest.to_s].include?(k) } &&
|
150
|
+
(f = node[RDF.first.to_s]).is_a?(Array) && f.length == 1 &&
|
151
|
+
(r = node[RDF.rest.to_s]).is_a?(Array) && r.length == 1 &&
|
152
|
+
((t = node['@type']).nil? || t == [RDF.List.to_s])
|
153
|
+
list << Array(node[RDF.first.to_s]).first
|
154
|
+
list_nodes << node['@id']
|
155
|
+
|
156
|
+
# get next node, moving backwards through list
|
157
|
+
node_usage = referenced_once[node['@id']]
|
158
|
+
node = node_usage[:node]
|
159
|
+
property = node_usage[:property]
|
160
|
+
head = node_usage[:value]
|
161
|
+
end
|
162
|
+
|
163
|
+
head.delete('@id')
|
164
|
+
head['@list'] = list.reverse
|
165
|
+
list_nodes.each { |node_id| graph_object.delete(node_id) }
|
141
166
|
end
|
142
167
|
|
143
|
-
|
144
|
-
|
145
|
-
list_nodes.each {|node_id| graph_object.delete(node_id)}
|
168
|
+
# Create annotations on graph object
|
169
|
+
create_annotations(graph_object)
|
146
170
|
end
|
147
171
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
n = graph_map[subject][s]
|
159
|
-
n.delete(:usages)
|
160
|
-
node['@graph'] << n unless node_reference?(n)
|
172
|
+
result = []
|
173
|
+
default_graph.keys.opt_sort(ordered: @options[:ordered]).each do |subject|
|
174
|
+
node = default_graph[subject]
|
175
|
+
if graph_map.key?(subject)
|
176
|
+
node['@graph'] = []
|
177
|
+
graph_map[subject].keys.opt_sort(ordered: @options[:ordered]).each do |s|
|
178
|
+
n = graph_map[subject][s]
|
179
|
+
n.delete(:usages)
|
180
|
+
node['@graph'] << n unless node_reference?(n)
|
181
|
+
end
|
161
182
|
end
|
183
|
+
node.delete(:usages)
|
184
|
+
result << node unless node_reference?(node)
|
162
185
|
end
|
163
|
-
|
164
|
-
result
|
186
|
+
# log_debug("fromRdf") {result.to_json(JSON_STATE) rescue 'malformed json'}
|
187
|
+
result
|
165
188
|
end
|
166
|
-
#log_debug("fromRdf") {result.to_json(JSON_STATE) rescue 'malformed json'}
|
167
|
-
result
|
168
|
-
end
|
169
189
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
190
|
+
private
|
191
|
+
|
192
|
+
RDF_LITERAL_NATIVE_TYPES = Set.new([RDF::XSD.boolean, RDF::XSD.integer, RDF::XSD.double]).freeze
|
193
|
+
|
194
|
+
def resource_representation(resource, useNativeTypes, extendedRepresentation)
|
195
|
+
case resource
|
196
|
+
when RDF::Statement
|
197
|
+
# Note, if either subject or object are a BNode which is used elsewhere,
|
198
|
+
# this might not work will with the BNode accounting from above.
|
199
|
+
rep = { '@id' => resource_representation(resource.subject, false, extendedRepresentation) }
|
200
|
+
if resource.predicate == RDF.type
|
201
|
+
rep['@id']['@type'] = resource.object.to_s
|
202
|
+
else
|
203
|
+
rep['@id'][resource.predicate.to_s] =
|
204
|
+
as_array(resource_representation(resource.object, useNativeTypes, extendedRepresentation))
|
205
|
+
end
|
206
|
+
rep
|
207
|
+
when RDF::Literal
|
208
|
+
base = @options[:base]
|
209
|
+
rdfDirection = @options[:rdfDirection]
|
210
|
+
res = {}
|
211
|
+
|
212
|
+
if resource.datatype == RDF_JSON && @context.processingMode('json-ld-1.1')
|
213
|
+
res['@type'] = '@json'
|
214
|
+
res['@value'] = begin
|
215
|
+
::JSON.parse(resource.object)
|
216
|
+
rescue ::JSON::ParserError => e
|
217
|
+
raise JSON::LD::JsonLdError::InvalidJsonLiteral, e.message
|
218
|
+
end
|
219
|
+
elsif useNativeTypes && extendedRepresentation
|
220
|
+
res['@value'] = resource # Raw literal
|
221
|
+
elsif resource.datatype.start_with?("https://www.w3.org/ns/i18n#") && rdfDirection == 'i18n-datatype' && @context.processingMode('json-ld-1.1')
|
222
|
+
lang, dir = resource.datatype.fragment.split('_')
|
223
|
+
res['@value'] = resource.to_s
|
224
|
+
unless lang.empty?
|
225
|
+
unless /^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$/.match?(lang)
|
226
|
+
if options[:validate]
|
227
|
+
raise JsonLdError::InvalidLanguageMapping, "rdf:language must be valid BCP47: #{lang.inspect}"
|
228
|
+
end
|
229
|
+
|
230
|
+
warn "rdf:language must be valid BCP47: #{lang.inspect}"
|
231
|
+
|
232
|
+
end
|
233
|
+
res['@language'] = lang
|
234
|
+
end
|
235
|
+
res['@direction'] = dir
|
236
|
+
elsif useNativeTypes && RDF_LITERAL_NATIVE_TYPES.include?(resource.datatype) && resource.valid?
|
237
|
+
res['@value'] = resource.object
|
238
|
+
else
|
239
|
+
resource.canonicalize! if resource.valid? && resource.datatype == RDF::XSD.double
|
240
|
+
if resource.datatype?
|
241
|
+
res['@type'] = resource.datatype.to_s
|
242
|
+
elsif resource.language?
|
243
|
+
res['@language'] = resource.language.to_s
|
244
|
+
end
|
245
|
+
res['@value'] = resource.to_s
|
246
|
+
end
|
247
|
+
res
|
179
248
|
else
|
180
|
-
|
181
|
-
resource.predicate.to_s =>
|
182
|
-
as_array(resource_representation(resource.object, useNativeTypes)))
|
249
|
+
{ '@id' => resource.to_s }
|
183
250
|
end
|
184
|
-
rep
|
185
|
-
when RDF::Literal
|
186
|
-
@context.expand_value(nil,
|
187
|
-
resource,
|
188
|
-
rdfDirection: @options[:rdfDirection],
|
189
|
-
useNativeTypes: useNativeTypes,
|
190
|
-
base: @options[:base])
|
191
|
-
else
|
192
|
-
{'@id' => resource.to_s}
|
193
251
|
end
|
194
252
|
end
|
195
253
|
end
|