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
Complete open statements.
-
#stream_prologue
Write out array start, and note not to prepend node-separating ‘,’.
-
#stream_statement(statement)
Write a statement, creating a current node definition, if necessary.
Instance Method Details
#stream_epilogue
This method returns an undefined value.
Complete open statements
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/json/ld/streaming_writer.rb', line 78 def stream_epilogue # log_debug("epilogue") {"state: #{@state.inspect}"} end_graph if context @output.puts "\n]}" else @output.puts "\n]" end self end |
#stream_prologue
This method returns an undefined value.
Write out array start, and note not to prepend node-separating ‘,’
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/json/ld/streaming_writer.rb', line 14 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.parse(@options[:context]) end # log_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)
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.
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 70 71 72 73 |
# File 'lib/json/ld/streaming_writer.rb', line 41 def stream_statement(statement) # log_debug("ss") {"state: #{@state.inspect}, stmt: #{statement}"} if @current_graph != statement.graph_name end_graph start_graph(statement.graph_name) 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 } elsif statement.object.datatype == RDF_JSON { "@value" => MultiJson.load(statement.object.to_s), "@type" => "@json" } else lit = { "@value" => statement.object.to_s } lit["@type"] = statement.object.datatype.to_s if statement.object.datatype? lit["@language"] = statement.object.language.to_s if statement.object.language? lit end end self end |