You are here

class EasyRdf_Parser_Json in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Json.php \EasyRdf_Parser_Json

A pure-php class to parse RDF/JSON with no dependancies.

http://n2.talis.com/wiki/RDF_JSON_Specification docs/appendix-a-rdf-formats-json.md

@package EasyRdf @copyright Copyright (c) 2009-2013 Nicholas J Humfrey @license http://www.opensource.org/licenses/bsd-license.php

Hierarchy

Expanded class hierarchy of EasyRdf_Parser_Json

1 string reference to 'EasyRdf_Parser_Json'
Format.php in vendor/easyrdf/easyrdf/lib/EasyRdf/Format.php

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Json.php, line 48

View source
class EasyRdf_Parser_Json extends EasyRdf_Parser_RdfPhp {
  private $jsonLastErrorExists = false;

  /**
   * Constructor
   *
   * @return object EasyRdf_Parser_Json
   */
  public function __construct() {
    $this->jsonLastErrorExists = function_exists('json_last_error');
  }

  /** Return the last JSON parser error as a string
   *
   * If json_last_error() is not available a generic message will be returned.
   *
   * @ignore
   */
  protected function jsonLastErrorString() {
    if ($this->jsonLastErrorExists) {
      switch (json_last_error()) {
        case JSON_ERROR_NONE:
          return null;
        case JSON_ERROR_DEPTH:
          return "JSON Parse error: the maximum stack depth has been exceeded";
        case JSON_ERROR_STATE_MISMATCH:
          return "JSON Parse error: invalid or malformed JSON";
        case JSON_ERROR_CTRL_CHAR:
          return "JSON Parse error: control character error, possibly incorrectly encoded";
        case JSON_ERROR_SYNTAX:
          return "JSON Parse syntax error";
        case JSON_ERROR_UTF8:
          return "JSON Parse error: malformed UTF-8 characters, possibly incorrectly encoded";
        default:
          return "JSON Parse error: unknown";
      }
    }
    else {
      return "JSON Parse error";
    }
  }

  /** Parse the triple-centric JSON format, as output by libraptor
   *
   * http://librdf.org/raptor/api/serializer-json.html
   *
   * @ignore
   */
  protected function parseJsonTriples($data, $baseUri) {
    foreach ($data['triples'] as $triple) {
      if ($triple['subject']['type'] == 'bnode') {
        $subject = $this
          ->remapBnode($triple['subject']['value']);
      }
      else {
        $subject = $triple['subject']['value'];
      }
      $predicate = $triple['predicate']['value'];
      if ($triple['object']['type'] == 'bnode') {
        $object = array(
          'type' => 'bnode',
          'value' => $this
            ->remapBnode($triple['object']['value']),
        );
      }
      else {
        $object = $triple['object'];
      }
      $this
        ->addTriple($subject, $predicate, $object);
    }
    return $this->tripleCount;
  }

  /**
   * Parse RDF/JSON into an EasyRdf_Graph
   *
   * @param object EasyRdf_Graph $graph   the graph to load the data into
   * @param string               $data    the RDF document data
   * @param string               $format  the format of the input data
   * @param string               $baseUri the base URI of the data being parsed
   * @return integer             The number of triples added to the graph
   */
  public function parse($graph, $data, $format, $baseUri) {
    $this
      ->checkParseParams($graph, $data, $format, $baseUri);
    if ($format != 'json') {
      throw new EasyRdf_Exception("EasyRdf_Parser_Json does not support: {$format}");
    }
    $decoded = @json_decode(strval($data), true);
    if ($decoded === null) {
      throw new EasyRdf_Parser_Exception($this
        ->jsonLastErrorString());
    }
    if (array_key_exists('triples', $decoded)) {
      return $this
        ->parseJsonTriples($decoded, $baseUri);
    }
    else {
      return parent::parse($graph, $decoded, 'php', $baseUri);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EasyRdf_Parser::$baseUri protected property The base URI for the document currently being parsed
EasyRdf_Parser::$bnodeMap private property Mapping from source to graph bnode identifiers
EasyRdf_Parser::$format protected property The format of the document currently being parsed
EasyRdf_Parser::$graph protected property The current graph to insert triples into
EasyRdf_Parser::$tripleCount protected property
EasyRdf_Parser::addTriple protected function Add a triple to the current graph, and keep count of the number of triples @ignore 1
EasyRdf_Parser::checkParseParams protected function Check, cleanup parameters and prepare for parsing @ignore
EasyRdf_Parser::remapBnode protected function Create a new, unique bnode identifier from a source identifier. If the source identifier has previously been seen, the same new bnode identifier is returned. @ignore
EasyRdf_Parser::resetBnodeMap protected function Delete the bnode mapping - to be called at the start of a new parse @ignore
EasyRdf_Parser_Json::$jsonLastErrorExists private property
EasyRdf_Parser_Json::jsonLastErrorString protected function Return the last JSON parser error as a string
EasyRdf_Parser_Json::parse public function Parse RDF/JSON into an EasyRdf_Graph Overrides EasyRdf_Parser_RdfPhp::parse 1
EasyRdf_Parser_Json::parseJsonTriples protected function Parse the triple-centric JSON format, as output by libraptor
EasyRdf_Parser_Json::__construct public function Constructor Overrides EasyRdf_Parser_RdfPhp::__construct 1