Class: JSON::LD::Writer
- Inherits:
-
RDF::Writer
- Object
- RDF::Writer
- JSON::LD::Writer
- Includes:
- Utils
- 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
-
.to_sym ⇒ Object
Override normal symbol generation.
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_graph(graph) ⇒ void
Write whole graph.
-
#write_statement(statement) ⇒ void
Adds a statement to be serialized.
-
#write_triple(subject, predicate, object) ⇒ void
abstract
Addes a triple to be serialized.
Methods included from Utils
#as_resource, #blank_node?, #index?, #list?, #node?, #node_reference?, #value?
Constructor Details
#initialize(output = $stdout, options = {}) {|writer| ... } ⇒ Writer
Initializes the RDF-LD writer instance.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/json/ld/writer.rb', line 95 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)
63 64 65 |
# File 'lib/json/ld/writer.rb', line 63 def context @context end |
#graph ⇒ RDF::Graph (readonly)
59 60 61 |
# File 'lib/json/ld/writer.rb', line 59 def graph @graph end |
Class Method Details
.to_sym ⇒ Object
Override normal symbol generation
67 68 69 |
# File 'lib/json/ld/writer.rb', line 67 def self.to_sym :jsonld 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
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 |
# File 'lib/json/ld/writer.rb', line 148 def write_epilogue @debug = [:debug] debug("writer") { "serialize #{@repo.count} statements, #{@options.inspect}"} result = API.fromRdf(@repo, ) # If we were provided a context, or prefixes, use them to compact the output context = RDF::Util::File.open_file([:context]) if [:context].is_a?(String) context ||= [:context] context ||= if [:prefixes] || [:language] || [:standard_prefixes] ctx = Context.new() ctx.language = [:language] if [:language] [:prefixes].each do |prefix, iri| ctx.set_mapping(prefix, iri) if prefix && iri end if [:prefixes] ctx end # Rename BNodes to uniquify them, if necessary if [:unique_bnodes] result = API.flatten(result, context, ) end # Perform compaction, if we have a context if context debug("writer") { "compact result"} result = API.compact(result, context, ) end @output.write(result.to_json(JSON_STATE)) end |
#write_graph(graph) ⇒ void
This method returns an undefined value.
Write whole graph
115 116 117 118 |
# File 'lib/json/ld/writer.rb', line 115 def write_graph(graph) debug {"Add graph #{graph.inspect}"} @repo = graph end |
#write_statement(statement) ⇒ void
This method returns an undefined value.
Adds a statement to be serialized
124 125 126 |
# File 'lib/json/ld/writer.rb', line 124 def write_statement(statement) @repo.insert(statement) end |
#write_triple(subject, predicate, object) ⇒ void
This method returns an undefined value.
Addes a triple to be serialized
136 137 138 |
# File 'lib/json/ld/writer.rb', line 136 def write_triple(subject, predicate, object) @repo.insert(Statement.new(subject, predicate, object)) end |