Module: JSON::LD::StreamingWriter
- Included in:
- Writer
- Defined in:
- lib/json/ld/streaming_writer.rb
Overview
Streaming writer interface.
Writes an array of statements serialized in expanded JSON-LD. No provision for turning rdf:first/rest into @list encodings.
Instance Method Summary collapse
-
#stream_epilogue ⇒ void
Complete open statements.
-
#stream_prologue ⇒ void
Write out array start, and note not to prepend node-separating ‘,’.
-
#stream_statement(statement) ⇒ void
Write a statement, creating a current node definition, if necessary.
Instance Method Details
#stream_epilogue ⇒ void
This method returns an undefined value.
Complete open statements
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/json/ld/streaming_writer.rb', line 74 def stream_epilogue debug("epilogue") {"state: #{@state.inspect}"} end_graph if context @output.puts "\n]}" else @output.puts "\n]" end self end |
#stream_prologue ⇒ void
This method returns an undefined value.
Write out array start, and note not to prepend node-separating ‘,’
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/json/ld/streaming_writer.rb', line 11 def stream_prologue # If we were provided a context, or prefixes, use them to compact the output @context = case @options[:context] when nil then nil when Context then @options[:context] else Context.new.parse(@options[:context]) end debug("prologue") {"context: #{context.inspect}"} if context @output.puts %({"@context": #{context.serialize['@context'].to_json}, "@graph": [) else @output.puts "[" end self end |
#stream_statement(statement) ⇒ void
This method returns an undefined value.
Write a statement, creating a current node definition, if necessary.
Once a new/first statement is seen, terminate the current node definition and compact if provided a context.
Also expects all statements in the same context to be contained in a block including all subjects in a block (except for list elements)
Note that if list elements are not received in order using the same subject and property, this may cause a bad serialization.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/json/ld/streaming_writer.rb', line 39 def stream_statement(statement) debug("ss") {"state: #{@state.inspect}, stmt: #{statement}"} if @current_graph != statement.context end_graph start_graph(statement.context) end # If we're writing a list @current_node_def ||= {'@id' => statement.subject.to_s} if statement.subject.to_s != @current_node_def['@id'] end_node @current_node_def = {'@id' => statement.subject.to_s} end if statement.predicate == RDF.type (@current_node_def['@type'] ||= []) << statement.object.to_s else pd = (@current_node_def[statement.predicate.to_s] ||= []) pd << if statement.object.resource? {'@id' => statement.object.to_s} else lit = {"@value" => statement.object.to_s} lit["@type"] = statement.object.datatype.to_s if statement.object.has_datatype? lit["@language"] = statement.object.language.to_s if statement.object.has_language? lit end end self end |