json-ld 3.2.3 → 3.2.5
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/lib/json/ld/api.rb +807 -764
- data/lib/json/ld/compact.rb +304 -304
- data/lib/json/ld/conneg.rb +179 -161
- data/lib/json/ld/context.rb +2080 -1945
- data/lib/json/ld/expand.rb +745 -666
- data/lib/json/ld/extensions.rb +14 -13
- data/lib/json/ld/flatten.rb +257 -247
- data/lib/json/ld/format.rb +202 -194
- data/lib/json/ld/frame.rb +525 -502
- data/lib/json/ld/from_rdf.rb +224 -166
- data/lib/json/ld/html/nokogiri.rb +123 -121
- data/lib/json/ld/html/rexml.rb +151 -147
- data/lib/json/ld/reader.rb +107 -100
- data/lib/json/ld/resource.rb +224 -205
- data/lib/json/ld/streaming_reader.rb +574 -507
- data/lib/json/ld/streaming_writer.rb +93 -92
- data/lib/json/ld/to_rdf.rb +171 -167
- data/lib/json/ld/utils.rb +270 -264
- data/lib/json/ld/version.rb +24 -14
- data/lib/json/ld/writer.rb +334 -311
- data/lib/json/ld.rb +103 -96
- metadata +78 -209
- data/spec/api_spec.rb +0 -132
- data/spec/compact_spec.rb +0 -3482
- data/spec/conneg_spec.rb +0 -373
- data/spec/context_spec.rb +0 -2036
- data/spec/expand_spec.rb +0 -4496
- data/spec/flatten_spec.rb +0 -1203
- data/spec/format_spec.rb +0 -115
- data/spec/frame_spec.rb +0 -2498
- data/spec/from_rdf_spec.rb +0 -1005
- data/spec/matchers.rb +0 -20
- data/spec/rdfstar_spec.rb +0 -25
- data/spec/reader_spec.rb +0 -883
- data/spec/resource_spec.rb +0 -76
- data/spec/spec_helper.rb +0 -281
- data/spec/streaming_reader_spec.rb +0 -237
- data/spec/streaming_writer_spec.rb +0 -145
- data/spec/suite_compact_spec.rb +0 -22
- data/spec/suite_expand_spec.rb +0 -36
- data/spec/suite_flatten_spec.rb +0 -34
- data/spec/suite_frame_spec.rb +0 -29
- data/spec/suite_from_rdf_spec.rb +0 -22
- data/spec/suite_helper.rb +0 -411
- data/spec/suite_html_spec.rb +0 -22
- data/spec/suite_http_spec.rb +0 -35
- data/spec/suite_remote_doc_spec.rb +0 -22
- data/spec/suite_to_rdf_spec.rb +0 -30
- data/spec/support/extensions.rb +0 -44
- data/spec/test-files/test-1-compacted.jsonld +0 -10
- data/spec/test-files/test-1-context.jsonld +0 -7
- data/spec/test-files/test-1-expanded.jsonld +0 -5
- data/spec/test-files/test-1-input.jsonld +0 -10
- data/spec/test-files/test-1-rdf.ttl +0 -8
- data/spec/test-files/test-2-compacted.jsonld +0 -20
- data/spec/test-files/test-2-context.jsonld +0 -7
- data/spec/test-files/test-2-expanded.jsonld +0 -16
- data/spec/test-files/test-2-input.jsonld +0 -20
- data/spec/test-files/test-2-rdf.ttl +0 -14
- data/spec/test-files/test-3-compacted.jsonld +0 -11
- data/spec/test-files/test-3-context.jsonld +0 -8
- data/spec/test-files/test-3-expanded.jsonld +0 -10
- data/spec/test-files/test-3-input.jsonld +0 -11
- data/spec/test-files/test-3-rdf.ttl +0 -8
- data/spec/test-files/test-4-compacted.jsonld +0 -10
- data/spec/test-files/test-4-context.jsonld +0 -7
- data/spec/test-files/test-4-expanded.jsonld +0 -6
- data/spec/test-files/test-4-input.jsonld +0 -10
- data/spec/test-files/test-4-rdf.ttl +0 -5
- data/spec/test-files/test-5-compacted.jsonld +0 -13
- data/spec/test-files/test-5-context.jsonld +0 -7
- data/spec/test-files/test-5-expanded.jsonld +0 -9
- data/spec/test-files/test-5-input.jsonld +0 -13
- data/spec/test-files/test-5-rdf.ttl +0 -7
- data/spec/test-files/test-6-compacted.jsonld +0 -10
- data/spec/test-files/test-6-context.jsonld +0 -7
- data/spec/test-files/test-6-expanded.jsonld +0 -10
- data/spec/test-files/test-6-input.jsonld +0 -10
- data/spec/test-files/test-6-rdf.ttl +0 -6
- data/spec/test-files/test-7-compacted.jsonld +0 -23
- data/spec/test-files/test-7-context.jsonld +0 -4
- data/spec/test-files/test-7-expanded.jsonld +0 -20
- data/spec/test-files/test-7-input.jsonld +0 -23
- data/spec/test-files/test-7-rdf.ttl +0 -14
- data/spec/test-files/test-8-compacted.jsonld +0 -34
- data/spec/test-files/test-8-context.jsonld +0 -11
- data/spec/test-files/test-8-expanded.jsonld +0 -24
- data/spec/test-files/test-8-frame.jsonld +0 -18
- data/spec/test-files/test-8-framed.jsonld +0 -25
- data/spec/test-files/test-8-input.jsonld +0 -30
- data/spec/test-files/test-8-rdf.ttl +0 -15
- data/spec/test-files/test-9-compacted.jsonld +0 -20
- data/spec/test-files/test-9-context.jsonld +0 -13
- data/spec/test-files/test-9-expanded.jsonld +0 -14
- data/spec/test-files/test-9-input.jsonld +0 -12
- data/spec/to_rdf_spec.rb +0 -1551
- data/spec/writer_spec.rb +0 -427
data/lib/json/ld/writer.rb
CHANGED
@@ -1,344 +1,367 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
1
|
# frozen_string_literal: true
|
2
|
+
|
3
3
|
require 'json/ld/streaming_writer'
|
4
4
|
require 'link_header'
|
5
5
|
|
6
|
-
module JSON
|
7
|
-
|
8
|
-
# A JSON-LD parser in Ruby.
|
9
|
-
#
|
10
|
-
# Note that the natural interface is to write a whole graph at a time.
|
11
|
-
# Writing statements or Triples will create a graph to add them to
|
12
|
-
# and then serialize the graph.
|
13
|
-
#
|
14
|
-
# @example Obtaining a JSON-LD writer class
|
15
|
-
# RDF::Writer.for(:jsonld) #=> JSON::LD::Writer
|
16
|
-
# RDF::Writer.for("etc/test.json")
|
17
|
-
# RDF::Writer.for(:file_name => "etc/test.json")
|
18
|
-
# RDF::Writer.for(file_extension: "json")
|
19
|
-
# RDF::Writer.for(:content_type => "application/turtle")
|
20
|
-
#
|
21
|
-
# @example Serializing RDF graph into an JSON-LD file
|
22
|
-
# JSON::LD::Writer.open("etc/test.json") do |writer|
|
23
|
-
# writer << graph
|
24
|
-
# end
|
25
|
-
#
|
26
|
-
# @example Serializing RDF statements into an JSON-LD file
|
27
|
-
# JSON::LD::Writer.open("etc/test.json") do |writer|
|
28
|
-
# graph.each_statement do |statement|
|
29
|
-
# writer << statement
|
30
|
-
# end
|
31
|
-
# end
|
32
|
-
#
|
33
|
-
# @example Serializing RDF statements into an JSON-LD string
|
34
|
-
# JSON::LD::Writer.buffer do |writer|
|
35
|
-
# graph.each_statement do |statement|
|
36
|
-
# writer << statement
|
37
|
-
# end
|
38
|
-
# end
|
39
|
-
#
|
40
|
-
# The writer will add prefix definitions, and use them for creating @context definitions, and minting CURIEs
|
41
|
-
#
|
42
|
-
# @example Creating @@context prefix definitions in output
|
43
|
-
# JSON::LD::Writer.buffer(
|
44
|
-
# prefixes: {
|
45
|
-
# nil => "http://example.com/ns#",
|
46
|
-
# foaf: "http://xmlns.com/foaf/0.1/"}
|
47
|
-
# ) do |writer|
|
48
|
-
# graph.each_statement do |statement|
|
49
|
-
# writer << statement
|
50
|
-
# end
|
51
|
-
# end
|
52
|
-
#
|
53
|
-
# Select the :expand option to output JSON-LD in expanded form
|
54
|
-
#
|
55
|
-
# @see https://www.w3.org/TR/json-ld11-api/
|
56
|
-
# @see https://www.w3.org/TR/json-ld11-api/#the-normalization-algorithm
|
57
|
-
# @author [Gregg Kellogg](http://greggkellogg.net/)
|
58
|
-
class Writer < RDF::Writer
|
59
|
-
include StreamingWriter
|
60
|
-
include Utils
|
61
|
-
include RDF::Util::Logger
|
62
|
-
format Format
|
63
|
-
|
64
|
-
# @!attribute [r] graph
|
65
|
-
# @return [RDF::Graph] Graph of statements serialized
|
66
|
-
attr_reader :graph
|
67
|
-
|
68
|
-
# @!attribute [r] context
|
69
|
-
# @return [Context] context used to load and administer contexts
|
70
|
-
attr_reader :context
|
71
|
-
|
6
|
+
module JSON
|
7
|
+
module LD
|
72
8
|
##
|
73
|
-
# JSON-LD
|
74
|
-
#
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
control: :checkbox,
|
133
|
-
on: ["--[no-]ordered"],
|
134
|
-
description: "Order object member processing lexographically.") {|arg| arg},
|
135
|
-
RDF::CLI::Option.new(
|
136
|
-
symbol: :processingMode,
|
137
|
-
datatype: %w(json-ld-1.0 json-ld-1.1),
|
138
|
-
control: :radio,
|
139
|
-
on: ["--processingMode MODE", %w(json-ld-1.0 json-ld-1.1)],
|
140
|
-
description: "Set Processing Mode (json-ld-1.0 or json-ld-1.1)"),
|
141
|
-
RDF::CLI::Option.new(
|
142
|
-
symbol: :rdfDirection,
|
143
|
-
datatype: %w(i18n-datatype compound-literal),
|
144
|
-
default: 'null',
|
145
|
-
control: :select,
|
146
|
-
on: ["--rdf-direction DIR", %w(i18n-datatype compound-literal)],
|
147
|
-
description: "How to serialize literal direction (i18n-datatype compound-literal)") {|arg| arg},
|
148
|
-
RDF::CLI::Option.new(
|
149
|
-
symbol: :requireAll,
|
150
|
-
datatype: TrueClass,
|
151
|
-
default: true,
|
152
|
-
control: :checkbox,
|
153
|
-
on: ["--[no-]require-all"],
|
154
|
-
description: "Require all properties to match (true). Default is `true` use --no-require-all to disable.") {|arg| arg},
|
155
|
-
RDF::CLI::Option.new(
|
156
|
-
symbol: :stream,
|
157
|
-
datatype: TrueClass,
|
158
|
-
control: :checkbox,
|
159
|
-
on: ["--[no-]stream"],
|
160
|
-
description: "Do not attempt to optimize graph presentation, suitable for streaming large graphs.") {|arg| arg},
|
161
|
-
RDF::CLI::Option.new(
|
162
|
-
symbol: :useNativeTypes,
|
163
|
-
datatype: TrueClass,
|
164
|
-
control: :checkbox,
|
165
|
-
on: ["--[no-]use-native-types"],
|
166
|
-
description: "Use native JSON values in value objects.") {|arg| arg},
|
167
|
-
RDF::CLI::Option.new(
|
168
|
-
symbol: :useRdfType,
|
169
|
-
datatype: TrueClass,
|
170
|
-
control: :checkbox,
|
171
|
-
on: ["--[no-]use-rdf-type"],
|
172
|
-
description: "Treat `rdf:type` like a normal property instead of using `@type`.") {|arg| arg},
|
173
|
-
]
|
174
|
-
end
|
9
|
+
# A JSON-LD parser in Ruby.
|
10
|
+
#
|
11
|
+
# Note that the natural interface is to write a whole graph at a time.
|
12
|
+
# Writing statements or Triples will create a graph to add them to
|
13
|
+
# and then serialize the graph.
|
14
|
+
#
|
15
|
+
# @example Obtaining a JSON-LD writer class
|
16
|
+
# RDF::Writer.for(:jsonld) #=> JSON::LD::Writer
|
17
|
+
# RDF::Writer.for("etc/test.json")
|
18
|
+
# RDF::Writer.for(:file_name => "etc/test.json")
|
19
|
+
# RDF::Writer.for(file_extension: "json")
|
20
|
+
# RDF::Writer.for(:content_type => "application/turtle")
|
21
|
+
#
|
22
|
+
# @example Serializing RDF graph into an JSON-LD file
|
23
|
+
# JSON::LD::Writer.open("etc/test.json") do |writer|
|
24
|
+
# writer << graph
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# @example Serializing RDF statements into an JSON-LD file
|
28
|
+
# JSON::LD::Writer.open("etc/test.json") do |writer|
|
29
|
+
# graph.each_statement do |statement|
|
30
|
+
# writer << statement
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# @example Serializing RDF statements into an JSON-LD string
|
35
|
+
# JSON::LD::Writer.buffer do |writer|
|
36
|
+
# graph.each_statement do |statement|
|
37
|
+
# writer << statement
|
38
|
+
# end
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# The writer will add prefix definitions, and use them for creating @context definitions, and minting CURIEs
|
42
|
+
#
|
43
|
+
# @example Creating @@context prefix definitions in output
|
44
|
+
# JSON::LD::Writer.buffer(
|
45
|
+
# prefixes: {
|
46
|
+
# nil => "http://example.com/ns#",
|
47
|
+
# foaf: "http://xmlns.com/foaf/0.1/"}
|
48
|
+
# ) do |writer|
|
49
|
+
# graph.each_statement do |statement|
|
50
|
+
# writer << statement
|
51
|
+
# end
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
# Select the :expand option to output JSON-LD in expanded form
|
55
|
+
#
|
56
|
+
# @see https://www.w3.org/TR/json-ld11-api/
|
57
|
+
# @see https://www.w3.org/TR/json-ld11-api/#the-normalization-algorithm
|
58
|
+
# @author [Gregg Kellogg](http://greggkellogg.net/)
|
59
|
+
class Writer < RDF::Writer
|
60
|
+
include StreamingWriter
|
61
|
+
include Utils
|
62
|
+
include RDF::Util::Logger
|
63
|
+
format Format
|
64
|
+
|
65
|
+
# @!attribute [r] graph
|
66
|
+
# @return [RDF::Graph] Graph of statements serialized
|
67
|
+
attr_reader :graph
|
175
68
|
|
176
|
-
|
177
|
-
|
178
|
-
attr_reader :
|
69
|
+
# @!attribute [r] context
|
70
|
+
# @return [Context] context used to load and administer contexts
|
71
|
+
attr_reader :context
|
179
72
|
|
180
73
|
##
|
181
|
-
#
|
182
|
-
#
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
74
|
+
# JSON-LD Writer options
|
75
|
+
# @see https://ruby-rdf.github.io/rdf/RDF/Writer#options-class_method
|
76
|
+
def self.options
|
77
|
+
super + [
|
78
|
+
RDF::CLI::Option.new(
|
79
|
+
symbol: :compactArrays,
|
80
|
+
datatype: TrueClass,
|
81
|
+
default: true,
|
82
|
+
control: :checkbox,
|
83
|
+
on: ["--[no-]compact-arrays"],
|
84
|
+
description: "Replaces arrays with just one element with that element during compaction. Default is `true` use --no-compact-arrays to disable."
|
85
|
+
) { |arg| arg },
|
86
|
+
RDF::CLI::Option.new(
|
87
|
+
symbol: :compactToRelative,
|
88
|
+
datatype: TrueClass,
|
89
|
+
default: true,
|
90
|
+
control: :checkbox,
|
91
|
+
on: ["--[no-]compact-to-relative"],
|
92
|
+
description: "Creates document relative IRIs when compacting, if `true`, otherwise leaves expanded. Default is `true` use --no-compact-to-relative to disable."
|
93
|
+
) { |arg| arg },
|
94
|
+
RDF::CLI::Option.new(
|
95
|
+
symbol: :context,
|
96
|
+
datatype: RDF::URI,
|
97
|
+
control: :url2,
|
98
|
+
on: ["--context CONTEXT"],
|
99
|
+
description: "Context to use when compacting."
|
100
|
+
) { |arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg)) },
|
101
|
+
RDF::CLI::Option.new(
|
102
|
+
symbol: :embed,
|
103
|
+
datatype: %w[@always @once @never],
|
104
|
+
default: '@once',
|
105
|
+
control: :select,
|
106
|
+
on: ["--embed EMBED"],
|
107
|
+
description: "How to embed matched objects (@once)."
|
108
|
+
) { |arg| RDF::URI(arg) },
|
109
|
+
RDF::CLI::Option.new(
|
110
|
+
symbol: :explicit,
|
111
|
+
datatype: TrueClass,
|
112
|
+
control: :checkbox,
|
113
|
+
on: ["--[no-]explicit"],
|
114
|
+
description: "Only include explicitly declared properties in output (false)"
|
115
|
+
) { |arg| arg },
|
116
|
+
RDF::CLI::Option.new(
|
117
|
+
symbol: :frame,
|
118
|
+
datatype: RDF::URI,
|
119
|
+
control: :url2,
|
120
|
+
use: :required,
|
121
|
+
on: ["--frame FRAME"],
|
122
|
+
description: "Frame to use when serializing."
|
123
|
+
) { |arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg)) },
|
124
|
+
RDF::CLI::Option.new(
|
125
|
+
symbol: :lowercaseLanguage,
|
126
|
+
datatype: TrueClass,
|
127
|
+
control: :checkbox,
|
128
|
+
on: ["--[no-]lowercase-language"],
|
129
|
+
description: "By default, language tags are left as is. To normalize to lowercase, set this option to `true`."
|
130
|
+
),
|
131
|
+
RDF::CLI::Option.new(
|
132
|
+
symbol: :omitDefault,
|
133
|
+
datatype: TrueClass,
|
134
|
+
control: :checkbox,
|
135
|
+
on: ["--[no-]omitDefault"],
|
136
|
+
description: "Omit missing properties from output (false)"
|
137
|
+
) { |arg| arg },
|
138
|
+
RDF::CLI::Option.new(
|
139
|
+
symbol: :ordered,
|
140
|
+
datatype: TrueClass,
|
141
|
+
control: :checkbox,
|
142
|
+
on: ["--[no-]ordered"],
|
143
|
+
description: "Order object member processing lexographically."
|
144
|
+
) { |arg| arg },
|
145
|
+
RDF::CLI::Option.new(
|
146
|
+
symbol: :processingMode,
|
147
|
+
datatype: %w[json-ld-1.0 json-ld-1.1],
|
148
|
+
control: :radio,
|
149
|
+
on: ["--processingMode MODE", %w[json-ld-1.0 json-ld-1.1]],
|
150
|
+
description: "Set Processing Mode (json-ld-1.0 or json-ld-1.1)"
|
151
|
+
),
|
152
|
+
RDF::CLI::Option.new(
|
153
|
+
symbol: :rdfDirection,
|
154
|
+
datatype: %w[i18n-datatype compound-literal],
|
155
|
+
default: 'null',
|
156
|
+
control: :select,
|
157
|
+
on: ["--rdf-direction DIR", %w[i18n-datatype compound-literal]],
|
158
|
+
description: "How to serialize literal direction (i18n-datatype compound-literal)"
|
159
|
+
) { |arg| arg },
|
160
|
+
RDF::CLI::Option.new(
|
161
|
+
symbol: :requireAll,
|
162
|
+
datatype: TrueClass,
|
163
|
+
default: true,
|
164
|
+
control: :checkbox,
|
165
|
+
on: ["--[no-]require-all"],
|
166
|
+
description: "Require all properties to match (true). Default is `true` use --no-require-all to disable."
|
167
|
+
) { |arg| arg },
|
168
|
+
RDF::CLI::Option.new(
|
169
|
+
symbol: :stream,
|
170
|
+
datatype: TrueClass,
|
171
|
+
control: :checkbox,
|
172
|
+
on: ["--[no-]stream"],
|
173
|
+
description: "Do not attempt to optimize graph presentation, suitable for streaming large graphs."
|
174
|
+
) { |arg| arg },
|
175
|
+
RDF::CLI::Option.new(
|
176
|
+
symbol: :useNativeTypes,
|
177
|
+
datatype: TrueClass,
|
178
|
+
control: :checkbox,
|
179
|
+
on: ["--[no-]use-native-types"],
|
180
|
+
description: "Use native JSON values in value objects."
|
181
|
+
) { |arg| arg },
|
182
|
+
RDF::CLI::Option.new(
|
183
|
+
symbol: :useRdfType,
|
184
|
+
datatype: TrueClass,
|
185
|
+
control: :checkbox,
|
186
|
+
on: ["--[no-]use-rdf-type"],
|
187
|
+
description: "Treat `rdf:type` like a normal property instead of using `@type`."
|
188
|
+
) { |arg| arg }
|
189
|
+
]
|
194
190
|
end
|
195
191
|
|
196
|
-
|
197
|
-
|
198
|
-
# @return [String]
|
199
|
-
def default_context; @default_context || JSON::LD::DEFAULT_CONTEXT; end
|
192
|
+
class << self
|
193
|
+
attr_reader :white_list, :black_list
|
200
194
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
195
|
+
##
|
196
|
+
# Use parameters from accept-params to determine if the parameters are acceptable to invoke this writer. The `accept_params` will subsequently be provided to the writer instance.
|
197
|
+
#
|
198
|
+
# @param [Hash{Symbol => String}] accept_params
|
199
|
+
# @yield [accept_params] if a block is given, returns the result of evaluating that block
|
200
|
+
# @yieldparam [Hash{Symbol => String}] accept_params
|
201
|
+
# @return [Boolean]
|
202
|
+
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
|
203
|
+
def accept?(accept_params)
|
204
|
+
if block_given?
|
205
|
+
yield(accept_params)
|
206
|
+
else
|
207
|
+
true
|
208
|
+
end
|
209
|
+
end
|
206
210
|
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
# the prefix mappings to use (not supported by all writers)
|
220
|
-
# @option options [Boolean] :standard_prefixes (false)
|
221
|
-
# Add standard prefixes to @prefixes, if necessary.
|
222
|
-
# @option options [IO, Array, Hash, String, Context] :context ({})
|
223
|
-
# context to use when serializing. Constructed context for native serialization.
|
224
|
-
# @option options [IO, Array, Hash, String, Context] :frame ({})
|
225
|
-
# frame to use when serializing.
|
226
|
-
# @option options [Boolean] :unique_bnodes (false)
|
227
|
-
# Use unique bnode identifiers, defaults to using the identifier which the node was originall initialized with (if any).
|
228
|
-
# @option options [Proc] serializer (JSON::LD::API.serializer)
|
229
|
-
# A Serializer method used for generating the JSON serialization of the result.
|
230
|
-
# @option options [Boolean] :stream (false)
|
231
|
-
# Do not attempt to optimize graph presentation, suitable for streaming large graphs.
|
232
|
-
# @yield [writer] `self`
|
233
|
-
# @yieldparam [RDF::Writer] writer
|
234
|
-
# @yieldreturn [void]
|
235
|
-
# @yield [writer]
|
236
|
-
# @yieldparam [RDF::Writer] writer
|
237
|
-
def initialize(output = $stdout, **options, &block)
|
238
|
-
options[:base_uri] ||= options[:base] if options.key?(:base)
|
239
|
-
options[:base] ||= options[:base_uri] if options.key?(:base_uri)
|
240
|
-
@serializer = options.fetch(:serializer, JSON::LD::API.method(:serializer))
|
241
|
-
super do
|
242
|
-
@repo = RDF::Repository.new
|
211
|
+
##
|
212
|
+
# Returns default context used for compacted profile without an explicit context URL
|
213
|
+
# @return [String]
|
214
|
+
def default_context
|
215
|
+
@default_context || JSON::LD::DEFAULT_CONTEXT
|
216
|
+
end
|
217
|
+
|
218
|
+
##
|
219
|
+
# Sets default context used for compacted profile without an explicit context URL
|
220
|
+
# @param [String] url
|
221
|
+
attr_writer :default_context
|
222
|
+
end
|
243
223
|
|
244
|
-
|
245
|
-
|
224
|
+
##
|
225
|
+
# Initializes the JSON-LD writer instance.
|
226
|
+
#
|
227
|
+
# @param [IO, File] output
|
228
|
+
# the output stream
|
229
|
+
# @param [Hash{Symbol => Object}] options
|
230
|
+
# any additional options
|
231
|
+
# @option options [Encoding] :encoding (Encoding::UTF_8)
|
232
|
+
# the encoding to use on the output stream (Ruby 1.9+)
|
233
|
+
# @option options [Boolean] :canonicalize (false)
|
234
|
+
# whether to canonicalize literals when serializing
|
235
|
+
# @option options [Hash] :prefixes ({})
|
236
|
+
# the prefix mappings to use (not supported by all writers)
|
237
|
+
# @option options [Boolean] :standard_prefixes (false)
|
238
|
+
# Add standard prefixes to @prefixes, if necessary.
|
239
|
+
# @option options [IO, Array, Hash, String, Context] :context ({})
|
240
|
+
# context to use when serializing. Constructed context for native serialization.
|
241
|
+
# @option options [IO, Array, Hash, String, Context] :frame ({})
|
242
|
+
# frame to use when serializing.
|
243
|
+
# @option options [Boolean] :unique_bnodes (false)
|
244
|
+
# Use unique bnode identifiers, defaults to using the identifier which the node was originall initialized with (if any).
|
245
|
+
# @option options [Proc] serializer (JSON::LD::API.serializer)
|
246
|
+
# A Serializer method used for generating the JSON serialization of the result.
|
247
|
+
# @option options [Boolean] :stream (false)
|
248
|
+
# Do not attempt to optimize graph presentation, suitable for streaming large graphs.
|
249
|
+
# @yield [writer] `self`
|
250
|
+
# @yieldparam [RDF::Writer] writer
|
251
|
+
# @yieldreturn [void]
|
252
|
+
# @yield [writer]
|
253
|
+
# @yieldparam [RDF::Writer] writer
|
254
|
+
def initialize(output = $stdout, **options, &block)
|
255
|
+
options[:base_uri] ||= options[:base] if options.key?(:base)
|
256
|
+
options[:base] ||= options[:base_uri] if options.key?(:base_uri)
|
257
|
+
@serializer = options.fetch(:serializer, JSON::LD::API.method(:serializer))
|
258
|
+
super do
|
259
|
+
@repo = RDF::Repository.new
|
260
|
+
|
261
|
+
if block
|
262
|
+
case block.arity
|
246
263
|
when 0 then instance_eval(&block)
|
247
|
-
else
|
264
|
+
else yield(self)
|
265
|
+
end
|
248
266
|
end
|
249
267
|
end
|
250
268
|
end
|
251
|
-
end
|
252
269
|
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
270
|
+
##
|
271
|
+
# Addes a triple to be serialized
|
272
|
+
# @param [RDF::Resource] subject
|
273
|
+
# @param [RDF::URI] predicate
|
274
|
+
# @param [RDF::Value] object
|
275
|
+
# @return [void]
|
276
|
+
# @abstract
|
277
|
+
def write_triple(subject, predicate, object)
|
278
|
+
write_quad(subject, predicate, object, nil)
|
279
|
+
end
|
263
280
|
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
281
|
+
##
|
282
|
+
# Outputs the N-Quads representation of a statement.
|
283
|
+
#
|
284
|
+
# @param [RDF::Resource] subject
|
285
|
+
# @param [RDF::URI] predicate
|
286
|
+
# @param [RDF::Term] object
|
287
|
+
# @return [void]
|
288
|
+
def write_quad(subject, predicate, object, graph_name)
|
289
|
+
statement = RDF::Statement.new(subject, predicate, object, graph_name: graph_name)
|
290
|
+
if @options[:stream]
|
291
|
+
stream_statement(statement)
|
292
|
+
else
|
293
|
+
@repo.insert(statement)
|
294
|
+
end
|
277
295
|
end
|
278
|
-
end
|
279
296
|
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
297
|
+
##
|
298
|
+
# Necessary for streaming
|
299
|
+
# @return [void] `self`
|
300
|
+
def write_prologue
|
301
|
+
stream_prologue if @options[:stream]
|
302
|
+
super
|
303
|
+
end
|
287
304
|
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
305
|
+
##
|
306
|
+
# Outputs the Serialized JSON-LD representation of all stored statements.
|
307
|
+
#
|
308
|
+
# If provided a context or prefixes, we'll create a context
|
309
|
+
# and use it to compact the output. Otherwise, we return un-compacted JSON-LD
|
310
|
+
#
|
311
|
+
# @return [void]
|
312
|
+
# @see #write_triple
|
313
|
+
def write_epilogue
|
314
|
+
if @options[:stream]
|
315
|
+
stream_epilogue
|
316
|
+
else
|
300
317
|
|
301
|
-
|
302
|
-
|
318
|
+
# log_debug("writer") { "serialize #{@repo.count} statements, #{@options.inspect}"}
|
319
|
+
result = API.fromRdf(@repo, **@options.merge(serializer: nil))
|
303
320
|
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
321
|
+
# Some options may be indicated from accept parameters
|
322
|
+
profile = @options.fetch(:accept_params, {}).fetch(:profile, "").split
|
323
|
+
links = LinkHeader.parse(@options[:link])
|
324
|
+
@options[:context] ||= begin
|
325
|
+
links.find_link(['rel', JSON_LD_NS + "context"]).href
|
326
|
+
rescue StandardError
|
327
|
+
nil
|
328
|
+
end
|
329
|
+
@options[:context] ||= Writer.default_context if profile.include?(JSON_LD_NS + "compacted")
|
330
|
+
@options[:frame] ||= begin
|
331
|
+
links.find_link(['rel', JSON_LD_NS + "frame"]).href
|
332
|
+
rescue StandardError
|
333
|
+
nil
|
334
|
+
end
|
310
335
|
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
336
|
+
# If we were provided a context, or prefixes, use them to compact the output
|
337
|
+
context = @options[:context]
|
338
|
+
context ||= if @options[:prefixes] || @options[:language] || @options[:standard_prefixes]
|
339
|
+
ctx = Context.new(**@options)
|
340
|
+
ctx.language = @options[:language] if @options[:language]
|
341
|
+
@options[:prefixes]&.each do |prefix, iri|
|
342
|
+
ctx.set_mapping(prefix, iri) if prefix && iri
|
343
|
+
end
|
344
|
+
ctx
|
345
|
+
end
|
321
346
|
|
322
|
-
|
323
|
-
|
324
|
-
result = API.flatten(result, context, **@options.merge(serializer: nil))
|
325
|
-
end
|
347
|
+
# Rename BNodes to uniquify them, if necessary
|
348
|
+
result = API.flatten(result, context, **@options.merge(serializer: nil)) if options[:unique_bnodes]
|
326
349
|
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
350
|
+
if (frame = @options[:frame])
|
351
|
+
# Perform framing, if given a frame
|
352
|
+
# log_debug("writer") { "frame result"}
|
353
|
+
result = API.frame(result, frame, **@options.merge(serializer: nil))
|
354
|
+
elsif context
|
355
|
+
# Perform compaction, if we have a context
|
356
|
+
# log_debug("writer") { "compact result"}
|
357
|
+
result = API.compact(result, context, **@options.merge(serializer: nil))
|
358
|
+
end
|
359
|
+
|
360
|
+
@output.write(@serializer.call(result, **@options))
|
335
361
|
end
|
336
362
|
|
337
|
-
|
363
|
+
super
|
338
364
|
end
|
339
|
-
|
340
|
-
super
|
341
365
|
end
|
342
366
|
end
|
343
367
|
end
|
344
|
-
|