Skip to content

Commit f0b382c

Browse files
committed
Finish 3.0.2
2 parents d0c0587 + 5ef5716 commit f0b382c

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.1
1+
3.0.2

json-ld.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Gem::Specification.new do |gem|
2323

2424
gem.required_ruby_version = '>= 2.2.2'
2525
gem.requirements = []
26-
#gem.add_runtime_dependency 'rdf', '~> 3.0'
26+
#gem.add_runtime_dependency 'rdf', '~> 3.0', '>= 3.0.4'
2727
gem.add_runtime_dependency 'rdf', '>= 2.2.8', '< 4.0'
2828
gem.add_runtime_dependency 'multi_json', '~> 1.12'
2929
#gem.add_development_dependency 'linkeddata', '~> 2.2'

lib/json/ld/compact.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ module JSON::LD
44
module Compact
55
include Utils
66

7+
# The following constant is used to reduce object allocations in #compact below
8+
CONTAINER_MAPPING_LANGUAGE_INDEX_ID_TYPE = Set.new(%w(@language @index @id @type)).freeze
9+
710
##
811
# This algorithm compacts a JSON-LD document, such that the given context is applied. This must result in shortening any applicable IRIs to terms or compact IRIs, any applicable keywords to keyword aliases, and any applicable JSON-LD values expressed in expanded form to simple values such as strings or numbers.
912
#
@@ -234,7 +237,7 @@ def compact(element, property: nil, ordered: false)
234237
add_value(nest_result, item_active_property, compacted_item,
235238
property_is_array: as_array)
236239
end
237-
elsif !(container & %w(@language @index @id @type)).empty? && !container.include?('@graph')
240+
elsif container.any? { |key| CONTAINER_MAPPING_LANGUAGE_INDEX_ID_TYPE.include?(key) } && !container.include?('@graph')
238241
map_object = nest_result[item_active_property] ||= {}
239242
c = container.first
240243
container_key = context.compact_iri(c, vocab: true, quiet: true)

lib/json/ld/context.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@ def merge!(context)
527527
self
528528
end
529529

530+
# The following constants are used to reduce object allocations in #create_term_definition below
531+
ID_NULL_OBJECT = { '@id' => nil }.freeze
532+
JSON_LD_10_EXPECTED_KEYS = Set.new(%w(@container @id @language @reverse @type)).freeze
533+
JSON_LD_EXPECTED_KEYS = Set.new(%w(@container @context @id @language @nest @prefix @reverse @type)).freeze
534+
530535
##
531536
# Create Term Definition
532537
#
@@ -570,7 +575,7 @@ def create_term_definition(local_context, term, defined)
570575
value = {'@id' => value} if simple_term
571576

572577
case value
573-
when nil, {'@id' => nil}
578+
when nil, ID_NULL_OBJECT
574579
# If value equals null or value is a JSON object containing the key-value pair (@id-null), then set the term definition in active context to null, set the value associated with defined's key term to true, and return.
575580
#log_debug("") {"=> nil"}
576581
term_definitions[term] = TermDefinition.new(term)
@@ -581,14 +586,16 @@ def create_term_definition(local_context, term, defined)
581586
definition = TermDefinition.new(term)
582587
definition.simple = simple_term
583588

584-
expected_keys = case processingMode
585-
when "json-ld-1.0", nil then %w(@container @id @language @reverse @type)
586-
else %w(@container @context @id @language @nest @prefix @reverse @type)
587-
end
589+
if options[:validate]
590+
expected_keys = case processingMode
591+
when "json-ld-1.0", nil then JSON_LD_10_EXPECTED_KEYS
592+
else JSON_LD_EXPECTED_KEYS
593+
end
588594

589-
extra_keys = value.keys - expected_keys
590-
if !extra_keys.empty? && @options[:validate]
591-
raise JsonLdError::InvalidTermDefinition, "Term definition for #{term.inspect} has unexpected keys: #{extra_keys.join(', ')}"
595+
if value.any? { |key, _| !expected_keys.include?(key) }
596+
extra_keys = value.keys - expected_keys.to_a
597+
raise JsonLdError::InvalidTermDefinition, "Term definition for #{term.inspect} has unexpected keys: #{extra_keys.join(', ')}"
598+
end
592599
end
593600

