json-ld 1.99.2 → 2.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/VERSION +1 -1
- data/lib/json/ld.rb +0 -3
- data/lib/json/ld/api.rb +38 -21
- data/lib/json/ld/compact.rb +24 -24
- data/lib/json/ld/context.rb +80 -79
- data/lib/json/ld/expand.rb +30 -30
- data/lib/json/ld/flatten.rb +2 -2
- data/lib/json/ld/format.rb +126 -1
- data/lib/json/ld/frame.rb +3 -3
- data/lib/json/ld/from_rdf.rb +3 -3
- data/lib/json/ld/reader.rb +15 -5
- data/lib/json/ld/streaming_writer.rb +6 -6
- data/lib/json/ld/to_rdf.rb +75 -85
- data/lib/json/ld/utils.rb +1 -30
- data/lib/json/ld/writer.rb +86 -51
- data/spec/api_spec.rb +8 -8
- data/spec/compact_spec.rb +19 -19
- data/spec/context_spec.rb +76 -96
- data/spec/expand_spec.rb +30 -30
- data/spec/flatten_spec.rb +3 -4
- data/spec/format_spec.rb +43 -0
- data/spec/frame_spec.rb +7 -9
- data/spec/from_rdf_spec.rb +25 -24
- data/spec/reader_spec.rb +4 -1
- data/spec/spec_helper.rb +0 -1
- data/spec/streaming_writer_spec.rb +18 -11
- data/spec/suite_helper.rb +29 -28
- data/spec/suite_to_rdf_spec.rb +0 -2
- data/spec/to_rdf_spec.rb +17 -18
- data/spec/writer_spec.rb +15 -11
- metadata +75 -35
- data/spec/matchers.rb +0 -67
data/lib/json/ld/expand.rb
CHANGED
@@ -16,11 +16,11 @@ module JSON::LD
|
|
16
16
|
# @return [Array, Hash]
|
17
17
|
def expand(input, active_property, context, options = {})
|
18
18
|
options = {ordered: true}.merge!(options)
|
19
|
-
|
19
|
+
log_debug("expand") {"input: #{input.inspect}, active_property: #{active_property.inspect}, context: #{context.inspect}"}
|
20
20
|
result = case input
|
21
21
|
when Array
|
22
22
|
# If element is an array,
|
23
|
-
|
23
|
+
log_depth do
|
24
24
|
is_list = context.container(active_property) == '@list'
|
25
25
|
value = input.map do |v|
|
26
26
|
# Initialize expanded item to the result of using this algorithm recursively, passing active context, active property, and item as element.
|
@@ -39,26 +39,26 @@ module JSON::LD
|
|
39
39
|
# If element contains the key @context, set active context to the result of the Context Processing algorithm, passing active context and the value of the @context key as local context.
|
40
40
|
if input.has_key?('@context')
|
41
41
|
context = context.parse(input.delete('@context'))
|
42
|
-
|
42
|
+
log_debug("expand") {"context: #{context.inspect}"}
|
43
43
|
end
|
44
44
|
|
45
|
-
|
45
|
+
log_depth do
|
46
46
|
output_object = {}
|
47
47
|
# Then, proceed and process each property and value in element as follows:
|
48
48
|
keys = options[:ordered] ? input.keys.kw_sort : input.keys
|
49
49
|
keys.each do |key|
|
50
50
|
# For each key and value in element, ordered lexicographically by key:
|
51
51
|
value = input[key]
|
52
|
-
expanded_property = context.expand_iri(key, vocab: true,
|
52
|
+
expanded_property = context.expand_iri(key, vocab: true, log_depth: @options[:log_depth])
|
53
53
|
|
54
54
|
# If expanded property is null or it neither contains a colon (:) nor it is a keyword, drop key by continuing to the next key.
|
55
55
|
next if expanded_property.is_a?(RDF::URI) && expanded_property.relative?
|
56
56
|
expanded_property = expanded_property.to_s if expanded_property.is_a?(RDF::Resource)
|
57
57
|
|
58
|
-
|
58
|
+
log_debug("expand property") {"ap: #{active_property.inspect}, expanded: #{expanded_property.inspect}, value: #{value.inspect}"}
|
59
59
|
|
60
60
|
if expanded_property.nil?
|
61
|
-
|
61
|
+
log_debug(" => ") {"skip nil property"}
|
62
62
|
next
|
63
63
|
end
|
64
64
|
|
@@ -78,21 +78,21 @@ module JSON::LD
|
|
78
78
|
"value of @id must be a string: #{value.inspect}" unless value.is_a?(String)
|
79
79
|
|
80
80
|
# Otherwise, set expanded value to the result of using the IRI Expansion algorithm, passing active context, value, and true for document relative.
|
81
|
-
context.expand_iri(value, documentRelative: true,
|
81
|
+
context.expand_iri(value, documentRelative: true, log_depth: @options[:log_depth]).to_s
|
82
82
|
when '@type'
|
83
83
|
# If expanded property is @type and value is neither a string nor an array of strings, an invalid type value error has been detected and processing is aborted. Otherwise, set expanded value to the result of using the IRI Expansion algorithm, passing active context, true for vocab, and true for document relative to expand the value or each of its items.
|
84
|
-
|
84
|
+
log_debug("@type") {"value: #{value.inspect}"}
|
85
85
|
case value
|
86
86
|
when Array
|
87
|
-
|
87
|
+
log_depth do
|
88
88
|
value.map do |v|
|
89
89
|
raise JsonLdError::InvalidTypeValue,
|
90
90
|
"@type value must be a string or array of strings: #{v.inspect}" unless v.is_a?(String)
|
91
|
-
context.expand_iri(v, vocab: true, documentRelative: true, quiet: true,
|
91
|
+
context.expand_iri(v, vocab: true, documentRelative: true, quiet: true, log_depth: @options[:log_depth]).to_s
|
92
92
|
end
|
93
93
|
end
|
94
94
|
when String
|
95
|
-
context.expand_iri(value, vocab: true, documentRelative: true, quiet: true,
|
95
|
+
context.expand_iri(value, vocab: true, documentRelative: true, quiet: true, log_depth: @options[:log_depth]).to_s
|
96
96
|
when Hash
|
97
97
|
# For framing
|
98
98
|
raise JsonLdError::InvalidTypeValue,
|
@@ -104,7 +104,7 @@ module JSON::LD
|
|
104
104
|
end
|
105
105
|
when '@graph'
|
106
106
|
# If expanded property is @graph, set expanded value to the result of using this algorithm recursively passing active context, @graph for active property, and value for element.
|
107
|
-
|
107
|
+
log_depth { expand(value, '@graph', context, options) }
|
108
108
|
when '@value'
|
109
109
|
# If expanded property is @value and value is not a scalar or null, an invalid value object value error has been detected and processing is aborted. Otherwise, set expanded value to value. If expanded value is null, set the @value member of result to null and continue with the next key from element. Null values need to be preserved in this case as the meaning of an @type member depends on the existence of an @value member.
|
110
110
|
raise JsonLdError::InvalidValueObjectValue,
|
@@ -131,7 +131,7 @@ module JSON::LD
|
|
131
131
|
next if (active_property || '@graph') == '@graph'
|
132
132
|
|
133
133
|
# Otherwise, initialize expanded value to the result of using this algorithm recursively passing active context, active property, and value for element.
|
134
|
-
value =
|
134
|
+
value = log_depth { expand(value, active_property, context, options) }
|
135
135
|
|
136
136
|
# Spec FIXME: need to be sure that result is an array
|
137
137
|
value = [value] unless value.is_a?(Array)
|
@@ -144,7 +144,7 @@ module JSON::LD
|
|
144
144
|
value
|
145
145
|
when '@set'
|
146
146
|
# If expanded property is @set, set expanded value to the result of using this algorithm recursively, passing active context, active property, and value for element.
|
147
|
-
|
147
|
+
log_depth { expand(value, active_property, context, options) }
|
148
148
|
when '@reverse'
|
149
149
|
# If expanded property is @reverse and value is not a JSON object, an invalid @reverse value error has been detected and processing is aborted.
|
150
150
|
raise JsonLdError::InvalidReverseValue,
|
@@ -152,11 +152,11 @@ module JSON::LD
|
|
152
152
|
|
153
153
|
# Otherwise
|
154
154
|
# Initialize expanded value to the result of using this algorithm recursively, passing active context, @reverse as active property, and value as element.
|
155
|
-
value =
|
155
|
+
value = log_depth { expand(value, '@reverse', context, options) }
|
156
156
|
|
157
157
|
# If expanded value contains an @reverse member, i.e., properties that are reversed twice, execute for each of its property and item the following steps:
|
158
158
|
if value.has_key?('@reverse')
|
159
|
-
|
159
|
+
log_debug("@reverse") {"double reverse: #{value.inspect}"}
|
160
160
|
value['@reverse'].each do |property, item|
|
161
161
|
# If result does not have a property member, create one and set its value to an empty array.
|
162
162
|
# Append item to the value of the property member of result.
|
@@ -184,14 +184,14 @@ module JSON::LD
|
|
184
184
|
next
|
185
185
|
when '@explicit', '@default', '@embed', '@explicit', '@omitDefault', '@preserve', '@requireAll'
|
186
186
|
# Framing keywords
|
187
|
-
|
187
|
+
log_depth { [expand(value, expanded_property, context, options)].flatten }
|
188
188
|
else
|
189
189
|
# Skip unknown keyword
|
190
190
|
next
|
191
191
|
end
|
192
192
|
|
193
193
|
# Unless expanded value is null, set the expanded property member of result to expanded value.
|
194
|
-
|
194
|
+
log_debug("expand #{expanded_property}") { expanded_value.inspect}
|
195
195
|
output_object[expanded_property] = expanded_value unless expanded_value.nil?
|
196
196
|
next
|
197
197
|
end
|
@@ -229,7 +229,7 @@ module JSON::LD
|
|
229
229
|
keys = options[:ordered] ? value.keys.sort : value.keys
|
230
230
|
keys.each do |k|
|
231
231
|
# Initialize index value to the result of using this algorithm recursively, passing active context, key as active property, and index value as element.
|
232
|
-
index_value =
|
232
|
+
index_value = log_depth { expand([value[k]].flatten, key, context, options) }
|
233
233
|
index_value.each do |item|
|
234
234
|
item['@index'] ||= k
|
235
235
|
ary << item
|
@@ -238,22 +238,22 @@ module JSON::LD
|
|
238
238
|
ary
|
239
239
|
else
|
240
240
|
# Otherwise, initialize expanded value to the result of using this algorithm recursively, passing active context, key for active property, and value for element.
|
241
|
-
|
241
|
+
log_depth { expand(value, key, context, options) }
|
242
242
|
end
|
243
243
|
|
244
244
|
# If expanded value is null, ignore key by continuing to the next key from element.
|
245
245
|
if expanded_value.nil?
|
246
|
-
|
246
|
+
log_debug(" => skip nil value")
|
247
247
|
next
|
248
248
|
end
|
249
|
-
|
249
|
+
log_debug {" => #{expanded_value.inspect}"}
|
250
250
|
|
251
251
|
# 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.
|
252
252
|
if context.container(key) == '@list' && !list?(expanded_value)
|
253
|
-
|
253
|
+
log_debug(" => ") { "convert #{expanded_value.inspect} to list"}
|
254
254
|
expanded_value = {'@list' => [expanded_value].flatten}
|
255
255
|
end
|
256
|
-
|
256
|
+
log_debug {" => #{expanded_value.inspect}"}
|
257
257
|
|
258
258
|
# Otherwise, if the term definition associated to key indicates that it is a reverse property
|
259
259
|
# Spec FIXME: this is not an otherwise.
|
@@ -276,7 +276,7 @@ module JSON::LD
|
|
276
276
|
end
|
277
277
|
end
|
278
278
|
|
279
|
-
|
279
|
+
log_debug("output object") {output_object.inspect}
|
280
280
|
|
281
281
|
# If result contains the key @value:
|
282
282
|
if value?(output_object)
|
@@ -298,7 +298,7 @@ module JSON::LD
|
|
298
298
|
raise JsonLdError::InvalidLanguageTaggedValue,
|
299
299
|
"when @language is used, @value must be a string: #{@value.inspect}"
|
300
300
|
elsif !output_object.fetch('@type', "").is_a?(String) ||
|
301
|
-
!context.expand_iri(output_object.fetch('@type', ""), vocab: true,
|
301
|
+
!context.expand_iri(output_object.fetch('@type', ""), vocab: true, log_depth: @options[:log_depth]).is_a?(RDF::URI)
|
302
302
|
# Otherwise, if the result has a @type member and its value is not an IRI, an invalid typed value error has been detected and processing is aborted.
|
303
303
|
raise JsonLdError::InvalidTypedValue,
|
304
304
|
"value of @type must be an IRI: #{output_object['@type'].inspect}"
|
@@ -324,7 +324,7 @@ module JSON::LD
|
|
324
324
|
if (active_property || '@graph') == '@graph' &&
|
325
325
|
(output_object.keys.any? {|k| %w(@value @list).include?(k)} ||
|
326
326
|
(output_object.keys - %w(@id)).empty?)
|
327
|
-
|
327
|
+
log_debug(" =>") { "empty top-level: " + output_object.inspect}
|
328
328
|
return nil
|
329
329
|
end
|
330
330
|
|
@@ -338,10 +338,10 @@ module JSON::LD
|
|
338
338
|
else
|
339
339
|
# Otherwise, unless the value is a number, expand the value according to the Value Expansion rules, passing active property.
|
340
340
|
return nil if input.nil? || active_property.nil? || active_property == '@graph'
|
341
|
-
context.expand_value(active_property, input,
|
341
|
+
context.expand_value(active_property, input, log_depth: @options[:log_depth])
|
342
342
|
end
|
343
343
|
|
344
|
-
|
344
|
+
log_debug {" => #{result.inspect}"}
|
345
345
|
result
|
346
346
|
end
|
347
347
|
end
|
data/lib/json/ld/flatten.rb
CHANGED
@@ -19,8 +19,8 @@ module JSON::LD
|
|
19
19
|
graph = '@default',
|
20
20
|
name = nil,
|
21
21
|
list = nil)
|
22
|
-
|
23
|
-
|
22
|
+
log_depth do
|
23
|
+
log_debug("node_map") {"graph: #{graph}, input: #{input.inspect}, name: #{name}"}
|
24
24
|
case input
|
25
25
|
when Array
|
26
26
|
# If input is an array, process each entry in input recursively by passing item for input, node map, active graph, active subject, active property, and list.
|
data/lib/json/ld/format.rb
CHANGED
@@ -41,7 +41,132 @@ module JSON::LD
|
|
41
41
|
# Exclude CSVW metadata
|
42
42
|
!sample.include?("http://www.w3.org/ns/csvw")
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
|
+
##
|
46
|
+
# Hash of CLI commands appropriate for this format
|
47
|
+
# @return [Hash{Symbol => Lambda(Array, Hash)}]
|
48
|
+
def self.cli_commands
|
49
|
+
{
|
50
|
+
expand: ->(files, options) do
|
51
|
+
out = options[:output] || $stdout
|
52
|
+
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
|
53
|
+
options = options.merge(expandContext: options.delete(:context)) if options.has_key?(:context)
|
54
|
+
if options[:format] == :jsonld
|
55
|
+
if files.empty?
|
56
|
+
# If files are empty, either use options[:execute]
|
57
|
+
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
|
58
|
+
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
|
59
|
+
JSON::LD::API.expand(input, options) do |expanded|
|
60
|
+
out.puts expanded.to_json(JSON::LD::JSON_STATE)
|
61
|
+
end
|
62
|
+
else
|
63
|
+
files.each do |file|
|
64
|
+
JSON::LD::API.expand(file, options) do |expanded|
|
65
|
+
out.puts expanded.to_json(JSON::LD::JSON_STATE)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
else
|
70
|
+
# Turn RDF into JSON-LD first
|
71
|
+
RDF::CLI.parse(files, options) do |reader|
|
72
|
+
JSON::LD::API.fromRdf(reader) do |expanded|
|
73
|
+
out.puts expanded.to_json(JSON::LD::JSON_STATE)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end,
|
78
|
+
compact: ->(files, options) do
|
79
|
+
raise ArgumentError, "Compacting requires a context" unless options[:context]
|
80
|
+
out = options[:output] || $stdout
|
81
|
+
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
|
82
|
+
if options[:format] == :jsonld
|
83
|
+
if files.empty?
|
84
|
+
# If files are empty, either use options[:execute]
|
85
|
+
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
|
86
|
+
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
|
87
|
+
JSON::LD::API.compact(input, options[:context], options) do |compacted|
|
88
|
+
out.puts compacted.to_json(JSON::LD::JSON_STATE)
|
89
|
+
end
|
90
|
+
else
|
91
|
+
files.each do |file|
|
92
|
+
JSON::LD::API.compact(file, options[:context], options) do |compacted|
|
93
|
+
out.puts compacted.to_json(JSON::LD::JSON_STATE)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
else
|
98
|
+
# Turn RDF into JSON-LD first
|
99
|
+
RDF::CLI.parse(files, options) do |reader|
|
100
|
+
JSON::LD::API.fromRdf(reader) do |expanded|
|
101
|
+
JSON::LD::API.compact(expanded, options[:context], options) do |compacted|
|
102
|
+
out.puts compacted.to_json(JSON::LD::JSON_STATE)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end,
|
108
|
+
flatten: ->(files, options) do
|
109
|
+
out = options[:output] || $stdout
|
110
|
+
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
|
111
|
+
if options[:format] == :jsonld
|
112
|
+
if files.empty?
|
113
|
+
# If files are empty, either use options[:execute]
|
114
|
+
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
|
115
|
+
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
|
116
|
+
JSON::LD::API.flatten(input, options[:context], options) do |flattened|
|
117
|
+
out.puts flattened.to_json(JSON::LD::JSON_STATE)
|
118
|
+
end
|
119
|
+
else
|
120
|
+
files.each do |file|
|
121
|
+
JSON::LD::API.flatten(file, options[:context], options) do |flattened|
|
122
|
+
out.puts flattened.to_json(JSON::LD::JSON_STATE)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
else
|
127
|
+
# Turn RDF into JSON-LD first
|
128
|
+
RDF::CLI.parse(files, options) do |reader|
|
129
|
+
JSON::LD::API.fromRdf(reader) do |expanded|
|
130
|
+
JSON::LD::API.flatten(expanded, options[:context], options) do |flattened|
|
131
|
+
out.puts flattened.to_json(JSON::LD::JSON_STATE)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end,
|
137
|
+
frame: ->(files, options) do
|
138
|
+
raise ArgumentError, "Framing requires a frame" unless options[:frame]
|
139
|
+
out = options[:output] || $stdout
|
140
|
+
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
|
141
|
+
if options[:format] == :jsonld
|
142
|
+
if files.empty?
|
143
|
+
# If files are empty, either use options[:execute]
|
144
|
+
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
|
145
|
+
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
|
146
|
+
JSON::LD::API.frame(input, options[:frame], options) do |framed|
|
147
|
+
out.puts framed.to_json(JSON::LD::JSON_STATE)
|
148
|
+
end
|
149
|
+
else
|
150
|
+
files.each do |file|
|
151
|
+
JSON::LD::API.frame(file, options[:frame], options) do |framed|
|
152
|
+
out.puts framed.to_json(JSON::LD::JSON_STATE)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
else
|
157
|
+
# Turn RDF into JSON-LD first
|
158
|
+
RDF::CLI.parse(files, options) do |reader|
|
159
|
+
JSON::LD::API.fromRdf(reader) do |expanded|
|
160
|
+
JSON::LD::API.frame(expanded, options[:frame], options) do |framed|
|
161
|
+
out.puts framed.to_json(JSON::LD::JSON_STATE)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end,
|
167
|
+
}
|
168
|
+
end
|
169
|
+
|
45
170
|
##
|
46
171
|
# Override normal symbol generation
|
47
172
|
def self.to_sym
|
data/lib/json/ld/frame.rb
CHANGED
@@ -17,7 +17,7 @@ module JSON::LD
|
|
17
17
|
# The parent property.
|
18
18
|
# @raise [JSON::LD::InvalidFrame]
|
19
19
|
def frame(state, subjects, frame, options = {})
|
20
|
-
|
20
|
+
log_depth do
|
21
21
|
parent, property = options[:parent], options[:property]
|
22
22
|
# Validate the frame
|
23
23
|
validate_frame(state, frame)
|
@@ -151,7 +151,7 @@ module JSON::LD
|
|
151
151
|
# @param [Array, Hash] input
|
152
152
|
# @return [Array, Hash]
|
153
153
|
def cleanup_preserve(input)
|
154
|
-
|
154
|
+
log_depth do
|
155
155
|
result = case input
|
156
156
|
when Array
|
157
157
|
# If, after replacement, an array contains only the value null remove the value, leaving an empty array.
|
@@ -337,7 +337,7 @@ module JSON::LD
|
|
337
337
|
# recursively remove dependent dangling embeds
|
338
338
|
def remove_dependents(id, embeds)
|
339
339
|
|
340
|
-
|
340
|
+
log_depth do
|
341
341
|
# get embed keys as a separate array to enable deleting keys in map
|
342
342
|
embeds.each do |id_dep, e|
|
343
343
|
p = e.fetch(:parent, {}) if e.is_a?(Hash)
|
data/lib/json/ld/from_rdf.rb
CHANGED
@@ -22,7 +22,7 @@ module JSON::LD
|
|
22
22
|
|
23
23
|
# For each triple in input
|
24
24
|
input.each do |statement|
|
25
|
-
|
25
|
+
log_debug("statement") { statement.to_nquads.chomp}
|
26
26
|
|
27
27
|
name = statement.graph_name ? ec.expand_iri(statement.graph_name).to_s : '@default'
|
28
28
|
|
@@ -71,7 +71,7 @@ module JSON::LD
|
|
71
71
|
list, list_nodes = [], []
|
72
72
|
|
73
73
|
# If property equals rdf:rest, the value associated to the usages member of node has exactly 1 entry, node has a rdf:first and rdf:rest property, both of which have as value an array consisting of a single element, and node has no other members apart from an optional @type member whose value is an array with a single item equal to rdf:List, node represents a well-formed list node. Continue with the following steps:
|
74
|
-
|
74
|
+
log_debug("list element?") {node.to_json(JSON_STATE) rescue 'malformed json'}
|
75
75
|
while property == RDF.rest.to_s &&
|
76
76
|
node_usages_map[node['@id']].uniq.length == 1 &&
|
77
77
|
blank_node?(node) &&
|
@@ -118,7 +118,7 @@ module JSON::LD
|
|
118
118
|
node.delete(:usages)
|
119
119
|
result << node unless node_reference?(node)
|
120
120
|
end
|
121
|
-
|
121
|
+
log_debug("fromRdf") {result.to_json(JSON_STATE) rescue 'malformed json'}
|
122
122
|
result
|
123
123
|
end
|
124
124
|
end
|
data/lib/json/ld/reader.rb
CHANGED
@@ -7,6 +7,19 @@ module JSON::LD
|
|
7
7
|
class Reader < RDF::Reader
|
8
8
|
format Format
|
9
9
|
|
10
|
+
##
|
11
|
+
# JSON-LD Reader options
|
12
|
+
# @see http://www.rubydoc.info/github/ruby-rdf/rdf/RDF/Reader#options-class_method
|
13
|
+
def self.options
|
14
|
+
super + [
|
15
|
+
RDF::CLI::Option.new(
|
16
|
+
symbol: :compactArrays,
|
17
|
+
datatype: TrueClass,
|
18
|
+
on: ["--compact-arrays"],
|
19
|
+
description: "Replaces arrays with just one element with that element during compaction.") {true},
|
20
|
+
]
|
21
|
+
end
|
22
|
+
|
10
23
|
##
|
11
24
|
# Initializes the RDF/JSON reader instance.
|
12
25
|
#
|
@@ -28,9 +41,6 @@ module JSON::LD
|
|
28
41
|
else
|
29
42
|
StringIO.new(input.to_s.sub(%r(\A[^{\[]*)m, '').sub(%r([^}\]]*\Z)m, ''))
|
30
43
|
end
|
31
|
-
rescue JSON::ParserError => e
|
32
|
-
raise RDF::ReaderError, "Failed to parse input document: #{e.message}" if validate?
|
33
|
-
@doc = StringIO.new("{}")
|
34
44
|
end
|
35
45
|
|
36
46
|
if block_given?
|
@@ -47,8 +57,8 @@ module JSON::LD
|
|
47
57
|
# @see RDF::Reader#each_statement
|
48
58
|
def each_statement(&block)
|
49
59
|
JSON::LD::API.toRdf(@doc, @options, &block)
|
50
|
-
rescue ::JSON::LD::JsonLdError => e
|
51
|
-
|
60
|
+
rescue ::JSON::ParserError, ::JSON::LD::JsonLdError => e
|
61
|
+
log_fatal("Failed to parse input document: #{e.message}", exception: RDF::ReaderError)
|
52
62
|
end
|
53
63
|
|
54
64
|
##
|
@@ -17,7 +17,7 @@ module JSON::LD
|
|
17
17
|
else Context.new.parse(@options[:context])
|
18
18
|
end
|
19
19
|
|
20
|
-
|
20
|
+
log_debug("prologue") {"context: #{context.inspect}"}
|
21
21
|
if context
|
22
22
|
@output.puts %({"@context": #{context.serialize['@context'].to_json}, "@graph": [)
|
23
23
|
else
|
@@ -37,7 +37,7 @@ module JSON::LD
|
|
37
37
|
#
|
38
38
|
# @return [void] `self`
|
39
39
|
def stream_statement(statement)
|
40
|
-
|
40
|
+
log_debug("ss") {"state: #{@state.inspect}, stmt: #{statement}"}
|
41
41
|
if @current_graph != statement.graph_name
|
42
42
|
end_graph
|
43
43
|
start_graph(statement.graph_name)
|
@@ -72,7 +72,7 @@ module JSON::LD
|
|
72
72
|
# Complete open statements
|
73
73
|
# @return [void] `self`
|
74
74
|
def stream_epilogue
|
75
|
-
|
75
|
+
log_debug("epilogue") {"state: #{@state.inspect}"}
|
76
76
|
end_graph
|
77
77
|
if context
|
78
78
|
@output.puts "\n]}"
|
@@ -85,7 +85,7 @@ module JSON::LD
|
|
85
85
|
private
|
86
86
|
|
87
87
|
def start_graph(resource)
|
88
|
-
|
88
|
+
log_debug("start_graph") {"state: #{@state.inspect}, resource: #{resource}"}
|
89
89
|
if resource
|
90
90
|
@output.puts(",") if [:wrote_node, :wrote_graph].include?(@state)
|
91
91
|
@output.puts %({"@id": "#{resource}", "@graph": [)
|
@@ -95,7 +95,7 @@ module JSON::LD
|
|
95
95
|
end
|
96
96
|
|
97
97
|
def end_graph
|
98
|
-
|
98
|
+
log_debug("end_graph") {"state: #{@state.inspect}, ctx: #{@current_graph}"}
|
99
99
|
end_node
|
100
100
|
if @current_graph
|
101
101
|
@output.write %(]})
|
@@ -104,7 +104,7 @@ module JSON::LD
|
|
104
104
|
end
|
105
105
|
|
106
106
|
def end_node
|
107
|
-
|
107
|
+
log_debug("end_node") {"state: #{@state.inspect}, node: #{@current_node_def.to_json}"}
|
108
108
|
@output.puts(",") if [:wrote_node, :wrote_graph].include?(@state)
|
109
109
|
if @current_node_def
|
110
110
|
node_def = if context
|