Class: JSON::LD::Writer
- Inherits:
-
RDF::Writer
- Object
- RDF::Writer
- JSON::LD::Writer
- Includes:
- StreamingWriter, Utils, RDF::Util::Logger
- Defined in:
- vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/writer.rb
Overview
A JSON-LD parser in Ruby.
Note that the natural interface is to write a whole graph at a time. Writing statements or Triples will create a graph to add them to and then serialize the graph.
The writer will add prefix definitions, and use them for creating @context definitions, and minting CURIEs
Select the :expand option to output JSON-LD in expanded form
Direct Known Subclasses
Constant Summary
Constants included from RDF::Util::Logger
Class Attribute Summary collapse
-
.black_list ⇒ Object
readonly
Returns the value of attribute black_list.
-
.default_context ⇒ String
Returns default context used for compacted profile without an explicit context URL.
-
.white_list ⇒ Object
readonly
Returns the value of attribute white_list.
Instance Attribute Summary collapse
-
#context ⇒ Context
readonly
Context used to load and administer contexts.
-
#graph ⇒ RDF::Graph
readonly
Graph of statements serialized.
Attributes inherited from RDF::Writer
Class Method Summary collapse
-
.accept?(accept_params) {|accept_params| ... } ⇒ Boolean
Use parameters from accept-params to determine if the parameters are acceptable to invoke this writer.
-
.options ⇒ Object
JSON-LD Writer options.
Instance Method Summary collapse
-
#initialize(output = $stdout, **options) {|writer| ... } ⇒ Writer
constructor
Initializes the JSON-LD writer instance.
-
#write_epilogue
Outputs the Serialized JSON-LD representation of all stored statements.
-
#write_prologue
Necessary for streaming.
-
#write_quad(subject, predicate, object, graph_name)
Outputs the N-Quads representation of a statement.
-
#write_triple(subject, predicate, object)
abstract
Addes a triple to be serialized.
Methods included from RDF::Util::Logger
#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger
Methods included from Utils
#add_value, #as_array, #as_resource, #blank_node?, #compare_values, #graph?, #has_value?, #index?, #list?, #node?, #node_or_ref?, #node_reference?, #property?, #simple_graph?, #value?
Methods included from StreamingWriter
#stream_epilogue, #stream_prologue, #stream_statement
Methods inherited from RDF::Writer
#base_uri, buffer, #canonicalize?, dump, each, #encoding, #escaped, #flush, for, format, #format_list, #format_literal, #format_node, #format_quotedTriple, #format_term, #format_tripleTerm, #format_uri, #node_id, open, #prefix, #prefixes, #prefixes=, #puts, #quoted, to_sym, #to_sym, #uri_for, #validate?, #write_comment, #write_statement, #write_triples
Methods included from RDF::Util::Aliasing::LateBound
Methods included from RDF::Writable
#<<, #insert, #insert_graph, #insert_reader, #insert_statement, #insert_statements, #writable?
Methods included from RDF::Util::Coercions
Constructor Details
#initialize(output = $stdout, **options) {|writer| ... } ⇒ Writer
Initializes the JSON-LD writer instance.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/writer.rb', line 254 def initialize(output = $stdout, **, &block) [:base_uri] ||= [:base] if .key?(:base) [:base] ||= [:base_uri] if .key?(:base_uri) @serializer = .fetch(:serializer, JSON::LD::API.method(:serializer)) super do @repo = RDF::Repository.new if block case block.arity when 0 then instance_eval(&block) else yield(self) end end end end |
Class Attribute Details
.black_list ⇒ Object (readonly)
Returns the value of attribute black_list.
193 194 195 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/writer.rb', line 193 def black_list @black_list end |
.default_context ⇒ String
Returns default context used for compacted profile without an explicit context URL
214 215 216 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/writer.rb', line 214 def default_context @default_context || JSON::LD::DEFAULT_CONTEXT end |
.white_list ⇒ Object (readonly)
Returns the value of attribute white_list.
193 194 195 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/writer.rb', line 193 def white_list @white_list end |
Instance Attribute Details
#context ⇒ Context (readonly)
Returns context used to load and administer contexts.
71 72 73 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/writer.rb', line 71 def context @context end |
#graph ⇒ RDF::Graph (readonly)
Returns Graph of statements serialized.
67 68 69 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/writer.rb', line 67 def graph @graph end |
Class Method Details
.accept?(accept_params) {|accept_params| ... } ⇒ Boolean
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.
203 204 205 206 207 208 209 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/writer.rb', line 203 def accept?(accept_params) if block_given? yield(accept_params) else true end end |
.options ⇒ Object
JSON-LD Writer options
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/writer.rb', line 76 def self. super + [ RDF::CLI::Option.new( symbol: :compactArrays, datatype: TrueClass, default: true, control: :checkbox, on: ["--[no-]compact-arrays"], description: "Replaces arrays with just one element with that element during compaction. Default is `true` use --no-compact-arrays to disable." ) { |arg| arg }, RDF::CLI::Option.new( symbol: :compactToRelative, datatype: TrueClass, default: true, control: :checkbox, on: ["--[no-]compact-to-relative"], description: "Creates document relative IRIs when compacting, if `true`, otherwise leaves expanded. Default is `true` use --no-compact-to-relative to disable." ) { |arg| arg }, RDF::CLI::Option.new( symbol: :context, datatype: RDF::URI, control: :url2, on: ["--context CONTEXT"], description: "Context to use when compacting." ) { |arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg)) }, RDF::CLI::Option.new( symbol: :embed, datatype: %w[@always @once @never], default: '@once', control: :select, on: ["--embed EMBED"], description: "How to embed matched objects (@once)." ) { |arg| RDF::URI(arg) }, RDF::CLI::Option.new( symbol: :explicit, datatype: TrueClass, control: :checkbox, on: ["--[no-]explicit"], description: "Only include explicitly declared properties in output (false)" ) { |arg| arg }, RDF::CLI::Option.new( symbol: :frame, datatype: RDF::URI, control: :url2, use: :required, on: ["--frame FRAME"], description: "Frame to use when serializing." ) { |arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg)) }, RDF::CLI::Option.new( symbol: :lowercaseLanguage, datatype: TrueClass, control: :checkbox, on: ["--[no-]lowercase-language"], description: "By default, language tags are left as is. To normalize to lowercase, set this option to `true`." ), RDF::CLI::Option.new( symbol: :omitDefault, datatype: TrueClass, control: :checkbox, on: ["--[no-]omitDefault"], description: "Omit missing properties from output (false)" ) { |arg| arg }, RDF::CLI::Option.new( symbol: :ordered, datatype: TrueClass, control: :checkbox, on: ["--[no-]ordered"], description: "Order object member processing lexographically." ) { |arg| arg }, RDF::CLI::Option.new( symbol: :processingMode, datatype: %w[json-ld-1.0 json-ld-1.1], control: :radio, on: ["--processingMode MODE", %w[json-ld-1.0 json-ld-1.1]], description: "Set Processing Mode (json-ld-1.0 or json-ld-1.1)" ), RDF::CLI::Option.new( symbol: :rdfDirection, datatype: %w[i18n-datatype compound-literal], default: 'null', control: :select, on: ["--rdf-direction DIR", %w[i18n-datatype compound-literal]], description: "How to serialize literal direction (i18n-datatype compound-literal)" ) { |arg| arg }, RDF::CLI::Option.new( symbol: :requireAll, datatype: TrueClass, default: true, control: :checkbox, on: ["--[no-]require-all"], description: "Require all properties to match (true). Default is `true` use --no-require-all to disable." ) { |arg| arg }, RDF::CLI::Option.new( symbol: :stream, datatype: TrueClass, control: :checkbox, on: ["--[no-]stream"], description: "Do not attempt to optimize graph presentation, suitable for streaming large graphs." ) { |arg| arg }, RDF::CLI::Option.new( symbol: :useNativeTypes, datatype: TrueClass, control: :checkbox, on: ["--[no-]use-native-types"], description: "Use native JSON values in value objects." ) { |arg| arg }, RDF::CLI::Option.new( symbol: :useRdfType, datatype: TrueClass, control: :checkbox, on: ["--[no-]use-rdf-type"], description: "Treat `rdf:type` like a normal property instead of using `@type`." ) { |arg| arg } ] end |
Instance Method Details
#write_epilogue
This method returns an undefined value.
Outputs the Serialized JSON-LD representation of all stored statements.
If provided a context or prefixes, we'll create a context and use it to compact the output. Otherwise, we return un-compacted JSON-LD
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/writer.rb', line 313 def write_epilogue if @options[:stream] stream_epilogue else # log_debug("writer") { "serialize #{@repo.count} statements, #{@options.inspect}"} result = API.fromRdf(@repo, **@options.merge(serializer: nil)) # Some options may be indicated from accept parameters profile = @options.fetch(:accept_params, {}).fetch(:profile, "").split links = LinkHeader.parse(@options[:link]) @options[:context] ||= begin links.find_link(['rel', JSON_LD_NS + "context"]).href rescue StandardError nil end @options[:context] ||= Writer.default_context if profile.include?(JSON_LD_NS + "compacted") @options[:frame] ||= begin links.find_link(['rel', JSON_LD_NS + "frame"]).href rescue StandardError nil end # If we were provided a context, or prefixes, use them to compact the output context = @options[:context] context ||= if @options[:prefixes] || @options[:language] || @options[:standard_prefixes] ctx = Context.new(**@options) ctx.language = @options[:language] if @options[:language] @options[:prefixes]&.each do |prefix, iri| ctx.set_mapping(prefix, iri) if prefix && iri end ctx end # Rename BNodes to uniquify them, if necessary result = API.flatten(result, context, **@options.merge(serializer: nil)) if [:unique_bnodes] if (frame = @options[:frame]) # Perform framing, if given a frame # log_debug("writer") { "frame result"} result = API.frame(result, frame, **@options.merge(serializer: nil)) elsif context # Perform compaction, if we have a context # log_debug("writer") { "compact result"} result = API.compact(result, context, **@options.merge(serializer: nil)) end @output.write(@serializer.call(result, **@options)) end super end |
#write_prologue
This method returns an undefined value.
Necessary for streaming
300 301 302 303 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/writer.rb', line 300 def write_prologue stream_prologue if @options[:stream] super end |
#write_quad(subject, predicate, object, graph_name)
This method returns an undefined value.
Outputs the N-Quads representation of a statement.
288 289 290 291 292 293 294 295 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/writer.rb', line 288 def write_quad(subject, predicate, object, graph_name) statement = RDF::Statement.new(subject, predicate, object, graph_name: graph_name) if @options[:stream] stream_statement(statement) else @repo.insert(statement) end end |
#write_triple(subject, predicate, object)
This method returns an undefined value.
Addes a triple to be serialized
277 278 279 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/writer.rb', line 277 def write_triple(subject, predicate, object) write_quad(subject, predicate, object, nil) end |