594601
if value.has_key?('@type')
@@ -650,7 +657,7 @@ def create_term_definition(local_context, term, defined)
650657

651658
# If id ends with a gen-delim, it may be used as a prefix for simple terms
652659
definition.prefix = true if !term.include?(':') &&
653-
definition.id.to_s.end_with?(*%w(: / ? # [ ] @)) &&
660+
definition.id.to_s.end_with?(':', '/', '?', '#', '[', ']', '@') &&
654661
simple_term
655662
elsif term.include?(':')
656663
# If term is a compact IRI with a prefix that is a key in local context then a dependency has been found. Use this algorithm recursively passing active context, local context, the prefix as term, and defined.

lib/json/ld/expand.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def expand_object(input, active_property, context, output_object, ordered:, fram
347347
term_context = context.term_definitions[key].context if context.term_definitions[key]
348348
active_context = term_context ? context.parse(term_context) : context
349349
container = active_context.container(key)
350-
expanded_value = if container == %w(@language) && value.is_a?(Hash)
350+
expanded_value = if container.length == 1 && container.first == '@language' && value.is_a?(Hash)
351351
# Otherwise, if key's container mapping in active context is @language and value is a JSON object then value is expanded from a language map as follows:
352352

353353
# Set multilingual array to an empty array.
@@ -370,7 +370,7 @@ def expand_object(input, active_property, context, output_object, ordered:, fram
370370
end
371371

372372
ary
373-
elsif !(CONTAINER_MAPPING_INDEX_ID_TYPE & container).empty? && value.is_a?(Hash)
373+
elsif container.any? { |key| CONTAINER_MAPPING_INDEX_ID_TYPE.include?(key) } && value.is_a?(Hash)
374374
# Otherwise, if key's container mapping in active context contains @index, @id, @type and value is a JSON object then value is expanded from an index map as follows:
375375

376376
# Set ary to an empty array.
@@ -426,14 +426,14 @@ def expand_object(input, active_property, context, output_object, ordered:, fram
426426
#log_debug {" => #{expanded_value.inspect}"}
427427

428428
# If the container mapping associated to key in active context is @list and expanded value is not already a list object, convert expanded value to a list object by first setting it to an array containing only expanded value if it is not already an array, and then by setting it to a JSON object containing the key-value pair @list-expanded value.
429-
if active_context.container(key) == %w(@list) && !list?(expanded_value)
429+
if container.first == '@list' && container.length == 1 && !list?(expanded_value)
430430
#log_debug(" => ") { "convert #{expanded_value.inspect} to list"}
431431
expanded_value = {'@list' => as_array(expanded_value)}
432432
end
433433
#log_debug {" => #{expanded_value.inspect}"}
434434

435435
# convert expanded value to @graph if container specifies it
436-
if active_context.container(key) == %w(@graph)
436+
if container.first == '@graph' && container.length == 1
437437
#log_debug(" => ") { "convert #{expanded_value.inspect} to list"}
438438
expanded_value = as_array(expanded_value).map do |v|
439439
graph?(v) ? v : {'@graph' => as_array(v)}
@@ -457,7 +457,14 @@ def expand_object(input, active_property, context, output_object, ordered:, fram
457457
else
458458
# Otherwise, if key is not a reverse property:
459459
# If result does not have an expanded property member, create one and initialize its value to an empty array.
460-
(output_object[expanded_property] ||= []).concat([expanded_value].flatten)
460+
(output_object[expanded_property] ||= []).tap do |memo|
461+
# expanded_value is either Array[Hash] or Hash; in both case append to memo without flatten
462+
if expanded_value.is_a?(Array)
463+
memo.concat(expanded_value)
464+
else # Hash
465+
memo << expanded_value
466+
end
467+
end
461468
end
462469
end
463470

0 commit comments

Comments
 (0)