json-ld 3.1.3 → 3.1.8

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.
@@ -64,8 +64,8 @@ module JSON::LD
64
64
  {"@value" => MultiJson.load(statement.object.to_s), "@type" => "@json"}
65
65
  else
66
66
  lit = {"@value" => statement.object.to_s}
67
- lit["@type"] = statement.object.datatype.to_s if statement.object.has_datatype?
68
- lit["@language"] = statement.object.language.to_s if statement.object.has_language?
67
+ lit["@type"] = statement.object.datatype.to_s if statement.object.datatype?
68
+ lit["@language"] = statement.object.language.to_s if statement.object.language?
69
69
  lit
70
70
  end
71
71
  end
@@ -91,7 +91,7 @@ module JSON::LD
91
91
  def start_graph(resource)
92
92
  #log_debug("start_graph") {"state: #{@state.inspect}, resource: #{resource}"}
93
93
  if resource
94
- @output.puts(",") if [:wrote_node, :wrote_graph].include?(@state)
94
+ @output.puts(",") if %i(wrote_node wrote_graph).include?(@state)
95
95
  @output.puts %({"@id": "#{resource}", "@graph": [)
96
96
  @state = :in_graph
97
97
  end
@@ -109,7 +109,7 @@ module JSON::LD
109
109
 
110
110
  def end_node
111
111
  #log_debug("end_node") {"state: #{@state.inspect}, node: #{@current_node_def.to_json}"}
112
- @output.puts(",") if [:wrote_node, :wrote_graph].include?(@state)
112
+ @output.puts(",") if %i(wrote_node wrote_graph).include?(@state)
113
113
  if @current_node_def
114
114
  node_def = if context
115
115
  compacted = JSON::LD::API.compact(@current_node_def, context, rename_bnodes: false, **@options)
@@ -16,6 +16,8 @@ module JSON::LD
16
16
  # @return RDF::Resource the subject of this item
17
17
  def item_to_rdf(item, graph_name: nil, &block)
18
18
  # Just return value object as Term
19
+ return unless item
20
+
19
21
  if value?(item)
20
22
  value, datatype = item.fetch('@value'), item.fetch('@type', nil)
21
23
 
@@ -46,7 +48,7 @@ module JSON::LD
46
48
  # Only valid for rdf:JSON
47
49
  value = value.to_json_c14n
48
50
  else
49
- if item.has_key?('@direction') && @options[:rdfDirection]
51
+ if item.key?('@direction') && @options[:rdfDirection]
50
52
  # Either serialize using a datatype, or a compound-literal
51
53
  case @options[:rdfDirection]
52
54
  when 'i18n-datatype'
@@ -61,7 +63,7 @@ module JSON::LD
61
63
  end
62
64
 
63
65
  # Otherwise, if datatype is null, set it to xsd:string or xsd:langString, depending on if item has a @language key.
64
- datatype ||= item.has_key?('@language') ? RDF.langString : RDF::XSD.string
66
+ datatype ||= item.key?('@language') ? RDF.langString : RDF::XSD.string
65
67
  if datatype == RDF::URI(RDF.to_uri + "JSON")
66
68
  value = value.to_json_c14n
67
69
  end
@@ -76,11 +78,13 @@ module JSON::LD
76
78
  return parse_list(item['@list'], graph_name: graph_name, &block)
77
79
  end
78
80
 
79
- # Skip if '@id' is nil
80
- subject = if item.has_key?('@id')
81
- item['@id'].nil? ? nil : as_resource(item['@id'])
82
- else
83
- node
81
+ subject = case item['@id']
82
+ when nil then node
83
+ when String then as_resource(item['@id'])
84
+ when Object
85
+ # Embedded statement
86
+ # (No error checking, as this is done in expansion)
87
+ to_enum(:item_to_rdf, item['@id']).to_a.first
84
88
  end
85
89
 
86
90
  #log_debug("item_to_rdf") {"subject: #{subject.to_ntriples rescue 'malformed rdf'}"}
data/lib/json/ld/utils.rb CHANGED
@@ -11,8 +11,8 @@ module JSON::LD
11
11
  # @return [Boolean]
12
12
  def node?(value)
13
13
  value.is_a?(Hash) &&
14
- !(value.has_key?('@value') || value.has_key?('@list') || value.has_key?('@set')) &&
15
- (value.length > 1 || !value.has_key?('@id'))
14
+ !(value.key?('@value') || value.key?('@list') || value.key?('@set')) &&
15
+ (value.length > 1 || !value.key?('@id'))
16
16
  end
17
17
 
18
18
  ##
@@ -29,7 +29,7 @@ module JSON::LD
29
29
  # @return [Boolean]
30
30
  def node_or_ref?(value)
31
31
  value.is_a?(Hash) &&
32
- !(value.has_key?('@value') || value.has_key?('@list') || value.has_key?('@set'))
32
+ !(value.key?('@value') || value.key?('@list') || value.key?('@set'))
33
33
  end
34
34
 
35
35
  ##
@@ -66,7 +66,7 @@ module JSON::LD
66
66
  # @param [Object] value
67
67
  # @return [Boolean]
68
68
  def simple_graph?(value)
69
- graph?(value) && !value.has_key?('@id')
69
+ graph?(value) && !value.key?('@id')
70
70
  end
71
71
 
72
72
  ##
@@ -75,7 +75,7 @@ module JSON::LD
75
75
  # @param [Object] value
76
76
  # @return [Boolean]
77
77
  def list?(value)
78
- value.is_a?(Hash) && value.has_key?('@list')
78
+ value.is_a?(Hash) && value.key?('@list')
79
79
  end
80
80
 
81
81
  ##
@@ -84,7 +84,7 @@ module JSON::LD
84
84
  # @param [Object] value
85
85
  # @return [Boolean]
86
86
  def index?(value)
87
- value.is_a?(Hash) && value.has_key?('@index')
87
+ value.is_a?(Hash) && value.key?('@index')
88
88
  end
89
89
 
90
90
  ##
@@ -93,7 +93,7 @@ module JSON::LD
93
93
  # @param [Object] value
94
94
  # @return [Boolean]
95
95
  def value?(value)
96
- value.is_a?(Hash) && value.has_key?('@value')
96
+ value.is_a?(Hash) && value.key?('@value')
97
97
  end
98
98
 
99
99
  ##
@@ -170,7 +170,7 @@ module JSON::LD
170
170
  end
171
171
  elsif subject[property]
172
172
  # check if subject already has value if duplicates not allowed
173
- _has_value = !allow_duplicate && has_value(subject, property, value)
173
+ _has_value = !allow_duplicate && has_value?(subject, property, value)
174
174
 
175
175
  # make property an array if value not present or always an array
176
176
  if !subject[property].is_a?(Array) && (!_has_value || property_is_array)
@@ -188,7 +188,7 @@ module JSON::LD
188
188
  # @param property the property to look for.
189
189
  #
190
190
  # @return [Boolean] true if the subject has the given property, false if not.
191
- def has_property(subject, property)
191
+ def property?(subject, property)
192
192
  return false unless value = subject[property]
193
193
  !value.is_a?(Array) || !value.empty?
194
194
  end
@@ -200,8 +200,8 @@ module JSON::LD
200
200
  # @param [Object] value the value to check.
201
201
  #
202
202
  # @return [Boolean] true if the value exists, false if not.
203
- def has_value(subject, property, value)
204
- if has_property(subject, property)
203
+ def has_value?(subject, property, value)
204
+ if property?(subject, property)
205
205
  val = subject[property]
206
206
  is_list = list?(val)
207
207
  if val.is_a?(Array) || is_list
@@ -265,7 +265,7 @@ module JSON::LD
265
265
  # @return [String]
266
266
  def get_sym(old = "")
267
267
  old = old.to_s.sub(/_:/, '')
268
- if old && self.has_key?(old)
268
+ if old && self.key?(old)
269
269
  self[old]
270
270
  elsif !old.empty?
271
271
  self[old] = RDF::Node.new.to_unique_base[2..-1]
@@ -289,7 +289,7 @@ module JSON::LD
289
289
  # @return [String]
290
290
  def get_sym(old = "")
291
291
  old = old.to_s.sub(/_:/, '')
292
- if !old.empty? && self.has_key?(old)
292
+ if !old.empty? && self.key?(old)
293
293
  self[old]
294
294
  elsif !old.empty?
295
295
  @num += 1
@@ -93,7 +93,7 @@ module JSON::LD
93
93
  datatype: RDF::URI,
94
94
  control: :url2,
95
95
  on: ["--context CONTEXT"],
96
- description: "Context to use when compacting.") {|arg| RDF::URI(arg)},
96
+ description: "Context to use when compacting.") {|arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))},
97
97
  RDF::CLI::Option.new(
98
98
  symbol: :embed,
99
99
  datatype: %w(@always @once @never),
@@ -107,6 +107,13 @@ module JSON::LD
107
107
  control: :checkbox,
108
108
  on: ["--[no-]explicit"],
109
109
  description: "Only include explicitly declared properties in output (false)") {|arg| arg},
