json-ld 3.0.1 → 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6f16239f3f6fc54a6a0e1e6a68d097ba97bb268893f3160df3fe86b4ac6170e
4
- data.tar.gz: f21243bfcf58a41dd1a091ac9a92664be80e4e8a813ed9630285f1f96ead4f4e
3
+ metadata.gz: 970e177e4a776881601b87546f04cfad8c616b533d322a985f6ce588c5572c19
4
+ data.tar.gz: 9d8388137478a542d0e42f143ed36d90efe27fa7ff649410af35d651fafce836
5
5
  SHA512:
6
- metadata.gz: 9890153889067a1d87707aac9b52cc6b914dcf0372253d51ba3e496b97f87f5b0bfaa07091513c1f3e3e8b23852151f7b221ef21e5d83e94f59cff3079120fd9
7
- data.tar.gz: 0e6240f1d3e02b145a7a786a14770c181700e777d99bf7af924d50af221074ccf2e0fb20659cfc5c061ef9c5550acb397c433b029f115f1eba89dd25aa60378e
6
+ metadata.gz: 70b363dafd7632225a082e0ea5113ceba39779e27593f25f18f78df62bed9a202b0f2a62627096186113888cbd5bf3583e4f33b85cf94d1bbf8ca10d1d02059f
7
+ data.tar.gz: c6186a16b4fac554ff6893926f23c729fb1194ab3423e801d9d40915c978330a39c251645bb8775e1d46a94add2cf9f905c579f47085d2d42aafb1cd04971a61
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.1
1
+ 3.0.2
@@ -4,6 +4,9 @@ module JSON::LD
4
4
  module Compact
5
5
  include Utils
6
6
 
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
+
7
10
  ##
8
11
  # 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.
9
12
  #
@@ -234,7 +237,7 @@ module JSON::LD
234
237
  add_value(nest_result, item_active_property, compacted_item,
235
238
  property_is_array: as_array)
236
239
  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')
238
241
  map_object = nest_result[item_active_property] ||= {}
239
242
  c = container.first
240
243
  container_key = context.compact_iri(c, vocab: true, quiet: true)
@@ -527,6 +527,11 @@ module JSON::LD
527
527
  self
528
528
  end
529
529
 
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
+
530
535
  ##
531
536
  # Create Term Definition
532
537
  #
@@ -570,7 +575,7 @@ module JSON::LD
570
575
  value = {'@id' => value} if simple_term
571
576
 
572
577
  case value
573
- when nil, {'@id' => nil}
578
+ when nil, ID_NULL_OBJECT
574
579
  # 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.
575
580
  #log_debug("") {"=> nil"}
576
581
  term_definitions[term] = TermDefinition.new(term)
@@ -581,14 +586,16 @@ module JSON::LD
581
586
  definition = TermDefinition.new(term)
582
587
  definition.simple = simple_term
583
588
 
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
588
594
 
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
592
599
  end
593
600
 
594
601
  if value.has_key?('@type')
@@ -650,7 +657,7 @@ module JSON::LD
650
657
 
651
658
  # If id ends with a gen-delim, it may be used as a prefix for simple terms
652
659
  definition.prefix = true if !term.include?(':') &&
653
- definition.id.to_s.end_with?(*%w(: / ? # [ ] @)) &&
660
+ definition.id.to_s.end_with?(':', '/', '?', '#', '[', ']', '@') &&
654
661
  simple_term
655
662
  elsif term.include?(':')
656
663
  # 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.
@@ -347,7 +347,7 @@ module JSON::LD
347
347
  term_context = context.term_definitions[key].context if context.term_definitions[key]
348
348
  active_context = term_context ? context.parse(term_context) : context
349
349
  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)
351
351
  # 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:
352
352
 
353
353
  # Set multilingual array to an empty array.
@@ -370,7 +370,7 @@ module JSON::LD
370
370
  end
371
371
 
372
372
  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)
374
374
  # 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:
375
375
 
376
376
  # Set ary to an empty array.
@@ -426,14 +426,14 @@ module JSON::LD
426
426
  #log_debug {" => #{expanded_value.inspect}"}
427
427
 
428
428
  # 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)
430
430
  #log_debug(" => ") { "convert #{expanded_value.inspect} to list"}
431
431
  expanded_value = {'@list' => as_array(expanded_value)}
432
432
  end
433
433
  #log_debug {" => #{expanded_value.inspect}"}
434
434
 
435
435
  # 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
437
437
  #log_debug(" => ") { "convert #{expanded_value.inspect} to list"}
438
438
  expanded_value = as_array(expanded_value).map do |v|
439
439
  graph?(v) ? v : {'@graph' => as_array(v)}
@@ -457,7 +457,14 @@ module JSON::LD
457
457
  else
458
458
  # Otherwise, if key is not a reverse property:
459
459
  # 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
461
468
  end
462
469
  end
463
470
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-ld
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregg Kellogg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-09 00:00:00.000000000 Z
11
+ date: 2018-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdf