Class: JSON::LD::Writer
- Inherits:
-
RDF::Writer
- Object
- RDF::Writer
- JSON::LD::Writer
- Includes:
- StreamingWriter, Utils, RDF::Util::Logger
- Defined in:
- 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
Instance Attribute Summary collapse
-
#context ⇒ Context
readonly
Context used to load and administer contexts.
-
#graph ⇒ RDF::Graph
readonly
Graph of statements serialized.
Class Method Summary collapse
-
.options ⇒ Object
JSON-LD Writer options.
Instance Method Summary collapse
-
#initialize(output = $stdout, **options) {|writer| ... } ⇒ Writer
constructor
Initializes the RDF-LD writer instance.
-
#write_epilogue ⇒ void
Outputs the Serialized JSON-LD representation of all stored statements.
-
#write_prologue ⇒ void
Necessary for streaming.
-
#write_quad(subject, predicate, object, graph_name) ⇒ void
Outputs the N-Quads representation of a statement.
-
#write_triple(subject, predicate, object) ⇒ void
abstract
Addes a triple to be serialized.
Methods included from Utils
#add_value, #as_array, #as_resource, #blank_node?, #compare_values, #graph?, #has_property, #has_value, #index?, #list?, #node?, #node_or_ref?, #node_reference?, #simple_graph?, #value?
Methods included from StreamingWriter
#stream_epilogue, #stream_prologue, #stream_statement
Constructor Details
#initialize(output = $stdout, **options) {|writer| ... } ⇒ Writer
Initializes the RDF-LD writer instance.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/json/ld/writer.rb', line 176 def initialize(output = $stdout, **, &block) [:base_uri] ||= [:base] if .has_key?(:base) [:base] ||= [:base_uri] if .has_key?(:base_uri) super do @repo = RDF::Repository.new if block_given? case block.arity when 0 then instance_eval(&block) else block.call(self) end end end end |
Instance Attribute Details
#context ⇒ Context (readonly)
Returns context used to load and administer contexts.
68 69 70 |
# File 'lib/json/ld/writer.rb', line 68 def context @context end |
#graph ⇒ RDF::Graph (readonly)
Returns Graph of statements serialized.
64 65 66 |
# File 'lib/json/ld/writer.rb', line 64 def graph @graph end |
Class Method Details
.options ⇒ Object
JSON-LD Writer options
73 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/json/ld/writer.rb', line 73 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)}, RDF::CLI::Option.new( symbol: :embed, datatype: %w(@always @last @never), default: '@last', control: :select, on: ["--embed EMBED"], description: "How to embed matched objects (@last).") {|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: :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: :requireAll, datatype: TrueClass, default: true, control: :checkbox, on: ["--[no-]requireAll"], description: "Require all properties to match (true). Default is `true` use --no-compact-to-relative 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: :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 ⇒ void
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
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/json/ld/writer.rb', line 234 def write_epilogue if @options[:stream] stream_epilogue else log_debug("writer") { "serialize #{@repo.count} statements, #{@options.inspect}"} result = API.fromRdf(@repo, @options) # If we were provided a context, or prefixes, use them to compact the output context = RDF::Util::File.open_file(@options[:context]) if @options[:context].is_a?(String) 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 if @options[:prefixes] ctx end # Rename BNodes to uniquify them, if necessary if [:unique_bnodes] result = API.flatten(result, context, @options) end frame = RDF::Util::File.open_file(@options[:frame]) if @options[:frame].is_a?(String) if frame ||= @options[:frame] # Perform framing, if given a frame log_debug("writer") { "frame result"} result = API.frame(result, frame, @options) elsif context # Perform compaction, if we have a context log_debug("writer") { "compact result"} result = API.compact(result, context, @options) end @output.write(result.to_json(JSON_STATE)) end super end |
#write_prologue ⇒ void
This method returns an undefined value.
Necessary for streaming
221 222 223 224 |
# File 'lib/json/ld/writer.rb', line 221 def write_prologue stream_prologue if @options[:stream] super end |
#write_quad(subject, predicate, object, graph_name) ⇒ void
This method returns an undefined value.
Outputs the N-Quads representation of a statement.
209 210 211 212 213 214 215 216 |
# File 'lib/json/ld/writer.rb', line 209 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) ⇒ void
This method returns an undefined value.
Addes a triple to be serialized
198 199 200 |
# File 'lib/json/ld/writer.rb', line 198 def write_triple(subject, predicate, object) write_quad(subject, predicate, object, nil) end |