110
+ RDF::CLI::Option.new(
111
+ symbol: :frame,
112
+ datatype: RDF::URI,
113
+ control: :url2,
114
+ use: :required,
115
+ on: ["--frame FRAME"],
116
+ description: "Frame to use when serializing.") {|arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))},
110
117
  RDF::CLI::Option.new(
111
118
  symbol: :lowercaseLanguage,
112
119
  datatype: TrueClass,
@@ -137,7 +144,7 @@ module JSON::LD
137
144
  default: 'null',
138
145
  control: :select,
139
146
  on: ["--rdf-direction DIR", %w(i18n-datatype compound-literal)],
140
- description: "How to serialize literal direction (i18n-datatype compound-literal)") {|arg| RDF::URI(arg)},
147
+ description: "How to serialize literal direction (i18n-datatype compound-literal)") {|arg| arg},
141
148
  RDF::CLI::Option.new(
142
149
  symbol: :requireAll,
143
150
  datatype: TrueClass,
@@ -202,7 +209,7 @@ module JSON::LD
202
209
  end
203
210
 
204
211
  ##
205
- # Initializes the RDF-LD writer instance.
212
+ # Initializes the JSON-LD writer instance.
206
213
  #
207
214
  # @param [IO, File] output
208
215
  # the output stream
@@ -230,8 +237,8 @@ module JSON::LD
230
237
  # @yield [writer]
231
238
  # @yieldparam [RDF::Writer] writer
232
239
  def initialize(output = $stdout, **options, &block)
233
- options[:base_uri] ||= options[:base] if options.has_key?(:base)
234
- options[:base] ||= options[:base_uri] if options.has_key?(:base_uri)
240
+ options[:base_uri] ||= options[:base] if options.key?(:base)
241
+ options[:base] ||= options[:base_uri] if options.key?(:base_uri)
235
242
  super do
236
243
  @repo = RDF::Repository.new
237
244
 
data/spec/api_spec.rb CHANGED
@@ -44,7 +44,7 @@ describe JSON::LD::API do
44
44
  end
45
45
 
46
46
  context "Test Files" do
47
- %w(oj json_gem ok_json yajl).map(&:to_sym).each do |adapter|
47
+ %i(oj json_gem ok_json yajl).each do |adapter|
48
48
  context "with MultiJson adapter #{adapter.inspect}" do
49
49
  Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), 'test-files/*-input.*'))) do |filename|
