lib/json/ld/writer.rb in json-ld-1.1.7 vs lib/json/ld/writer.rb in json-ld-1.1.8
- old
+ new
@@ -1,5 +1,6 @@
+require 'json/ld/streaming_writer'
module JSON::LD
##
# A JSON-LD parser in Ruby.
#
# Note that the natural interface is to write a whole graph at a time.
@@ -8,11 +9,11 @@
#
# @example Obtaining a JSON-LD writer class
# RDF::Writer.for(:jsonld) #=> JSON::LD::Writer
# RDF::Writer.for("etc/test.json")
# RDF::Writer.for(:file_name => "etc/test.json")
- # RDF::Writer.for(:file_extension => "json")
+ # RDF::Writer.for(file_extension: "json")
# RDF::Writer.for(:content_type => "application/turtle")
#
# @example Serializing RDF graph into an JSON-LD file
# JSON::LD::Writer.open("etc/test.json") do |writer|
# writer << graph
@@ -34,13 +35,13 @@
#
# The writer will add prefix definitions, and use them for creating @context definitions, and minting CURIEs
#
# @example Creating @@context prefix definitions in output
# JSON::LD::Writer.buffer(
- # :prefixes => {
+ # prefixes: {
# nil => "http://example.com/ns#",
- # :foaf => "http://xmlns.com/foaf/0.1/"}
+ # foaf: "http://xmlns.com/foaf/0.1/"}
# ) do |writer|
# graph.each_statement do |statement|
# writer << statement
# end
# end
@@ -49,10 +50,11 @@
#
# @see http://json-ld.org/spec/ED/20110507/
# @see http://json-ld.org/spec/ED/20110507/#the-normalization-algorithm
# @author [Gregg Kellogg](http://greggkellogg.net/)
class Writer < RDF::Writer
+ include StreamingWriter
include Utils
format Format
# @!attribute [r] graph
# @return [RDF::Graph] Graph of statements serialized
@@ -87,20 +89,23 @@
# context to use when serializing. Constructed context for native serialization.
# @option options [IO, Array, Hash, String, Context] :frame ({})
# frame to use when serializing.
# @option options [Boolean] :unique_bnodes (false)
# Use unique bnode identifiers, defaults to using the identifier which the node was originall initialized with (if any).
+ # @option options [Boolean] :stream (false)
+ # Do not attempt to optimize graph presentation, suitable for streaming large graphs.
# @yield [writer] `self`
# @yieldparam [RDF::Writer] writer
# @yieldreturn [void]
# @yield [writer]
# @yieldparam [RDF::Writer] writer
def initialize(output = $stdout, options = {}, &block)
options[:base_uri] ||= options[:base] if options.has_key?(:base)
options[:base] ||= options[:base_uri] if options.has_key?(:base_uri)
super do
@repo = RDF::Repository.new
+ @debug = @options[:debug]
if block_given?
case block.arity
when 0 then instance_eval(&block)
else block.call(self)
@@ -122,11 +127,17 @@
##
# Adds a statement to be serialized
# @param [RDF::Statement] statement
# @return [void]
def write_statement(statement)
- @repo.insert(statement)
+ case
+ when @options[:stream]
+ stream_statement(statement)
+ else
+ # Add to repo and output in epilogue
+ @repo.insert(statement)
+ end
end
##
# Addes a triple to be serialized
# @param [RDF::Resource] subject
@@ -134,22 +145,34 @@
# @param [RDF::Value] object
# @return [void]
# @raise [NotImplementedError] unless implemented in subclass
# @abstract
def write_triple(subject, predicate, object)
- @repo.insert(Statement.new(subject, predicate, object))
+ write_statement(Statement.new(subject, predicate, object))
end
##
+ # Necessary for streaming
+ # @return [void] `self`
+ def write_prologue
+ case
+ when @options[:stream]
+ stream_prologue
+ else
+ super
+ end
+ end
+
+ ##
# 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
#
# @return [void]
# @see #write_triple
def write_epilogue
- @debug = @options[:debug]
+ return stream_epilogue if @options[:stream]
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