Class: RDFObject::JSONParser

Inherits:
Parser
  • Object
show all
Defined in:
lib/rdf_objects/parsers.rb

Instance Attribute Summary

Attributes inherited from Parser

#base_uri, #collection

Instance Method Summary collapse

Methods inherited from Parser

init_parser, #initialize, parse, #sanitize_uri

Constructor Details

This class inherits a constructor from RDFObject::Parser

Instance Method Details

#data=(json) ⇒ Object



468
469
470
471
472
473
474
475
476
# File 'lib/rdf_objects/parsers.rb', line 468

def data=(json)
  if json.is_a?(String)
    @json = JSON.parse(json)
  elsif json.is_a?(Hash)
    @json = json
  elsif json.respond_to?(:read)
    @json = JSON.parse(json.read)
  end
end

#parseObject



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/rdf_objects/parsers.rb', line 478

def parse
  @json.each_pair do |subject, assertions|
    resource = @collection.find_or_create(subject)
    assertions.each_pair do |predicate, objects|
      objects.each do | object |
        if object['type'] == 'literal'
          opts = {}
          if object['lang']
            opts[:language] = object['lang'].to_sym
          end
          if object['datatype']
            opts[:datatype] = object['datatype']
          end
          literal = RDF::Literal.new(object['value'],opts)
          resource.assert(predicate, literal)
        elsif object['type'] == 'uri'
          o = @collection.find_or_create(object['value'])
          resource.assert(predicate, o)
        elsif object['type'] == 'bnode' # For now, we're going to treat a blank node like a URI resource.
          o = @collection.find_or_create(object['value'])
          resource.assert(predicate, o)
        end
      end
    end
  end
  @collection
end