50
50
  test = File.basename(filename).sub(/-input\..*$/, '')
data/spec/compact_spec.rb CHANGED
@@ -3113,6 +3113,210 @@ describe JSON::LD::API do
3113
3113
  end
3114
3114
  end
3115
3115
 
3116
+ context "JSON-LD*" do
3117
+ {
3118
+ "subject-iii": {
3119
+ input: %([{
3120
+ "@id": {
3121
+ "@id": "http://example/s1",
3122
+ "http://example/p1": [{"@id": "http://example/o1"}]
3123
+ },
3124
+ "http://example/p": [{"@id": "http://example/o"}]
3125
+ }]),
3126
+ context: %({"ex": "http://example/"}),
3127
+ output: %({
3128
+ "@context": {"ex": "http://example/"},
3129
+ "@id": {
3130
+ "@id": "ex:s1",
3131
+ "ex:p1": {"@id": "ex:o1"}
3132
+ },
3133
+ "ex:p": {"@id": "ex:o"}
3134
+ })
3135
+ },
3136
+ "subject-iib": {
3137
+ input: %([{
3138
+ "@id": {
3139
+ "@id": "http://example/s1",
3140
+ "http://example/p1": [{"@id": "_:o1"}]
3141
+ },
3142
+ "http://example/p": [{"@id": "http://example/o"}]
3143
+ }]),
3144
+ context: %({"ex": "http://example/"}),
3145
+ output: %({
3146
+ "@context": {"ex": "http://example/"},
3147
+ "@id": {
3148
+ "@id": "ex:s1",
3149
+ "ex:p1": {"@id": "_:o1"}
3150
+ },
3151
+ "ex:p": {"@id": "ex:o"}
3152
+ })
3153
+ },
3154
+ "subject-iil": {
3155
+ input: %([{
3156
+ "@id": {
3157
+ "@id": "http://example/s1",
3158
+ "http://example/p1": [{"@value": "o1"}]
3159
+ },
3160
+ "http://example/p": [{"@id": "http://example/o"}]
3161
+ }]),
3162
+ context: %({"ex": "http://example/"}),
3163
+ output: %({
3164
+ "@context": {"ex": "http://example/"},
3165
+ "@id": {
3166
+ "@id": "ex:s1",
3167
+ "ex:p1": "o1"
3168
+ },
3169
+ "ex:p": {"@id": "ex:o"}
3170
+ })
3171
+ },
3172
+ "subject-bii": {
3173
+ input: %([{
3174
+ "@id": {
3175
+ "@id": "_:s1",
3176
+ "http://example/p1": [{"@id": "http://example/o1"}]
3177
+ },
3178
+ "http://example/p": [{"@id": "http://example/o"}]
3179
+ }]),
3180
+ context: %({"ex": "http://example/"}),
3181
+ output: %({
3182
+ "@context": {"ex": "http://example/"},
3183
+ "@id": {
3184
+ "@id": "_:s1",
3185
+ "ex:p1": {"@id": "ex:o1"}
3186
+ },
3187
+ "ex:p": {"@id": "ex:o"}
3188
+ })
3189
+ },
3190
+ "subject-bib": {
3191
+ input: %([{
3192
+ "@id": {
3193
+ "@id": "_:s1",
3194
+ "http://example/p1": [{"@id": "_:o1"}]
3195
+ },
3196
+ "http://example/p": [{"@id": "http://example/o"}]
3197
+ }]),
3198
+ context: %({"ex": "http://example/"}),
3199
+ output: %({
3200
+ "@context": {"ex": "http://example/"},
3201
+ "@id": {
3202
+ "@id": "_:s1",
3203
+ "ex:p1": {"@id": "_:o1"}
3204
+ },
3205
+ "ex:p": {"@id": "ex:o"}
3206
+ })
3207
+ },
3208
+ "subject-bil": {
3209
+ input: %([{
3210
+ "@id": {
3211
+ "@id": "_:s1",
3212
+ "http://example/p1": [{"@value": "o1"}]
3213
+ },
3214
+ "http://example/p": [{"@id": "http://example/o"}]
3215
+ }]),
3216
+ context: %({"ex": "http://example/"}),
3217
+ output: %({
3218
+ "@context": {"ex": "http://example/"},
3219
+ "@id": {
3220
+ "@id": "_:s1",
3221
+ "ex:p1": "o1"
3222
+ },
3223
+ "ex:p": {"@id": "ex:o"}
3224
+ })
3225
+ },
3226
+ "object-iii": {
3227
+ input: %([{
3228
+ "@id": "http://example/s",
3229
+ "http://example/p": [{
3230
+ "@id": {
3231
+ "@id": "http://example/s1",
3232
+ "http://example/p1": [{"@id": "http://example/o1"}]
3233
+ }
3234
+ }]
3235
+ }]),
3236
+ context: %({"ex": "http://example/"}),
3237
+ output: %({
3238
+ "@context": {"ex": "http://example/"},
3239
+ "@id": "ex:s",
3240
+ "ex:p": {
3241
+ "@id": {
3242
+ "@id": "ex:s1",
3243
+ "ex:p1": {"@id": "ex:o1"}
3244
+ }
3245
+ }
3246
+ })
3247
+ },
3248
+ "object-iib": {
3249
+ input: %([{
3250
+ "@id": "http://example/s",
3251
+ "http://example/p": [{
3252
+ "@id": {
3253
+ "@id": "http://example/s1",
3254
+ "http://example/p1": [{"@id": "_:o1"}]
3255
+ }
3256
+ }]
3257
+ }]),
3258
+ context: %({"ex": "http://example/"}),
3259
+ output: %({
3260
+ "@context": {"ex": "http://example/"},
3261
+ "@id": "ex:s",
3262
+ "ex:p": {
3263
+ "@id": {
3264
+ "@id": "ex:s1",
3265
+ "ex:p1": {"@id": "_:o1"}
3266
+ }
3267
+ }
3268
+ })
3269
+ },
3270
+ "object-iil": {
3271
+ input: %([{
3272
+ "@id": "http://example/s",
3273
+ "http://example/p": [{
3274
+ "@id": {
3275
+ "@id": "http://example/s1",
3276
+ "http://example/p1": [{"@value": "o1"}]
3277
+ }
3278
+ }]
3279
+ }]),
3280
+ context: %({"ex": "http://example/"}),
3281
+ output: %({
3282
+ "@context": {"ex": "http://example/"},
3283
+ "@id": "ex:s",
3284
+ "ex:p": {
3285
+ "@id": {
3286
+ "@id": "ex:s1",
3287
+ "ex:p1": "o1"
3288
+ }
3289
+ }
3290
+ })
3291
+ },
3292
+ "recursive-subject": {
3293
+ input: %([{
3294
+ "@id": {
3295
+ "@id": {
3296
+ "@id": "http://example/s2",
3297
+ "http://example/p2": [{"@id": "http://example/o2"}]
3298
+ },
3299
+ "http://example/p1": [{"@id": "http://example/o1"}]
3300
+ },
3301
+ "http://example/p": [{"@id": "http://example/o"}]
3302
+ }]),
3303
+ context: %({"ex": "http://example/"}),
3304
+ output: %({
3305
+ "@context": {"ex": "http://example/"},
3306
+ "@id": {
3307
+ "@id": {
3308
+ "@id": "ex:s2",
3309
+ "ex:p2": {"@id": "ex:o2"}
3310
+ },
3311
+ "ex:p1": {"@id": "ex:o1"}
3312
+ },
3313
+ "ex:p": {"@id": "ex:o"}
3314
+ })
3315
+ },
3316
+ }.each do |name, params|
3317
+ it(name) {run_compact(params.merge(rdfstar: true))}
3318
+ end
3319
+ end
3116
3320
 
