Class: RDF::JSON::Reader

Inherits:
Reader show all
Includes:
Util::Logger
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-json-3ea668ef8905/lib/rdf/json/reader.rb

Overview

RDF/JSON parser.

Examples:

Loading RDF/JSON parsing support

require 'rdf/json'

Obtaining an RDF/JSON reader class

RDF::Reader.for(:json)         #=> RDF::JSON::Reader
RDF::Reader.for("etc/doap.json")
RDF::Reader.for(:file_name      => "etc/doap.json")
RDF::Reader.for(:file_extension => "json")
RDF::Reader.for(:content_type   => "application/json")

Parsing RDF statements from an RDF/JSON file

RDF::JSON::Reader.open("etc/doap.json") do |reader|
  reader.each_statement do |statement|
    puts statement.inspect
  end
end

Parsing RDF statements from an RDF/JSON string

data = StringIO.new(File.read("etc/doap.json"))
RDF::JSON::Reader.new(data) do |reader|
  reader.each_statement do |statement|
    puts statement.inspect
  end
end

See Also:

Instance Attribute Summary collapse

Attributes inherited from Reader

#options

Instance Method Summary collapse

Methods included from Util::Logger

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

Methods inherited from Reader

#base_uri, #canonicalize?, #close, each, #encoding, #fail_object, #fail_predicate, #fail_subject, for, format, #intern?, #lineno, open, options, #prefix, #prefixes, #prefixes=, #read_statement, #read_triple, #rewind, #to_sym, to_sym, #valid?, #validate?

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Enumerable

add_entailment, #dump, #each_graph, #each_object, #each_predicate, #each_quad, #each_subject, #each_term, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph_names, #has_graph?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_term?, #has_triple?, #invalid?, #method_missing, #objects, #predicates, #project_graph, #quads, #respond_to_missing?, #statements, #subjects, #supports?, #terms, #to_a, #to_h, #to_set, #triples, #valid?, #validate!

Methods included from Countable

#count, #empty?

Methods included from Isomorphic

#bijection_to, #isomorphic_with?

Methods included from Readable

#readable?

Constructor Details

#initialize(input = $stdin, options = {}) {|reader| ... } ⇒ Reader

Initializes the RDF/JSON reader instance.

Parameters:

  • input (IO, File, String) (defaults to: $stdin)
  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional options (see RDF::Reader#initialize)

Yields:

  • (reader)

    self

Yield Parameters:

Yield Returns:

  • (void)

    ignored



50
51
52
53
54
55
56
57
58
59
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-json-3ea668ef8905/lib/rdf/json/reader.rb', line 50

def initialize(input = $stdin, options = {}, &block)
  super do
    if block_given?
      case block.arity
        when 0 then instance_eval(&block)
        else block.call(self)
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Enumerable

Instance Attribute Details

#graphRDF::Graph (readonly)

The graph constructed when parsing.

Returns:



39
40
41
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-json-3ea668ef8905/lib/rdf/json/reader.rb', line 39

def graph
  @graph
end

Instance Method Details

#parse_node(string) ⇒ RDF::Node Also known as: parse_bnode

Parses an RDF/JSON blank node string into an RDF::Node instance.

Parameters:

Returns:

Since:

  • 0.3.0



118
119
120
121
122
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-json-3ea668ef8905/lib/rdf/json/reader.rb', line 118

def parse_node(string)
  @nodes ||= {}
  id = string[2..-1] # strips off the initial '_:'
  @nodes[id.to_sym] ||= RDF::Node.new(id)
end

#parse_object(object) ⇒ RDF::Value

Parses an RDF/JSON object string into an RDF value.

Parameters:

Returns:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-json-3ea668ef8905/lib/rdf/json/reader.rb', line 88

def parse_object(object)
  log_error("missing 'type' key in #{object.inspect}", exception: RDF::ReaderError) unless object.has_key?('type')
  log_error("missing 'value' key in #{object.inspect}", exception: RDF::ReaderError) unless object.has_key?('value')

  case type = object['type']
    when 'bnode'
      parse_node(object['value'])
    when 'uri'
      parse_uri(object['value'])
    when 'literal'
      literal = RDF::Literal.new(object['value'], {
        :language => object['lang'],
        :datatype => object['datatype'],
      })
      literal.validate!     if validate?
      literal.canonicalize! if canonicalize?
      literal
    else
      log_error("expected 'type' to be 'bnode', 'uri', or 'literal', but got #{type.inspect}", exception: RDF::ReaderError)
  end
rescue RDF::ReaderError
  nil
end

#parse_predicate(predicate) ⇒ RDF::URI

Parses an RDF/JSON predicate string into a URI reference.

Parameters:

Returns:



78
79
80
81
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-json-3ea668ef8905/lib/rdf/json/reader.rb', line 78

def parse_predicate(predicate)
  # TODO: optional support for CURIE predicates? (issue #1 on GitHub).
  parse_uri(predicate, :intern => true)
end

#parse_subject(subject) ⇒ RDF::Resource

Parses an RDF/JSON subject string into a URI reference or blank node.

Parameters:

Returns:



66
67
68
69
70
71
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-json-3ea668ef8905/lib/rdf/json/reader.rb', line 66

def parse_subject(subject)
  case subject
    when /^_:/ then parse_node(subject)
    else parse_uri(subject)
  end
end

#parse_uri(string, options = {}) ⇒ RDF::URI

Parses an RDF/JSON URI string into an RDF::URI instance.

Parameters:

Options Hash (options):

  • :intern (Boolean) — default: false

Returns:

Since:

  • 0.3.0



133
134
135
136
137
138
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-json-3ea668ef8905/lib/rdf/json/reader.rb', line 133

def parse_uri(string, options = {})
  uri = RDF::URI.send(intern = intern? && options[:intern] ? :intern : :new, string)
  uri.validate!     if validate?
  uri.canonicalize! if canonicalize? && !intern
  uri
end