Class: RDF::JSON::Writer

Inherits:
Writer show all
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-json-7d579de01769/lib/rdf/json/writer.rb

Overview

RDF/JSON serializer.

Examples:

Loading RDF/JSON serialization support

require 'rdf/json'

Obtaining an RDF/JSON writer class

RDF::Writer.for(:json)         #=> RDF::JSON::Writer
RDF::Writer.for("etc/test.rj")
RDF::Writer.for(:file_name      => "etc/test.rj")
RDF::Writer.for(:file_extension => "rj")
RDF::Writer.for(:content_type   => "application/rdf+json")

Serializing RDF statements into an RDF/JSON file

RDF::JSON::Writer.open("etc/test.rj") do |writer|
  graph.each_statement do |statement|
    writer << statement
  end
end

Serializing RDF statements into an RDF/JSON string

RDF::JSON::Writer.buffer do |writer|
  graph.each_statement do |statement|
    writer << statement
  end
end

See Also:

Constant Summary

Constants included from Util::Logger

Util::Logger::IOWrapper

Instance Attribute Summary

Attributes inherited from Writer

#options

Instance Method Summary collapse

Methods inherited from Writer

accept?, #base_uri, buffer, #canonicalize?, dump, each, #encoding, #escaped, #flush, for, format, #format_list, #format_quotedTriple, #format_term, #format_tripleTerm, #initialize, #node_id, open, options, #prefix, #prefixes, #prefixes=, #puts, #quoted, #to_sym, to_sym, #uri_for, #validate?, #write_comment, #write_prologue, #write_statement, #write_triples

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Writable

#<<, #insert, #insert_graph, #insert_reader, #insert_statement, #insert_statements, #writable?

Methods included from Util::Coercions

#coerce_statements

Methods included from Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Constructor Details

This class inherits a constructor from RDF::Writer

Instance Method Details

#format_literal(value, **options) ⇒ String

Returns the RDF/JSON representation of a literal.

Parameters:

Returns:



87
88
89
90
91
92
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-json-7d579de01769/lib/rdf/json/writer.rb', line 87

def format_literal(value, **options)
  case value
    when RDF::Literal then value.to_rdf_json.to_json
    else RDF::Literal.new(value).to_rdf_json.to_json
  end
end

#format_node(value, **options) ⇒ String

Returns the RDF/JSON representation of a blank node.

Parameters:

Returns:



67
68
69
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-json-7d579de01769/lib/rdf/json/writer.rb', line 67

def format_node(value, **options)
  value.to_rdf_json.to_json
end

#format_uri(value, **options) ⇒ String

Returns the RDF/JSON representation of a URI reference.

Parameters:

Returns:



77
78
79
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-json-7d579de01769/lib/rdf/json/writer.rb', line 77

def format_uri(value, **options)
  value.to_rdf_json.to_json
end

#write_epilogue

This method returns an undefined value.

Outputs the RDF/JSON representation of all stored triples.

See Also:



56
57
58
59
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-json-7d579de01769/lib/rdf/json/writer.rb', line 56

def write_epilogue
  puts @json.to_json
  super
end

#write_triple(subject, predicate, object)

This method returns an undefined value.

Stores the RDF/JSON representation of a triple.

Parameters:

See Also:



41
42
43
44
45
46
47
48
49
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-json-7d579de01769/lib/rdf/json/writer.rb', line 41

def write_triple(subject, predicate, object)
  s = subject.to_s
  p = predicate.to_s
  o = object.is_a?(RDF::Value) ? object : RDF::Literal.new(object)
  @json       ||= {}
  @json[s]    ||= {}
  @json[s][p] ||= []
  @json[s][p] << o.to_rdf_json
end