3117
3321
  context "problem cases" do
3118
3322
  {
@@ -3185,7 +3389,7 @@ describe JSON::LD::API do
3185
3389
  input = ::JSON.parse(input) if input.is_a?(String)
3186
3390
  output = ::JSON.parse(output) if output.is_a?(String)
3187
3391
  context = ::JSON.parse(context) if context.is_a?(String)
3188
- context = context['@context'] if context.has_key?('@context')
3392
+ context = context['@context'] if context.key?('@context')
3189
3393
  pending params.fetch(:pending, "test implementation") unless input
3190
3394
  if params[:exception]
3191
3395
  expect {JSON::LD::API.compact(input, context, logger: logger, **params)}.to raise_error(params[:exception])
@@ -3200,8 +3404,8 @@ describe JSON::LD::API do
3200
3404
  expect(jld).to produce_jsonld(output, logger)
3201
3405
 
3202
3406
  # Compare expanded jld/output too to make sure list values remain ordered
3203
- exp_jld = JSON::LD::API.expand(jld, processingMode: 'json-ld-1.1')
3204
- exp_output = JSON::LD::API.expand(output, processingMode: 'json-ld-1.1')
3407
+ exp_jld = JSON::LD::API.expand(jld, processingMode: 'json-ld-1.1', rdfstar: params[:rdfstar])
3408
+ exp_output = JSON::LD::API.expand(output, processingMode: 'json-ld-1.1', rdfstar: params[:rdfstar])
3205
3409
  expect(exp_jld).to produce_jsonld(exp_output, logger)
3206
3410
  end
3207
3411
  end