json-ld 3.1.7 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -10
- data/VERSION +1 -1
- data/bin/jsonld +13 -11
- data/lib/json/ld/api.rb +34 -17
- data/lib/json/ld/compact.rb +6 -5
- data/lib/json/ld/context.rb +63 -57
- data/lib/json/ld/expand.rb +57 -19
- data/lib/json/ld/extensions.rb +4 -4
- data/lib/json/ld/flatten.rb +137 -9
- data/lib/json/ld/format.rb +8 -1
- data/lib/json/ld/frame.rb +8 -8
- data/lib/json/ld/from_rdf.rb +14 -8
- data/lib/json/ld/html/nokogiri.rb +3 -4
- data/lib/json/ld/reader.rb +1 -0
- data/lib/json/ld/streaming_reader.rb +5 -5
- data/lib/json/ld/streaming_writer.rb +4 -4
- data/lib/json/ld/to_rdf.rb +2 -2
- data/lib/json/ld/utils.rb +13 -13
- data/lib/json/ld/writer.rb +2 -2
- data/lib/json/ld.rb +3 -1
- data/spec/api_spec.rb +1 -1
- data/spec/compact_spec.rb +72 -2
- data/spec/context_spec.rb +8 -3
- data/spec/expand_spec.rb +385 -2
- data/spec/flatten_spec.rb +517 -1
- data/spec/frame_spec.rb +93 -3
- data/spec/from_rdf_spec.rb +1 -1
- data/spec/rdfstar_spec.rb +25 -0
- data/spec/spec_helper.rb +40 -2
- data/spec/suite_flatten_spec.rb +4 -0
- data/spec/suite_frame_spec.rb +7 -0
- data/spec/suite_helper.rb +13 -7
- data/spec/suite_to_rdf_spec.rb +1 -1
- data/spec/to_rdf_spec.rb +4 -4
- data/spec/writer_spec.rb +1 -1
- metadata +88 -86
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e157972b224a8cbe834defdf32656a0b9f090357760fab179076e728427d1639
|
4
|
+
data.tar.gz: a63cd584853e5a830c85a427d30711bc84999e958949c81bc7d236df20f14ee6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77a85aa85eea6844e1adb9107fe0b2d5a4ad82b877456adcdbbc56031f884da21cc992169232c326f255491d8aa6f607aacbeaeb386578fe8a1ac374cc795ff5
|
7
|
+
data.tar.gz: 2f8353dca7bb6ea9bed3ed7fbea3e0bf883bdec9a3687d3b084f936ae0171a21df1d55bda8cf44fc0ae6d6682d5235234792901fb2fc7ffed327d2956fec289f
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
[![Gem Version](https://badge.fury.io/rb/json-ld.png)](https://rubygems.org/gems/json-ld)
|
6
6
|
[![Build Status](https://secure.travis-ci.org/ruby-rdf/json-ld.png?branch=develop)](https://github.com/ruby-rdf/json-ld/actions?query=workflow%3ACI)
|
7
|
-
[![Coverage Status](https://coveralls.io/repos/ruby-rdf/json-ld/badge.svg)](https://coveralls.io/github/ruby-rdf/json-ld)
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/ruby-rdf/json-ld/badge.svg?branch=develop)](https://coveralls.io/github/ruby-rdf/json-ld?branch=develop)
|
8
8
|
[![Gitter chat](https://badges.gitter.im/ruby-rdf.png)](https://gitter.im/gitterHQ/gitter)
|
9
9
|
|
10
10
|
## Features
|
@@ -14,10 +14,10 @@ JSON::LD parses and serializes [JSON-LD][] into [RDF][] and implements expansion
|
|
14
14
|
JSON::LD can now be used to create a _context_ from an RDFS/OWL definition, and optionally include a JSON-LD representation of the ontology itself. This is currently accessed through the `script/gen_context` script.
|
15
15
|
|
16
16
|
* If the [jsonlint][] gem is installed, it will be used when validating an input document.
|
17
|
-
* If available, uses [Nokogiri][]
|
18
|
-
* Provisional support for [JSON-LD
|
17
|
+
* If available, uses [Nokogiri][] for parsing HTML, falls back to REXML otherwise.
|
18
|
+
* Provisional support for [JSON-LD-star][JSON-LD-star].
|
19
19
|
|
20
|
-
[Implementation Report](
|
20
|
+
[Implementation Report](https://ruby-rdf.github.io/json-ld/etc/earl.html)
|
21
21
|
|
22
22
|
Install with `gem install json-ld`
|
23
23
|
|
@@ -37,9 +37,9 @@ The order of triples retrieved from the `RDF::Enumerable` dataset determines the
|
|
37
37
|
### MultiJson parser
|
38
38
|
The [MultiJson](https://rubygems.org/gems/multi_json) gem is used for parsing JSON; this defaults to the native JSON parser, but will use a more performant parser if one is available. A specific parser can be specified by adding the `:adapter` option to any API call. See [MultiJson](https://rubygems.org/gems/multi_json) for more information.
|
39
39
|
|
40
|
-
### JSON-LD
|
40
|
+
### JSON-LD-star (RDFStar)
|
41
41
|
|
42
|
-
The {JSON::LD::API.toRdf} and {JSON::LD::API.fromRdf} API methods, along with the {JSON::LD::Reader} and {JSON::LD::Writer}, include provisional support for [JSON-LD
|
42
|
+
The {JSON::LD::API.expand}, {JSON::LD::API.compact}, {JSON::LD::API.toRdf}, and {JSON::LD::API.fromRdf} API methods, along with the {JSON::LD::Reader} and {JSON::LD::Writer}, include provisional support for [JSON-LD-star][JSON-LD-star].
|
43
43
|
|
44
44
|
Internally, an `RDF::Statement` is treated as another resource, along with `RDF::URI` and `RDF::Node`, which allows an `RDF::Statement` to have a `#subject` or `#object` which is also an `RDF::Statement`.
|
45
45
|
|
@@ -55,6 +55,19 @@ In JSON-LD, with the `rdfstar` option set, the value of `@id`, in addition to an
|
|
55
55
|
"ex:certainty": 0.9
|
56
56
|
}
|
57
57
|
|
58
|
+
Additionally, the `@annotation` property (or alias) may be used on a node object or value object to annotate the statement for which the associated node is the object of a triple.
|
59
|
+
|
60
|
+
{
|
61
|
+
"@context": {"foaf": "http://xmlns.com/foaf/0.1/"},
|
62
|
+
"@id": "bob",
|
63
|
+
"foaf:age" 23,
|
64
|
+
"@annotation": {
|
65
|
+
"ex:certainty": 0.9
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
In the first case, the embedded node is not asserted, and only appears as the subject of a triple. In the second case, the triple is asserted and used as the subject in another statement which annotates it.
|
70
|
+
|
58
71
|
**Note: This feature is subject to change or elimination as the standards process progresses.**
|
59
72
|
|
60
73
|
#### Serializing a Graph containing embedded statements
|
@@ -575,9 +588,9 @@ Note, the API method signatures differed in versions before 1.0, in that they al
|
|
575
588
|
* {JSON::LD::Writer}
|
576
589
|
|
577
590
|
## Dependencies
|
578
|
-
* [Ruby](https://ruby-lang.org/) (>= 2.
|
579
|
-
* [RDF.rb](https://rubygems.org/gems/rdf) (~> 3.
|
580
|
-
* [JSON](https://rubygems.org/gems/json) (>= 2.
|
591
|
+
* [Ruby](https://ruby-lang.org/) (>= 2.6)
|
592
|
+
* [RDF.rb](https://rubygems.org/gems/rdf) (~> 3.2)
|
593
|
+
* [JSON](https://rubygems.org/gems/json) (>= 2.6)
|
581
594
|
|
582
595
|
## Installation
|
583
596
|
The recommended installation method is via [RubyGems](https://rubygems.org/).
|
@@ -623,7 +636,7 @@ see <https://unlicense.org/> or the accompanying {file:UNLICENSE} file.
|
|
623
636
|
[YARD-GS]: https://rubydoc.info/docs/yard/file/docs/GettingStarted.md
|
624
637
|
[PDD]: https://unlicense.org/#unlicensing-contributions
|
625
638
|
[RDF.rb]: https://rubygems.org/gems/rdf
|
626
|
-
[JSON-LD
|
639
|
+
[JSON-LD-star]: https://json-ld.github.io/json-ld-star/
|
627
640
|
[Rack::LinkedData]: https://rubygems.org/gems/rack-linkeddata
|
628
641
|
[Backports]: https://rubygems.org/gems/backports
|
629
642
|
[JSON-LD]: https://www.w3.org/TR/json-ld11/ "JSON-LD 1.1"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.
|
1
|
+
3.2.0
|
data/bin/jsonld
CHANGED
@@ -24,36 +24,36 @@ def run(input, options, parser_options)
|
|
24
24
|
|
25
25
|
start = Time.new
|
26
26
|
if options[:expand]
|
27
|
-
parser_options = parser_options.merge(expandContext: parser_options.delete(:context)) if parser_options.
|
27
|
+
parser_options = parser_options.merge(expandContext: parser_options.delete(:context)) if parser_options.key?(:context)
|
28
28
|
input = JSON::LD::API.fromRdf(reader) if reader
|
29
|
-
output = JSON::LD::API.expand(input, parser_options)
|
29
|
+
output = JSON::LD::API.expand(input, **parser_options)
|
30
30
|
secs = Time.new - start
|
31
31
|
options[:output].puts output.to_json(JSON::LD::JSON_STATE)
|
32
32
|
STDERR.puts "Expanded in #{secs} seconds." unless options[:quiet]
|
33
33
|
elsif options[:compact]
|
34
34
|
input = JSON::LD::API.fromRdf(reader) if reader
|
35
|
-
output = JSON::LD::API.compact(input, parser_options[:context], parser_options)
|
35
|
+
output = JSON::LD::API.compact(input, parser_options[:context], **parser_options)
|
36
36
|
secs = Time.new - start
|
37
37
|
options[:output].puts output.to_json(JSON::LD::JSON_STATE)
|
38
38
|
STDERR.puts "Compacted in #{secs} seconds." unless options[:quiet]
|
39
39
|
elsif options[:flatten]
|
40
40
|
input = JSON::LD::API.fromRdf(reader) if reader
|
41
|
-
output = JSON::LD::API.flatten(input, parser_options[:context], parser_options)
|
41
|
+
output = JSON::LD::API.flatten(input, parser_options[:context], **parser_options)
|
42
42
|
secs = Time.new - start
|
43
43
|
options[:output].puts output.to_json(JSON::LD::JSON_STATE)
|
44
44
|
STDERR.puts "Flattened in #{secs} seconds." unless options[:quiet]
|
45
45
|
elsif options[:frame]
|
46
46
|
input = JSON::LD::API.fromRdf(reader) if reader
|
47
|
-
output = JSON::LD::API.frame(input, parser_options[:frame], parser_options)
|
47
|
+
output = JSON::LD::API.frame(input, parser_options[:frame], **parser_options)
|
48
48
|
secs = Time.new - start
|
49
49
|
options[:output].puts output.to_json(JSON::LD::JSON_STATE)
|
50
50
|
STDERR.puts "Framed in #{secs} seconds." unless options[:quiet]
|
51
51
|
else
|
52
|
-
parser_options = parser_options.merge(expandContext: parser_options.delete(:context)) if parser_options.
|
52
|
+
parser_options = parser_options.merge(expandContext: parser_options.delete(:context)) if parser_options.key?(:context)
|
53
53
|
parser_options[:standard_prefixes] = true
|
54
|
-
reader ||= JSON::LD::Reader.new(input, parser_options)
|
54
|
+
reader ||= JSON::LD::Reader.new(input, **parser_options)
|
55
55
|
num = 0
|
56
|
-
RDF::Writer.for(options[:output_format]).new(options[:output], parser_options) do |w|
|
56
|
+
RDF::Writer.for(options[:output_format]).new(options[:output], **parser_options) do |w|
|
57
57
|
reader.each do |statement|
|
58
58
|
num += 1
|
59
59
|
w << statement
|
@@ -113,6 +113,7 @@ OPT_ARGS = [
|
|
113
113
|
["--processingMode",GetoptLong::REQUIRED_ARGUMENT,"Set processing mode, defaults to json-ld-1.1"],
|
114
114
|
["--quiet", GetoptLong::NO_ARGUMENT, "Supress most output other than progress indicators"],
|
115
115
|
["--rename_bnodes", GetoptLong::OPTIONAL_ARGUMENT,"Rename bnodes as part of expansion, or keep them the same"],
|
116
|
+
["--rdfstar", GetoptLong::NO_ARGUMENT, "Parse JSON-LD-star"],
|
116
117
|
["--requireAll", GetoptLong::OPTIONAL_ARGUMENT,"Rename bnodes as part of expansion, or keep them the same"],
|
117
118
|
["--stream", GetoptLong::NO_ARGUMENT, "Use Streaming reader/writer"],
|
118
119
|
["--unique_bnodes", GetoptLong::OPTIONAL_ARGUMENT,"Use unique bnode identifiers"],
|
@@ -140,7 +141,7 @@ opts = GetoptLong.new(*OPT_ARGS.map {|o| o[0..-2]})
|
|
140
141
|
opts.each do |opt, arg|
|
141
142
|
case opt
|
142
143
|
when '--debug' then logger.level = Logger::DEBUG
|
143
|
-
when '--compact' then
|
144
|
+
when '--compact' then options[:compact] = true
|
144
145
|
when "--compactArrays" then parser_options[:compactArrays] = (arg || 'true') == 'true'
|
145
146
|
when '--context' then parser_options[:context] = RDF::URI(arg).absolute? ? arg : File.open(arg)
|
146
147
|
when '--evaluate' then input = arg
|
@@ -158,6 +159,7 @@ opts.each do |opt, arg|
|
|
158
159
|
when '--quiet'
|
159
160
|
options[:quiet] = true
|
160
161
|
logger.level = Logger::FATAL
|
162
|
+
when "--rdfstar" then parser_options[:rdfstar] = true
|
161
163
|
when "--rename_bnodes" then parser_options[:rename_bnodes] = (arg || 'true') == 'true'
|
162
164
|
when "--requireAll" then parser_options[:requireAll] = (arg || 'true') == 'true'
|
163
165
|
when '--stream' then parser_options[:stream] = true
|
@@ -181,7 +183,7 @@ opts.each do |opt, arg|
|
|
181
183
|
end
|
182
184
|
|
183
185
|
# Hack
|
184
|
-
if !(options.keys &
|
186
|
+
if !(options.keys & %i{expand compact flatten frame}).empty? &&
|
185
187
|
(parser_options[:stream] || options[:output_format] != :jsonld)
|
186
188
|
STDERR.puts "Incompatible options"
|
187
189
|
exit(1)
|
@@ -193,7 +195,7 @@ if ARGV.empty?
|
|
193
195
|
else
|
194
196
|
ARGV.each do |file|
|
195
197
|
# Call with opened files
|
196
|
-
RDF::Util::File.open_file(file, options) {|f| run(f, options, parser_options)}
|
198
|
+
RDF::Util::File.open_file(file, **options) {|f| run(f, options, parser_options)}
|
197
199
|
end
|
198
200
|
end
|
199
201
|
puts
|
data/lib/json/ld/api.rb
CHANGED
@@ -90,7 +90,7 @@ module JSON::LD
|
|
90
90
|
# Processing mode, json-ld-1.0 or json-ld-1.1.
|
91
91
|
# If `processingMode` is not specified, a mode of `json-ld-1.0` or `json-ld-1.1` is set, the context used for `expansion` or `compaction`.
|
92
92
|
# @option options [Boolean] rdfstar (false)
|
93
|
-
# support parsing JSON-LD
|
93
|
+
# support parsing JSON-LD-star statement resources.
|
94
94
|
# @option options [Boolean] :rename_bnodes (true)
|
95
95
|
# Rename bnodes as part of expansion, or keep them the same.
|
96
96
|
# @option options [Boolean] :unique_bnodes (false)
|
@@ -102,13 +102,15 @@ module JSON::LD
|
|
102
102
|
# @yield [api]
|
103
103
|
# @yieldparam [API]
|
104
104
|
# @raise [JsonLdError]
|
105
|
-
def initialize(input, context,
|
105
|
+
def initialize(input, context, **options, &block)
|
106
106
|
@options = {
|
107
107
|
compactArrays: true,
|
108
108
|
ordered: false,
|
109
109
|
extractAllScripts: false,
|
110
|
+
rename_bnodes: true,
|
111
|
+
unique_bnodes: false,
|
110
112
|
}.merge(options)
|
111
|
-
@namer = unique_bnodes ? BlankNodeUniqer.new : (rename_bnodes ? BlankNodeNamer.new("b") : BlankNodeMapper.new)
|
113
|
+
@namer = @options[:unique_bnodes] ? BlankNodeUniqer.new : (@options[:rename_bnodes] ? BlankNodeNamer.new("b") : BlankNodeMapper.new)
|
112
114
|
|
113
115
|
@options[:base] = RDF::URI(@options[:base]) if @options[:base] && !@options[:base].is_a?(RDF::URI)
|
114
116
|
# For context via Link header
|
@@ -202,9 +204,9 @@ module JSON::LD
|
|
202
204
|
# The JSON-LD object to copy and perform the compaction upon.
|
203
205
|
# @param [String, #read, Hash, Array, JSON::LD::Context] context
|
204
206
|
# The base context to use when compacting the input.
|
207
|
+
# @param [Boolean] expanded (false) Input is already expanded
|
205
208
|
# @param [Hash{Symbol => Object}] options
|
206
209
|
# @option options (see #initialize)
|
207
|
-
# @option options [Boolean] :expanded Input is already expanded
|
208
210
|
# @yield jsonld
|
209
211
|
# @yieldparam [Hash] jsonld
|
210
212
|
# The compacted JSON-LD document
|
@@ -248,9 +250,11 @@ module JSON::LD
|
|
248
250
|
# The JSON-LD object or array of JSON-LD objects to flatten or an IRI referencing the JSON-LD document to flatten.
|
249
251
|
# @param [String, #read, Hash, Array, JSON::LD::EvaluationContext] context
|
250
252
|
# An optional external context to use additionally to the context embedded in input when expanding the input.
|
253
|
+
# @param [Boolean] expanded (false) Input is already expanded
|
251
254
|
# @param [Hash{Symbol => Object}] options
|
252
255
|
# @option options (see #initialize)
|
253
|
-
# @option options [Boolean] :
|
256
|
+
# @option options [Boolean] :createAnnotations
|
257
|
+
# Unfold embedded nodes which can be represented using `@annotation`.
|
254
258
|
# @yield jsonld
|
255
259
|
# @yieldparam [Hash] jsonld
|
256
260
|
# The flattened JSON-LD document
|
@@ -275,10 +279,20 @@ module JSON::LD
|
|
275
279
|
API.new(expanded_input, context, no_default_base: true, **options) do
|
276
280
|
log_debug(".flatten") {"expanded input: #{value.to_json(JSON_STATE) rescue 'malformed json'}"}
|
277
281
|
|
282
|
+
# Rename blank nodes recusively. Note that this does not create new blank node identifiers where none exist, which is performed in the node map generation algorithm.
|
283
|
+
@value = rename_bnodes(@value) if @options[:rename_bnodes]
|
284
|
+
|
278
285
|
# Initialize node map to a JSON object consisting of a single member whose key is @default and whose value is an empty JSON object.
|
279
286
|
graph_maps = {'@default' => {}}
|
280
287
|
create_node_map(value, graph_maps)
|
281
288
|
|
289
|
+
# If create annotations flag is set, then update each node map in graph maps with the result of calling the create annotations algorithm.
|
290
|
+
if options[:createAnnotations]
|
291
|
+
graph_maps.values.each do |node_map|
|
292
|
+
create_annotations(node_map)
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
282
296
|
default_graph = graph_maps['@default']
|
283
297
|
graph_maps.keys.opt_sort(ordered: @options[:ordered]).each do |graph_name|
|
284
298
|
next if graph_name == '@default'
|
@@ -297,7 +311,7 @@ module JSON::LD
|
|
297
311
|
if context && !flattened.empty?
|
298
312
|
# Otherwise, return the result of compacting flattened according the Compaction algorithm passing context ensuring that the compaction result uses the @graph keyword (or its alias) at the top-level, even if the context is empty or if there is only one element to put in the @graph array. This ensures that the returned document has a deterministic structure.
|
299
313
|
compacted = as_array(compact(flattened))
|
300
|
-
kwgraph = self.context.compact_iri('@graph')
|
314
|
+
kwgraph = self.context.compact_iri('@graph', vocab: true)
|
301
315
|
flattened = self.context.
|
302
316
|
serialize(provided_context: context).
|
303
317
|
merge(kwgraph => compacted)
|
@@ -316,6 +330,7 @@ module JSON::LD
|
|
316
330
|
# The JSON-LD object to copy and perform the framing on.
|
317
331
|
# @param [String, #read, Hash, Array] frame
|
318
332
|
# The frame to use when re-arranging the data.
|
333
|
+
# @param [Boolean] expanded (false) Input is already expanded
|
319
334
|
# @option options (see #initialize)
|
320
335
|
# @option options ['@always', '@link', '@once', '@never'] :embed ('@once')
|
321
336
|
# a flag specifying that objects should be directly embedded in the output, instead of being referred to by their IRI.
|
@@ -325,7 +340,6 @@ module JSON::LD
|
|
325
340
|
# A flag specifying that all properties present in the input frame must either have a default value or be present in the JSON-LD input for the frame to match.
|
326
341
|
# @option options [Boolean] :omitDefault (false)
|
327
342
|
# a flag specifying that properties that are missing from the JSON-LD input should be omitted from the output.
|
328
|
-
# @option options [Boolean] :expanded Input is already expanded
|
329
343
|
# @option options [Boolean] :pruneBlankNodeIdentifiers (true) removes blank node identifiers that are only used once.
|
330
344
|
# @option options [Boolean] :omitGraph does not use `@graph` at top level unless necessary to describe multiple objects, defaults to `true` if processingMode is 1.1, otherwise `false`.
|
331
345
|
# @yield jsonld
|
@@ -391,10 +405,13 @@ module JSON::LD
|
|
391
405
|
end
|
392
406
|
|
393
407
|
# Set omitGraph option, if not present, based on processingMode
|
394
|
-
unless options.
|
408
|
+
unless options.key?(:omitGraph)
|
395
409
|
options[:omitGraph] = context.processingMode('json-ld-1.1')
|
396
410
|
end
|
397
411
|
|
412
|
+
# Rename blank nodes recusively. Note that this does not create new blank node identifiers where none exist, which is performed in the node map generation algorithm.
|
413
|
+
@value = rename_bnodes(@value)
|
414
|
+
|
398
415
|
# Get framing nodes from expanded input, replacing Blank Node identifiers as necessary
|
399
416
|
create_node_map(value, framing_state[:graphMap], active_graph: '@default')
|
400
417
|
|
@@ -415,7 +432,7 @@ module JSON::LD
|
|
415
432
|
frame(framing_state, framing_state[:subjects].keys.opt_sort(ordered: @options[:ordered]), (expanded_frame.first || {}), parent: result, **options)
|
416
433
|
|
417
434
|
# Default to based on processinMode
|
418
|
-
if !options.
|
435
|
+
if !options.key?(:pruneBlankNodeIdentifiers)
|
419
436
|
options[:pruneBlankNodeIdentifiers] = context.processingMode('json-ld-1.1')
|
420
437
|
end
|
421
438
|
|
@@ -440,7 +457,7 @@ module JSON::LD
|
|
440
457
|
result = if !compacted.is_a?(Array)
|
441
458
|
compacted
|
442
459
|
else
|
443
|
-
kwgraph = context.compact_iri('@graph')
|
460
|
+
kwgraph = context.compact_iri('@graph', vocab: true)
|
444
461
|
{kwgraph => compacted}
|
445
462
|
end
|
446
463
|
# Only add context if one was provided
|
@@ -458,10 +475,10 @@ module JSON::LD
|
|
458
475
|
#
|
459
476
|
# @param [String, #read, Hash, Array] input
|
460
477
|
# The JSON-LD object to process when outputting statements.
|
478
|
+
# @param [Boolean] expanded (false) Input is already expanded
|
461
479
|
# @option options (see #initialize)
|
462
480
|
# @option options [Boolean] :produceGeneralizedRdf (false)
|
463
481
|
# If true, output will include statements having blank node predicates, otherwise they are dropped.
|
464
|
-
# @option options [Boolean] :expanded Input is already expanded
|
465
482
|
# @raise [JsonLdError]
|
466
483
|
# @yield statement
|
467
484
|
# @yieldparam [RDF::Statement] statement
|
@@ -470,7 +487,7 @@ module JSON::LD
|
|
470
487
|
unless block_given?
|
471
488
|
results = []
|
472
489
|
results.extend(RDF::Enumerable)
|
473
|
-
self.toRdf(input, **options) do |stmt|
|
490
|
+
self.toRdf(input, expanded: expanded, **options) do |stmt|
|
474
491
|
results << stmt
|
475
492
|
end
|
476
493
|
return results
|
@@ -480,16 +497,16 @@ module JSON::LD
|
|
480
497
|
extractAllScripts: true,
|
481
498
|
}.merge(options)
|
482
499
|
|
483
|
-
#
|
484
|
-
|
500
|
+
# Flatten input to simplify processing
|
501
|
+
flattened_input = API.flatten(input, nil, expanded: expanded, ordered: false, **options)
|
485
502
|
|
486
|
-
API.new(
|
503
|
+
API.new(flattened_input, nil, **options) do
|
487
504
|
# 1) Perform the Expansion Algorithm on the JSON-LD input.
|
488
505
|
# This removes any existing context to allow the given context to be cleanly applied.
|
489
|
-
log_debug(".toRdf") {"
|
506
|
+
log_debug(".toRdf") {"flattened input: #{flattened_input.to_json(JSON_STATE) rescue 'malformed json'}"}
|
490
507
|
|
491
508
|
# Recurse through input
|
492
|
-
|
509
|
+
flattened_input.each do |node|
|
493
510
|
item_to_rdf(node) do |statement|
|
494
511
|
next if statement.predicate.node? && !options[:produceGeneralizedRdf]
|
495
512
|
|
data/lib/json/ld/compact.rb
CHANGED
@@ -64,7 +64,7 @@ module JSON::LD
|
|
64
64
|
log_debug("prop-scoped", depth: log_depth.to_i) {"context: #{self.context.inspect}"}
|
65
65
|
end
|
66
66
|
|
67
|
-
if element.key?('@id') || element.key?('@value')
|
67
|
+
if (element.key?('@id') || element.key?('@value')) && !element.key?('@annotation')
|
68
68
|
result = context.compact_value(property, element, base: @options[:base])
|
69
69
|
if !result.is_a?(Hash) || context.coerce(property) == '@json'
|
70
70
|
log_debug("", depth: log_depth.to_i) {"=> scalar result: #{result.inspect}"}
|
@@ -100,7 +100,7 @@ module JSON::LD
|
|
100
100
|
if expanded_property == '@id'
|
101
101
|
compacted_value = as_array(expanded_value).map do |expanded_id|
|
102
102
|
if node?(expanded_id) && @options[:rdfstar]
|
103
|
-
# This can only really happen for valid RDF
|
103
|
+
# This can only really happen for valid RDF-star
|
104
104
|
compact(expanded_id, base: base,
|
105
105
|
property: '@id',
|
106
106
|
log_depth: log_depth.to_i + 1)
|
@@ -145,7 +145,7 @@ module JSON::LD
|
|
145
145
|
end
|
146
146
|
|
147
147
|
unless compacted_value.empty?
|
148
|
-
al = context.compact_iri('@reverse')
|
148
|
+
al = context.compact_iri('@reverse', vocab: true)
|
149
149
|
log_debug("", depth: log_depth.to_i) {"remainder: #{al} => #{compacted_value.inspect}"}
|
150
150
|
result[al] = compacted_value
|
151
151
|
end
|
@@ -231,7 +231,7 @@ module JSON::LD
|
|
231
231
|
unless container.include?('@list')
|
232
232
|
al = context.compact_iri('@list', vocab: true)
|
233
233
|
compacted_item = {al => compacted_item}
|
234
|
-
if expanded_item.
|
234
|
+
if expanded_item.key?('@index')
|
235
235
|
key = context.compact_iri('@index', vocab: true)
|
236
236
|
compacted_item[key] = expanded_item['@index']
|
237
237
|
end
|
@@ -276,7 +276,7 @@ module JSON::LD
|
|
276
276
|
al = context.compact_iri('@id', vocab: true)
|
277
277
|
compacted_item[al] = context.compact_iri(expanded_item['@id'], vocab: false)
|
278
278
|
end
|
279
|
-
if expanded_item.
|
279
|
+
if expanded_item.key?('@index')
|
280
280
|
key = context.compact_iri('@index', vocab: true)
|
281
281
|
compacted_item[key] = expanded_item['@index']
|
282
282
|
end
|
@@ -297,6 +297,7 @@ module JSON::LD
|
|
297
297
|
if index_key == '@index'
|
298
298
|
map_key = expanded_item['@index']
|
299
299
|
else
|
300
|
+
index_key = context.expand_iri(index_key, vocab: true)
|
300
301
|
container_key = context.compact_iri(index_key, vocab: true)
|
301
302
|
map_key, *others = Array(compacted_item[container_key])
|
302
303
|
if map_key.is_a?(String)
|