Skip to content

Commit 82882c7

Browse files
committed
Make renaming bnodes default to false for Reader and to_rdf_spec.
1 parent fbc9f7b commit 82882c7

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

lib/json/ld/api.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,15 @@ class API
102102
# @yield [api]
103103
# @yieldparam [API]
104104
# @raise [JsonLdError]
105-
def initialize(input, context, rename_bnodes: true, unique_bnodes: false, **options, &block)
105+
def initialize(input, context, **options, &block)
106106
@options = {
107107
compactArrays: true,
108108
ordered: false,
109109
extractAllScripts: false,
110+
rename_bnodes: true,
111+
unique_bnodes: false,
110112
}.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)
112114

113115
@options[:base] = RDF::URI(@options[:base]) if @options[:base] && !@options[:base].is_a?(RDF::URI)
114116
# For context via Link header
@@ -202,9 +204,9 @@ def self.expand(input, framing: false, **options, &block)
202204
# The JSON-LD object to copy and perform the compaction upon.
203205
# @param [String, #read, Hash, Array, JSON::LD::Context] context
204206
# The base context to use when compacting the input.
207+
# @param [Boolean] expanded (false) Input is already expanded
205208
# @param [Hash{Symbol => Object}] options
206209
# @option options (see #initialize)
207-
# @option options [Boolean] :expanded Input is already expanded
208210
# @yield jsonld
209211
# @yieldparam [Hash] jsonld
210212
# The compacted JSON-LD document
@@ -248,9 +250,9 @@ def self.compact(input, context, expanded: false, **options)
248250
# The JSON-LD object or array of JSON-LD objects to flatten or an IRI referencing the JSON-LD document to flatten.
249251
# @param [String, #read, Hash, Array, JSON::LD::EvaluationContext] context
250252
# 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
251254
# @param [Hash{Symbol => Object}] options
252255
# @option options (see #initialize)
253-
# @option options [Boolean] :expanded Input is already expanded
254256
# @yield jsonld
255257
# @yieldparam [Hash] jsonld
256258
# The flattened JSON-LD document
@@ -275,6 +277,9 @@ def self.flatten(input, context, expanded: false, **options)
275277
API.new(expanded_input, context, no_default_base: true, **options) do
276278
log_debug(".flatten") {"expanded input: #{value.to_json(JSON_STATE) rescue 'malformed json'}"}
277279

280+
# 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.
281+
#@value = rename_bnodes(@value) if @options[:rename_bnodes]
282+
278283
# Initialize node map to a JSON object consisting of a single member whose key is @default and whose value is an empty JSON object.
279284
graph_maps = {'@default' => {}}
280285
create_node_map(value, graph_maps)
@@ -316,6 +321,7 @@ def self.flatten(input, context, expanded: false, **options)
316321
# The JSON-LD object to copy and perform the framing on.
317322
# @param [String, #read, Hash, Array] frame
318323
# The frame to use when re-arranging the data.
324+
# @param [Boolean] expanded (false) Input is already expanded
319325
# @option options (see #initialize)
320326
# @option options ['@always', '@link', '@once', '@never'] :embed ('@once')
321327
# a flag specifying that objects should be directly embedded in the output, instead of being referred to by their IRI.
@@ -325,7 +331,6 @@ def self.flatten(input, context, expanded: false, **options)
325331
# 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.
326332
# @option options [Boolean] :omitDefault (false)
327333
# 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
329334
# @option options [Boolean] :pruneBlankNodeIdentifiers (true) removes blank node identifiers that are only used once.
330335
# @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`.
331336
# @yield jsonld
@@ -458,10 +463,10 @@ def self.frame(input, frame, expanded: false, **options)
458463
#
459464
# @param [String, #read, Hash, Array] input
460465
# The JSON-LD object to process when outputting statements.
466+
# @param [Boolean] expanded (false) Input is already expanded
461467
# @option options (see #initialize)
462468
# @option options [Boolean] :produceGeneralizedRdf (false)
463469
# If true, output will include statements having blank node predicates, otherwise they are dropped.
464-
# @option options [Boolean] :expanded Input is already expanded
465470
# @raise [JsonLdError]
466471
# @yield statement
467472
# @yieldparam [RDF::Statement] statement
@@ -470,7 +475,7 @@ def self.toRdf(input, expanded: false, **options, &block)
470475
unless block_given?
471476
results = []
472477
results.extend(RDF::Enumerable)
473-
self.toRdf(input, **options) do |stmt|
478+
self.toRdf(input, expanded: expanded, **options) do |stmt|
474479
results << stmt
475480
end
476481
return results

lib/json/ld/reader.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def self.options
6868
# @raise [RDF::ReaderError] if the JSON document cannot be loaded
6969
def initialize(input = $stdin, **options, &block)
7070
options[:base_uri] ||= options[:base]
71+
options[:rename_bnodes] = false unless options.key?(:rename_bnodes)
7172
super do
7273
@options[:base] ||= base_uri.to_s if base_uri
7374
# Trim non-JSON stuff in script.

spec/suite_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def run(rspec_example = nil)
237237
repo << statement
238238
end
239239
else
240-
JSON::LD::API.toRdf(input_loc, logger: logger, **options) do |statement|
240+
JSON::LD::API.toRdf(input_loc, rename_bnodes: false, logger: logger, **options) do |statement|
241241
repo << statement
242242
end
243243
end
@@ -327,7 +327,7 @@ def run(rspec_example = nil)
327327
if t.manifest_url.to_s.include?('stream')
328328
JSON::LD::Reader.open(t.input_loc, stream: true, logger: logger, **options).each_statement {}
329329
else
330-
JSON::LD::API.toRdf(t.input_loc, logger: logger, **options) {}
330+
JSON::LD::API.toRdf(t.input_loc, rename_bnodes: false, logger: logger, **options) {}
331331
end
332332
else
333333
success("Unknown test type: #{testType}")

spec/to_rdf_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@
15281528
def parse(input, **options)
15291529
graph = options[:graph] || RDF::Graph.new
15301530
options = {logger: logger, validate: true, canonicalize: false}.merge(options)
1531-
JSON::LD::API.toRdf(StringIO.new(input), **options) {|st| graph << st}
1531+
JSON::LD::API.toRdf(StringIO.new(input), rename_bnodes: false, **options) {|st| graph << st}
15321532
graph
15331533
end
15341534

@@ -1541,9 +1541,9 @@ def run_to_rdf(params)
15411541
expect {JSON::LD::API.toRdf(input, **params)}.to raise_error(params[:exception])
15421542
else
15431543
if params[:write]
1544-
expect{JSON::LD::API.toRdf(input, base: params[:base], logger: logger, **params) {|st| graph << st}}.to write(params[:write]).to(:error)
1544+
expect{JSON::LD::API.toRdf(input, base: params[:base], logger: logger, rename_bnodes: false, **params) {|st| graph << st}}.to write(params[:write]).to(:error)
15451545
else
1546-
expect{JSON::LD::API.toRdf(input, base: params[:base], logger: logger, **params) {|st| graph << st}}.not_to write.to(:error)
1546+
expect{JSON::LD::API.toRdf(input, base: params[:base], logger: logger, rename_bnodes: false, **params) {|st| graph << st}}.not_to write.to(:error)
15471547
end
15481548
expect(graph).to be_equivalent_graph(output, logger: logger, inputDocument: input)
15491549
end

0 commit comments

Comments
 (0)