json-ld 2.1.3 → 2.1.4
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/bin/jsonld +18 -5
- data/lib/json/ld/api.rb +1 -1
- data/lib/json/ld/compact.rb +16 -7
- data/lib/json/ld/context.rb +74 -22
- data/lib/json/ld/expand.rb +0 -1
- data/lib/json/ld/frame.rb +8 -4
- data/spec/context_spec.rb +2 -13
- data/spec/suite_helper.rb +66 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d25a036d130e6e5442d6a1ce31847926874bce11
|
4
|
+
data.tar.gz: 4ef35668b4fd657f9fd2b26058fe336d20cbbb3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: def848f7294bc73b90d653579509c9e1dea81adb48b457bd85e30f307eecff4b416d57ef42f29d0aff5c11da8d979632a6c0e893b62fd66ba42dc52309e39ea1
|
7
|
+
data.tar.gz: 00e042aaebf1efa94822a21878c35046129469ceb9ec50dfea5923189b3c7ac7cc24a9a5bf99eeef74d5dbcc378dd6e2c3596343370163addd064ff758a4a316
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.1.
|
1
|
+
2.1.4
|
data/bin/jsonld
CHANGED
@@ -8,6 +8,7 @@ $:.unshift(File.expand_path("../../lib", __FILE__))
|
|
8
8
|
require 'json/ld'
|
9
9
|
require 'getoptlong'
|
10
10
|
require 'open-uri'
|
11
|
+
require 'logger'
|
11
12
|
|
12
13
|
def run(input, options)
|
13
14
|
reader_class = RDF::Reader.for(options[:input_format].to_sym)
|
@@ -62,17 +63,26 @@ def run(input, options)
|
|
62
63
|
STDERR.puts "\nParsed #{num} statements in #{secs} seconds @ #{num/secs} statements/second." unless options[:quiet]
|
63
64
|
end
|
64
65
|
rescue
|
65
|
-
fname =
|
66
|
+
fname = case
|
67
|
+
when input.respond_to?(:path) then input.path
|
68
|
+
when input.respond_to?(:requested_url) && input.requested_url then input.requested_url
|
69
|
+
when input.respond_to?(:base_uri) then input.base_uri
|
70
|
+
else "-stdin-"
|
71
|
+
end
|
66
72
|
STDERR.puts("Error in #{fname}")
|
67
73
|
raise
|
68
74
|
end
|
69
75
|
|
76
|
+
logger = Logger.new(STDERR)
|
77
|
+
logger.level = Logger::WARN
|
78
|
+
logger.formatter = lambda {|severity, datetime, progname, msg| "#{severity}: #{msg}\n"}
|
79
|
+
|
70
80
|
parser_options = {
|
71
81
|
base: nil,
|
72
|
-
progress: false,
|
73
82
|
validate: false,
|
74
83
|
stream: false,
|
75
84
|
strict: false,
|
85
|
+
logger: logger,
|
76
86
|
}
|
77
87
|
|
78
88
|
options = {
|
@@ -80,11 +90,12 @@ options = {
|
|
80
90
|
output: STDOUT,
|
81
91
|
output_format: :jsonld,
|
82
92
|
input_format: :jsonld,
|
93
|
+
logger: logger,
|
83
94
|
}
|
84
95
|
input = nil
|
85
96
|
|
86
97
|
OPT_ARGS = [
|
87
|
-
["--
|
98
|
+
["--debug", GetoptLong::NO_ARGUMENT, "Turn on verbose debugging"],
|
88
99
|
["--compact", GetoptLong::NO_ARGUMENT, "Compact document, using --context"],
|
89
100
|
["--compactArrays", GetoptLong::OPTIONAL_ARGUMENT, "Set compactArrays option"],
|
90
101
|
["--context", GetoptLong::REQUIRED_ARGUMENT,"Context to apply for expand, compact and converting from RDF"],
|
@@ -130,7 +141,7 @@ opts = GetoptLong.new(*OPT_ARGS.map {|o| o[0..-2]})
|
|
130
141
|
|
131
142
|
opts.each do |opt, arg|
|
132
143
|
case opt
|
133
|
-
when '--
|
144
|
+
when '--debug' then logger.level = Logger::DEBUG
|
134
145
|
when '--compact' then options[:compact] = true
|
135
146
|
when "--compactArrays" then options[:compactArrays] = (arg || 'true') == 'true'
|
136
147
|
when '--context' then options[:context] = arg
|
@@ -146,7 +157,9 @@ opts.each do |opt, arg|
|
|
146
157
|
when '--output' then options[:output] = File.open(arg, "w")
|
147
158
|
when '--parse-only' then options[:parse_only] = true
|
148
159
|
when '--processingMode' then options[:processingMode] = arg
|
149
|
-
when '--quiet'
|
160
|
+
when '--quiet'
|
161
|
+
options[:quiet] = true
|
162
|
+
logger.level = Logger::FATAL
|
150
163
|
when "--rename_bnodes" then options[:rename_bnodes] = (arg || 'true') == 'true'
|
151
164
|
when "--requireAll" then options[:requireAll] = (arg || 'true') == 'true'
|
152
165
|
when '--stream' then parser_options[:stream] = true
|
data/lib/json/ld/api.rb
CHANGED
@@ -136,7 +136,7 @@ module JSON::LD
|
|
136
136
|
@context = Context.parse(context || {}, @options)
|
137
137
|
|
138
138
|
# If not set explicitly, the context figures out the processing mode
|
139
|
-
@options[:processingMode] ||= @context.processingMode
|
139
|
+
@options[:processingMode] ||= @context.processingMode || "json-ld-1.0"
|
140
140
|
@options[:validate] ||= %w(json-ld-1.0 json-ld-1.1).include?(@options[:processingMode])
|
141
141
|
|
142
142
|
if block_given?
|
data/lib/json/ld/compact.rb
CHANGED
@@ -30,7 +30,7 @@ module JSON::LD
|
|
30
30
|
# If element has a single member and the active property has no
|
31
31
|
# @container mapping to @list or @set, the compacted value is that
|
32
32
|
# member; otherwise the compacted value is element
|
33
|
-
if result.length == 1 && context.
|
33
|
+
if result.length == 1 && !context.as_array?(property) && @options[:compactArrays]
|
34
34
|
#log_debug("=> extract single element: #{result.first.inspect}")
|
35
35
|
result.first
|
36
36
|
else
|
@@ -85,7 +85,7 @@ module JSON::LD
|
|
85
85
|
compacted_value.each do |prop, value|
|
86
86
|
if context.reverse?(prop)
|
87
87
|
value = [value] if !value.is_a?(Array) &&
|
88
|
-
(context.
|
88
|
+
(context.as_array?(prop) || !@options[:compactArrays])
|
89
89
|
#log_debug("") {"merge #{prop} => #{value.inspect}"}
|
90
90
|
|
91
91
|
merge_compacted_value(result, prop, value)
|
@@ -101,6 +101,17 @@ module JSON::LD
|
|
101
101
|
next
|
102
102
|
end
|
103
103
|
|
104
|
+
if expanded_property == '@preserve'
|
105
|
+
# Compact using `property`
|
106
|
+
compacted_value = compact(expanded_value, property: property)
|
107
|
+
#log_debug("@preserve") {"compacted_value: #{compacted_value.inspect}"}
|
108
|
+
|
109
|
+
unless compacted_value.is_a?(Array) && compacted_value.empty?
|
110
|
+
result['@preserve'] = compacted_value
|
111
|
+
end
|
112
|
+
next
|
113
|
+
end
|
114
|
+
|
104
115
|
if expanded_property == '@index' && context.container(property) == '@index'
|
105
116
|
#log_debug("@index") {"drop @index"}
|
106
117
|
next
|
@@ -150,6 +161,7 @@ module JSON::LD
|
|
150
161
|
end
|
151
162
|
|
152
163
|
container = context.container(item_active_property)
|
164
|
+
as_array = context.as_array?(item_active_property)
|
153
165
|
value = list?(expanded_item) ? expanded_item['@list'] : expanded_item
|
154
166
|
compacted_item = compact(value, property: item_active_property)
|
155
167
|
#log_debug("") {" => compacted key: #{item_active_property.inspect} for #{compacted_item.inspect}"}
|
@@ -195,14 +207,11 @@ module JSON::LD
|
|
195
207
|
end
|
196
208
|
compacted_item
|
197
209
|
end
|
210
|
+
compacted_item = [compacted_item] if as_array && !compacted_item.is_a?(Array)
|
198
211
|
merge_compacted_value(map_object, map_key, compacted_item)
|
199
212
|
else
|
200
213
|
compacted_item = [compacted_item] if
|
201
|
-
!compacted_item.is_a?(Array) && (
|
202
|
-
!@options[:compactArrays] ||
|
203
|
-
%w(@set @list).include?(container) ||
|
204
|
-
%w(@list @graph).include?(expanded_property)
|
205
|
-
)
|
214
|
+
!compacted_item.is_a?(Array) && (!@options[:compactArrays] || as_array)
|
206
215
|
merge_compacted_value(nest_result, item_active_property, compacted_item)
|
207
216
|
end
|
208
217
|
end
|
data/lib/json/ld/context.rb
CHANGED
@@ -36,8 +36,13 @@ module JSON::LD
|
|
36
36
|
# @return [String] Type mapping
|
37
37
|
attr_accessor :type_mapping
|
38
38
|
|
39
|
-
#
|
40
|
-
|
39
|
+
# Base container mapping, without @set
|
40
|
+
# @return ['@index', '@language', '@index', '@type', '@id'] Container mapping
|
41
|
+
attr_reader :container_mapping
|
42
|
+
|
43
|
+
# If container mapping was defined along with @set
|
44
|
+
# @return [Boolean]
|
45
|
+
attr_reader :as_set
|
41
46
|
|
42
47
|
# @return [String] Term used for nest properties
|
43
48
|
attr_accessor :nest
|
@@ -81,15 +86,24 @@ module JSON::LD
|
|
81
86
|
nest: nil,
|
82
87
|
simple: false,
|
83
88
|
context: nil)
|
84
|
-
@term
|
85
|
-
@id
|
86
|
-
@type_mapping
|
87
|
-
|
88
|
-
@language_mapping
|
89
|
-
@reverse_property
|
90
|
-
@nest
|
91
|
-
@simple
|
92
|
-
@context
|
89
|
+
@term = term
|
90
|
+
@id = id.to_s if id
|
91
|
+
@type_mapping = type_mapping.to_s if type_mapping
|
92
|
+
self.container_mapping = container_mapping if container_mapping
|
93
|
+
@language_mapping = language_mapping if language_mapping
|
94
|
+
@reverse_property = reverse_property if reverse_property
|
95
|
+
@nest = nest if nest
|
96
|
+
@simple = simple if simple
|
97
|
+
@context = context if context
|
98
|
+
end
|
99
|
+
|
100
|
+
# Set container mapping, from an array which may include @set
|
101
|
+
def container_mapping=(mapping)
|
102
|
+
mapping = Array(mapping)
|
103
|
+
if @as_set = mapping.include?('@set')
|
104
|
+
mapping -= %w(@set)
|
105
|
+
end
|
106
|
+
@container_mapping = mapping.first
|
93
107
|
end
|
94
108
|
|
95
109
|
##
|
@@ -109,6 +123,7 @@ module JSON::LD
|
|
109
123
|
|
110
124
|
if language_mapping.nil? &&
|
111
125
|
container_mapping.nil? &&
|
126
|
+
!as_set &&
|
112
127
|
type_mapping.nil? &&
|
113
128
|
reverse_property.nil? &&
|
114
129
|
self.context.nil? &&
|
@@ -125,7 +140,10 @@ module JSON::LD
|
|
125
140
|
context.compact_iri(type_mapping, vocab: true)
|
126
141
|
end
|
127
142
|
end
|
128
|
-
|
143
|
+
|
144
|
+
cm = [container_mapping, ('@set' if as_set)].compact
|
145
|
+
cm = cm.first if cm.length == 1
|
146
|
+
defn['@container'] = cm unless cm.empty?
|
129
147
|
# Language set as false to be output as null
|
130
148
|
defn['@language'] = (language_mapping ? language_mapping : nil) unless language_mapping.nil?
|
131
149
|
defn['@context'] = self.context unless self.context.nil?
|
@@ -143,6 +161,9 @@ module JSON::LD
|
|
143
161
|
%w(id type_mapping container_mapping language_mapping reverse_property nest simple context).each do |acc|
|
144
162
|
v = instance_variable_get("@#{acc}".to_sym)
|
145
163
|
v = v.to_s if v.is_a?(RDF::Term)
|
164
|
+
if acc == 'container_mapping' && as_set
|
165
|
+
v = v ? [v, '@set'] : '@set'
|
166
|
+
end
|
146
167
|
defn << "#{acc}: #{v.inspect}" if v
|
147
168
|
end
|
148
169
|
defn.join(', ') + ")"
|
@@ -154,6 +175,7 @@ module JSON::LD
|
|
154
175
|
v << "term=#{@term}"
|
155
176
|
v << "rev" if reverse_property
|
156
177
|
v << "container=#{container_mapping}" if container_mapping
|
178
|
+
v << "as_set=#{as_set.inspect}"
|
157
179
|
v << "lang=#{language_mapping.inspect}" unless language_mapping.nil?
|
158
180
|
v << "type=#{type_mapping}" unless type_mapping.nil?
|
159
181
|
v << "nest=#{nest.inspect}" unless nest.nil?
|
@@ -467,7 +489,7 @@ module JSON::LD
|
|
467
489
|
end
|
468
490
|
else
|
469
491
|
# 3.3) If context is not a JSON object, an invalid local context error has been detected and processing is aborted.
|
470
|
-
raise JsonLdError::InvalidLocalContext, context.inspect
|
492
|
+
raise JsonLdError::InvalidLocalContext, "must be a URL, JSON object or array of same: #{context.inspect}"
|
471
493
|
end
|
472
494
|
end
|
473
495
|
result
|
@@ -607,7 +629,7 @@ module JSON::LD
|
|
607
629
|
container = value['@container']
|
608
630
|
raise JsonLdError::InvalidReverseProperty,
|
609
631
|
"unknown mapping for '@container' to #{container.inspect} on term #{term.inspect}" unless
|
610
|
-
['@set', '@index'].include?(container)
|
632
|
+
container.is_a?(String) && ['@set', '@index'].include?(container)
|
611
633
|
definition.container_mapping = check_container(container, local_context, defined, term)
|
612
634
|
end
|
613
635
|
definition.reverse_property = true
|
@@ -650,7 +672,7 @@ module JSON::LD
|
|
650
672
|
|
651
673
|
if value.has_key?('@context')
|
652
674
|
# Not supported in JSON-LD 1.0
|
653
|
-
raise JsonLdError::InvalidTermDefinition, '@context not valid in term definition for JSON-LD 1.0' if processingMode < 'json-ld-1.1'
|
675
|
+
raise JsonLdError::InvalidTermDefinition, '@context not valid in term definition for JSON-LD 1.0 on term #{term.inspect}, set processing mode using @version' if processingMode < 'json-ld-1.1'
|
654
676
|
|
655
677
|
begin
|
656
678
|
self.parse(value['@context'])
|
@@ -670,10 +692,10 @@ module JSON::LD
|
|
670
692
|
|
671
693
|
if value.has_key?('@nest')
|
672
694
|
# Not supported in JSON-LD 1.0
|
673
|
-
raise JsonLdError::InvalidTermDefinition, '@nest not valid in term definition for JSON-LD 1.0' if processingMode < 'json-ld-1.1'
|
695
|
+
raise JsonLdError::InvalidTermDefinition, '@nest not valid in term definition for JSON-LD 1.0 on term #{term.inspect}, set processing mode using @version' if processingMode < 'json-ld-1.1'
|
674
696
|
|
675
697
|
# Not supported in JSON-LD 1.0
|
676
|
-
raise JsonLdError::InvalidTermDefinition, '@nest not valid in term definition for JSON-LD 1.0' if processingMode < 'json-ld-1.1'
|
698
|
+
raise JsonLdError::InvalidTermDefinition, '@nest not valid in term definition for JSON-LD 1.0 on term #{term.inspect}, set processing mode using @version' if processingMode < 'json-ld-1.1'
|
677
699
|
|
678
700
|
nest = value['@nest']
|
679
701
|
raise JsonLdError::InvalidNestValue, "nest must be a string, was #{nest.inspect}} on term #{term.inspect}" unless nest.is_a?(String)
|
@@ -848,6 +870,17 @@ module JSON::LD
|
|
848
870
|
term && term.container_mapping
|
849
871
|
end
|
850
872
|
|
873
|
+
##
|
874
|
+
# Should values be represented as a set?
|
875
|
+
#
|
876
|
+
# @param [Term, #to_s] term in unexpanded form
|
877
|
+
# @return [Boolean]
|
878
|
+
def as_array?(term)
|
879
|
+
return true if %w(@graph @list).include?(term)
|
880
|
+
term = find_definition(term)
|
881
|
+
term && (term.as_set || term.container_mapping == '@list')
|
882
|
+
end
|
883
|
+
|
851
884
|
##
|
852
885
|
# Retrieve content of a term
|
853
886
|
#
|
@@ -873,7 +906,7 @@ module JSON::LD
|
|
873
906
|
term.nest
|
874
907
|
else
|
875
908
|
nest_term = find_definition(term.nest)
|
876
|
-
raise JsonLdError::InvalidNestValue, "nest must a term resolving to @nest" unless nest_term && nest_term.simple? && nest_term.id == '@nest'
|
909
|
+
raise JsonLdError::InvalidNestValue, "nest must a term resolving to @nest, was #{nest_term.inspect}" unless nest_term && nest_term.simple? && nest_term.id == '@nest'
|
877
910
|
term.nest
|
878
911
|
end
|
879
912
|
end
|
@@ -1023,6 +1056,9 @@ module JSON::LD
|
|
1023
1056
|
containers = []
|
1024
1057
|
tl, tl_value = "@language", "@null"
|
1025
1058
|
|
1059
|
+
# If the value is a JSON Object with the key @preserve, use the value of @preserve.
|
1060
|
+
value = value['@preserve'].first if value.is_a?(Hash) && value.has_key?('@preserve')
|
1061
|
+
|
1026
1062
|
# If the value is a JSON Object, then for the keywords @index, @id, and @type, if the value contains that keyword, append it to containers.
|
1027
1063
|
%w(@index @id @type).each do |kw|
|
1028
1064
|
containers << kw if value.has_key?(kw)
|
@@ -1321,6 +1357,7 @@ module JSON::LD
|
|
1321
1357
|
defn << "base: #{self.base.to_s.inspect}" if self.base
|
1322
1358
|
defn << "language: #{self.default_language.inspect}" if self.default_language
|
1323
1359
|
defn << "vocab: #{self.vocab.to_s.inspect}" if self.vocab
|
1360
|
+
defn << "processingMode: #{self.processingMode.inspect}" if self.processingMode
|
1324
1361
|
term_defs = term_definitions.map do |term, td|
|
1325
1362
|
" " + term.inspect + " => " + td.to_rb
|
1326
1363
|
end.sort
|
@@ -1444,7 +1481,7 @@ module JSON::LD
|
|
1444
1481
|
a.length == b.length ? (a <=> b) : (a.length <=> b.length)
|
1445
1482
|
end.each do |term|
|
1446
1483
|
next unless td = term_definitions[term]
|
1447
|
-
container = td.container_mapping || '@none'
|
1484
|
+
container = td.container_mapping || (td.as_set ? '@set' : '@none')
|
1448
1485
|
container_map = result[td.id.to_s] ||= {}
|
1449
1486
|
tl_map = container_map[container] ||= {'@language' => {}, '@type' => {}}
|
1450
1487
|
type_map = tl_map['@type']
|
@@ -1561,8 +1598,23 @@ module JSON::LD
|
|
1561
1598
|
# Ensure @container mapping is appropriate
|
1562
1599
|
# The result is the original container definition. For IRI containers, this is necessary to be able to determine the @type mapping for string values
|
1563
1600
|
def check_container(container, local_context, defined, term)
|
1564
|
-
|
1565
|
-
|
1601
|
+
if container.is_a?(Array) && processingMode < 'json-ld-1.1'
|
1602
|
+
raise JsonLdError::InvalidContainerMapping,
|
1603
|
+
"'@container' on term #{term.inspect} must be a string: #{container.inspect}"
|
1604
|
+
end
|
1605
|
+
|
1606
|
+
val = Array(container)
|
1607
|
+
val -= %w(@set) if has_set = val.include?('@set')
|
1608
|
+
|
1609
|
+
raise JsonLdError::InvalidContainerMapping,
|
1610
|
+
"'@container' has more than one value other than @set" if val.length > 1
|
1611
|
+
|
1612
|
+
case val.first
|
1613
|
+
when '@list'
|
1614
|
+
raise JsonLdError::InvalidContainerMapping,
|
1615
|
+
"'@container' on term #{term.inspect} cannot be both @list and @set" if has_set
|
1616
|
+
# Okay
|
1617
|
+
when '@language', '@index', nil
|
1566
1618
|
# Okay
|
1567
1619
|
when '@type', '@id', nil
|
1568
1620
|
raise JsonLdError::InvalidContainerMapping,
|
@@ -1572,7 +1624,7 @@ module JSON::LD
|
|
1572
1624
|
raise JsonLdError::InvalidContainerMapping,
|
1573
1625
|
"unknown mapping for '@container' to #{container.inspect} on term #{term.inspect}"
|
1574
1626
|
end
|
1575
|
-
container
|
1627
|
+
Array(container)
|
1576
1628
|
end
|
1577
1629
|
end
|
1578
1630
|
end
|
data/lib/json/ld/expand.rb
CHANGED
@@ -384,7 +384,6 @@ module JSON::LD
|
|
384
384
|
|
385
385
|
# Initialize index value to the result of using this algorithm recursively, passing active context, key as active property, and index value as element.
|
386
386
|
index_value = expand([value[k]].flatten, key, map_context, ordered: ordered)
|
387
|
-
#require 'byebug'; byebug
|
388
387
|
index_value.each do |item|
|
389
388
|
case container
|
390
389
|
when '@index' then item[container] ||= k
|
data/lib/json/ld/frame.rb
CHANGED
@@ -84,6 +84,7 @@ module JSON::LD
|
|
84
84
|
|
85
85
|
# Subject is also the name of a graph
|
86
86
|
if state[:graphMap].has_key?(id)
|
87
|
+
log_debug("frame") {"#{id} in graphMap"}
|
87
88
|
# check frame's "@graph" to see what to do next
|
88
89
|
# 1. if it doesn't exist and state.graph === "@merged", don't recurse
|
89
90
|
# 2. if it doesn't exist and state.graph !== "@merged", recurse
|
@@ -95,7 +96,7 @@ module JSON::LD
|
|
95
96
|
recurse, subframe = (state[:graph] != '@merged'), {}
|
96
97
|
else
|
97
98
|
subframe = frame['@graph'].first
|
98
|
-
recurse = !%w(@merged @default).include?(
|
99
|
+
recurse = !%w(@merged @default).include?(id)
|
99
100
|
subframe = {} unless subframe.is_a?(Hash)
|
100
101
|
end
|
101
102
|
|
@@ -215,11 +216,15 @@ module JSON::LD
|
|
215
216
|
result = case input
|
216
217
|
when Array
|
217
218
|
# If, after replacement, an array contains only the value null remove the value, leaving an empty array.
|
218
|
-
input.map {|o| cleanup_preserve(o, bnodes_to_clear)}.compact
|
219
|
+
v = input.map {|o| cleanup_preserve(o, bnodes_to_clear)}.compact
|
220
|
+
|
221
|
+
# If the array contains a single member, which is itself an array, use that value as the result
|
222
|
+
(v.length == 1 && v.first.is_a?(Array)) ? v.first : v
|
219
223
|
when Hash
|
220
224
|
output = Hash.new
|
221
225
|
input.each do |key, value|
|
222
226
|
if key == '@preserve'
|
227
|
+
#require 'byebug'; byebug
|
223
228
|
# replace all key-value pairs where the key is @preserve with the value from the key-pair
|
224
229
|
output = cleanup_preserve(value, bnodes_to_clear)
|
225
230
|
elsif context.expand_iri(key) == '@id' && bnodes_to_clear.include?(value)
|
@@ -228,8 +233,7 @@ module JSON::LD
|
|
228
233
|
v = cleanup_preserve(value, bnodes_to_clear)
|
229
234
|
|
230
235
|
# Because we may have added a null value to an array, we need to clean that up, if we possible
|
231
|
-
v = v.first if v.is_a?(Array) && v.length == 1 &&
|
232
|
-
context.expand_iri(key) != "@graph" && context.container(key).nil?
|
236
|
+
v = v.first if v.is_a?(Array) && v.length == 1 && !context.as_array?(key)
|
233
237
|
output[key] = v
|
234
238
|
end
|
235
239
|
end
|
data/spec/context_spec.rb
CHANGED
@@ -200,14 +200,6 @@ describe JSON::LD::Context do
|
|
200
200
|
}, logger)
|
201
201
|
end
|
202
202
|
|
203
|
-
it "associates @set container mapping with term" do
|
204
|
-
expect(subject.parse({
|
205
|
-
"foo" => {"@id" => "http://example.com/", "@container" => "@set"}
|
206
|
-
}).containers).to produce({
|
207
|
-
"foo" => '@set'
|
208
|
-
}, logger)
|
209
|
-
end
|
210
|
-
|
211
203
|
it "associates @type container mapping with term" do
|
212
204
|
expect(subject.parse({
|
213
205
|
"foo" => {"@id" => "http://example.com/", "@container" => "@type"}
|
@@ -934,7 +926,6 @@ describe JSON::LD::Context do
|
|
934
926
|
"listdouble" => {"@id" => "http://example.com/double", "@type" => "xsd:double", "@container" => "@list"},
|
935
927
|
"listdate" => {"@id" => "http://example.com/date", "@type" => "xsd:date", "@container" => "@list"},
|
936
928
|
"listid" => {"@id" => "http://example.com/id", "@type" => "@id", "@container" => "@list"},
|
937
|
-
"setplain" => {"@id" => "http://example.com/plain", "@container" => "@set"},
|
938
929
|
"setlang" => {"@id" => "http://example.com/lang", "@language" => "en", "@container" => "@set"},
|
939
930
|
"setbool" => {"@id" => "http://example.com/bool", "@type" => "xsd:boolean", "@container" => "@set"},
|
940
931
|
"setinteger" => {"@id" => "http://example.com/integer", "@type" => "xsd:integer", "@container" => "@set"},
|
@@ -950,7 +941,7 @@ describe JSON::LD::Context do
|
|
950
941
|
{
|
951
942
|
"langmap" => [{"@value" => "en", "@language" => "en"}],
|
952
943
|
#"plain" => [{"@value" => "foo"}],
|
953
|
-
"setplain" => [{"@value" => "foo", "@language" => "pl"}]
|
944
|
+
#"setplain" => [{"@value" => "foo", "@language" => "pl"}]
|
954
945
|
}.each do |prop, values|
|
955
946
|
context "uses #{prop}" do
|
956
947
|
values.each do |value|
|
@@ -1370,7 +1361,6 @@ describe JSON::LD::Context do
|
|
1370
1361
|
{
|
1371
1362
|
"ex" => nil,
|
1372
1363
|
"list" => "@list",
|
1373
|
-
"set" => "@set",
|
1374
1364
|
"language" => "@language",
|
1375
1365
|
"ndx" => "@index",
|
1376
1366
|
"id" => "@id",
|
@@ -1384,7 +1374,6 @@ describe JSON::LD::Context do
|
|
1384
1374
|
{
|
1385
1375
|
"ex" => nil,
|
1386
1376
|
"list" => "@list",
|
1387
|
-
"set" => "@set",
|
1388
1377
|
"language" => "@language",
|
1389
1378
|
"ndx" => "@index",
|
1390
1379
|
"id" => "@id",
|
@@ -1519,7 +1508,7 @@ describe JSON::LD::Context do
|
|
1519
1508
|
|
1520
1509
|
context "with container_mapping" do
|
1521
1510
|
subject {described_class.new("term", container_mapping: "@set")}
|
1522
|
-
its(:container_mapping) {is_expected.to
|
1511
|
+
its(:container_mapping) {is_expected.to be_nil}
|
1523
1512
|
its(:to_rb) {is_expected.to eq %(TermDefinition.new("term", container_mapping: "@set"))}
|
1524
1513
|
end
|
1525
1514
|
|
data/spec/suite_helper.rb
CHANGED
@@ -1,6 +1,72 @@
|
|
1
1
|
require 'json/ld'
|
2
2
|
require 'support/extensions'
|
3
3
|
|
4
|
+
|
5
|
+
# For now, override RDF::Utils::File.open_file to look for the file locally before attempting to retrieve it
|
6
|
+
module RDF::Util
|
7
|
+
module File
|
8
|
+
REMOTE_PATH = "http://json-ld.org/test-suite/"
|
9
|
+
LOCAL_PATH = ::File.expand_path("../json-ld.org/test-suite", __FILE__) + '/'
|
10
|
+
|
11
|
+
class << self
|
12
|
+
alias_method :original_open_file, :open_file
|
13
|
+
end
|
14
|
+
|
15
|
+
##
|
16
|
+
# Override to use Patron for http and https, Kernel.open otherwise.
|
17
|
+
#
|
18
|
+
# @param [String] filename_or_url to open
|
19
|
+
# @param [Hash{Symbol => Object}] options
|
20
|
+
# @option options [Array, String] :headers
|
21
|
+
# HTTP Request headers.
|
22
|
+
# @return [IO] File stream
|
23
|
+
# @yield [IO] File stream
|
24
|
+
def self.open_file(filename_or_url, options = {}, &block)
|
25
|
+
case
|
26
|
+
when filename_or_url.to_s =~ /^file:/
|
27
|
+
path = filename_or_url[5..-1]
|
28
|
+
Kernel.open(path.to_s, options, &block)
|
29
|
+
when Dir.exist?(LOCAL_PATH) &&
|
30
|
+
!filename_or_url.to_s.include?('remote-doc') &&
|
31
|
+
filename_or_url.to_s =~ %r{^#{REMOTE_PATH}}
|
32
|
+
#puts "attempt to open #{filename_or_url} locally"
|
33
|
+
localpath = filename_or_url.to_s.sub(REMOTE_PATH, LOCAL_PATH)
|
34
|
+
response = begin
|
35
|
+
::File.open(localpath)
|
36
|
+
rescue Errno::ENOENT => e
|
37
|
+
raise IOError, e.message
|
38
|
+
end
|
39
|
+
document_options = {
|
40
|
+
base_uri: RDF::URI(filename_or_url),
|
41
|
+
charset: Encoding::UTF_8,
|
42
|
+
code: 200,
|
43
|
+
headers: {}
|
44
|
+
}
|
45
|
+
#puts "use #{filename_or_url} locally"
|
46
|
+
document_options[:headers][:content_type] = case filename_or_url.to_s
|
47
|
+
when /\.ttl$/ then 'text/turtle'
|
48
|
+
when /\.nt$/ then 'application/n-triples'
|
49
|
+
when /\.jsonld$/ then 'application/ld+json'
|
50
|
+
else 'unknown'
|
51
|
+
end
|
52
|
+
|
53
|
+
document_options[:headers][:content_type] = response.content_type if response.respond_to?(:content_type)
|
54
|
+
# For overriding content type from test data
|
55
|
+
document_options[:headers][:content_type] = options[:contentType] if options[:contentType]
|
56
|
+
|
57
|
+
remote_document = RDF::Util::File::RemoteDocument.new(response.read, document_options)
|
58
|
+
if block_given?
|
59
|
+
yield remote_document
|
60
|
+
else
|
61
|
+
remote_document
|
62
|
+
end
|
63
|
+
else
|
64
|
+
original_open_file(filename_or_url, options, &block)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
4
70
|
module Fixtures
|
5
71
|
module SuiteTest
|
6
72
|
SUITE = RDF::URI("http://json-ld.org/test-suite/")
|
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: 2.1.
|
4
|
+
version: 2.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gregg Kellogg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rdf
|