Class: JSON::LD::Writer

Inherits:
RDF::Writer
  • Object
show all
Includes:
Utils
Defined in:
lib/json/ld/writer.rb

Overview

A JSON-LD parser in Ruby.

Note that the natural interface is to write a whole graph at a time. Writing statements or Triples will create a graph to add them to and then serialize the graph.

The writer will add prefix definitions, and use them for creating @context definitions, and minting CURIEs

Select the :expand option to output JSON-LD in expanded form

Examples:

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(:content_type   => "application/turtle")

Serializing RDF graph into an JSON-LD file

JSON::LD::Writer.open("etc/test.json") do |writer|
  writer << graph
end

Serializing RDF statements into an JSON-LD file

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

Serializing RDF statements into an JSON-LD string

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

Creating @@context prefix definitions in output

JSON::LD::Writer.buffer(
  :prefixes => {
    nil => "http://example.com/ns#",
    :foaf => "http://xmlns.com/foaf/0.1/"}
) do |writer|
  graph.each_statement do |statement|
    writer << statement
  end
end

See Also:

Author:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#as_resource, #blank_node?, #index?, #list?, #node?, #node_reference?, #value?

Constructor Details

#initialize(output = $stdout, options = {}) {|writer| ... } ⇒ Writer

Initializes the RDF-LD writer instance.

Parameters:

  • output (IO, File) (defaults to: $stdout)

    the output stream

  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional options

Options Hash (options):

  • :encoding (Encoding) — default: Encoding::UTF_8

    the encoding to use on the output stream (Ruby 1.9+)

  • :canonicalize (Boolean) — default: false

    whether to canonicalize literals when serializing

  • :prefixes (Hash) — default: Hash.ordered

    the prefix mappings to use (not supported by all writers)

  • :standard_prefixes (Boolean) — default: false

    Add standard prefixes to @prefixes, if necessary.

  • :context (IO, Array, Hash, String, Context) — default: Hash.ordered

    context to use when serializing. Constructed context for native serialization.

Yields:

  • (writer)

    ‘self`

  • (writer)

Yield Parameters:

  • writer (RDF::Writer)
  • writer (RDF::Writer)

Yield Returns:

  • (void)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/json/ld/writer.rb', line 93

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

    if block_given?
      case block.arity
        when 0 then instance_eval(&block)
        else block.call(self)
      end
    end
  end
end

Instance Attribute Details

#contextContext (readonly)

Returns context used to load and administer contexts.

Returns:

  • (Context)

    context used to load and administer contexts



63
64
65
# File 'lib/json/ld/writer.rb', line 63

def context
  @context
end

#graphRDF::Graph (readonly)

Returns Graph of statements serialized.

Returns:



59
60
61
# File 'lib/json/ld/writer.rb', line 59

def graph
  @graph
end

Class Method Details

.to_symObject

Override normal symbol generation



67
68
69
# File 'lib/json/ld/writer.rb', line 67

def self.to_sym
  :jsonld
end

Instance Method Details

#write_epiloguevoid

This method returns an undefined value.

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

See Also:



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/json/ld/writer.rb', line 146

def write_epilogue
  @debug = @options[:debug]

  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
  context = RDF::Util::File.open_file(@options[:context]) if @options[:context].is_a?(String)
  context ||= @options[:context]
  context ||= if @options[:prefixes] || @options[:language] || @options[:standard_prefixes]
    ctx = Context.new(@options)
    ctx.language = @options[:language] if @options[:language]
    @options[:prefixes].each do |prefix, iri|
      ctx.set_mapping(prefix, iri) if prefix && iri
    end if @options[:prefixes]
    ctx
  end
  
  # Perform compaction, if we have a context
  if context
    debug("writer") { "compact result"}
    result = API.compact(result, context,  @options)
  end

  @output.write(result.to_json(JSON_STATE))
end

#write_graph(graph) ⇒ void

This method returns an undefined value.

Write whole graph

Parameters:

  • graph (Graph)


113
114
115
116
# File 'lib/json/ld/writer.rb', line 113

def write_graph(graph)
  debug {"Add graph #{graph.inspect}"}
  @repo = graph
end

#write_statement(statement) ⇒ void

This method returns an undefined value.

Adds a statement to be serialized

Parameters:

  • statement (RDF::Statement)


122
123
124
# File 'lib/json/ld/writer.rb', line 122

def write_statement(statement)
  @repo.insert(statement)
end

#write_triple(subject, predicate, object) ⇒ void

This method is abstract.

This method returns an undefined value.

Addes a triple to be serialized

Parameters:

  • subject (RDF::Resource)
  • predicate (RDF::URI)
  • object (RDF::Value)

Raises:

  • (NotImplementedError)

    unless implemented in subclass



134
135
136
# File 'lib/json/ld/writer.rb', line 134

def write_triple(subject, predicate, object)
  @repo.insert(Statement.new(subject, predicate, object))
end