lib/json/ld/writer.rb in json-ld-3.2.1 vs lib/json/ld/writer.rb in json-ld-3.2.2

- old
+ new

@@ -184,14 +184,10 @@ # @yield [accept_params] if a block is given, returns the result of evaluating that block # @yieldparam [Hash{Symbol => String}] accept_params # @return [Boolean] # @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1 def accept?(accept_params) - # Profiles that aren't specific IANA relations represent the URL - # of a context or frame that may be subject to black- or white-listing - profile = accept_params[:profile].to_s.split(/\s+/) - if block_given? yield(accept_params) else true end @@ -227,20 +223,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 [Proc] serializer (JSON::LD::API.serializer) + # A Serializer method used for generating the JSON serialization of the result. # @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.key?(:base) options[:base] ||= options[:base_uri] if options.key?(:base_uri) + @serializer = options.fetch(:serializer, JSON::LD::API.method(:serializer)) super do @repo = RDF::Repository.new if block_given? case block.arity @@ -298,11 +297,11 @@ if @options[:stream] stream_epilogue else log_debug("writer") { "serialize #{@repo.count} statements, #{@options.inspect}"} - result = API.fromRdf(@repo, **@options) + result = API.fromRdf(@repo, **@options.merge(serializer: nil)) # Some options may be indicated from accept parameters profile = @options.fetch(:accept_params, {}).fetch(:profile, "").split(' ') links = LinkHeader.parse(@options[:link]) @options[:context] ||= links.find_link(['rel', JSON_LD_NS+"context"]).href rescue nil @@ -320,23 +319,23 @@ ctx end # Rename BNodes to uniquify them, if necessary if options[:unique_bnodes] - result = API.flatten(result, context, **@options) + result = API.flatten(result, context, **@options.merge(serializer: nil)) end if frame = @options[:frame] # Perform framing, if given a frame log_debug("writer") { "frame result"} - result = API.frame(result, frame, **@options) + result = API.frame(result, frame, **@options.merge(serializer: nil)) elsif context # Perform compaction, if we have a context log_debug("writer") { "compact result"} - result = API.compact(result, context, **@options) + result = API.compact(result, context, **@options.merge(serializer: nil)) end - @output.write(result.to_json(JSON_STATE)) + @output.write(@serializer.call(result, **@options)) end super end end