8 * Copyright (c) 2009-2013 Nicholas J Humfrey. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
18 * promote products derived from this software without specific prior
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 * @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
35 * @license http://www.opensource.org/licenses/bsd-license.php
39 * Class to serialise an EasyRdf_Graph to JSON-LD
42 * @copyright Copyright (c) 2013 Alexey Zakhlestin
43 * @license http://www.opensource.org/licenses/bsd-license.php
45 class EasyRdf_Serialiser_JsonLd extends EasyRdf_Serialiser
47 public function __construct()
49 if (!class_exists('\ML\JsonLD\JsonLD')) {
50 throw new LogicException('Please install "ml/json-ld" dependency to use JSON-LD serialisation');
53 parent::__construct();
57 * @param EasyRdf_Graph $graph
58 * @param string $format
59 * @param array $options
60 * @throws EasyRdf_Exception
63 public function serialise($graph, $format, array $options = array())
65 parent::checkSerialiseParams($graph, $format);
67 if ($format != 'jsonld') {
68 throw new EasyRdf_Exception(__CLASS__.' does not support: '.$format);
72 $ld_graph = new \ML\JsonLD\Graph();
73 $nodes = array(); // cache for id-to-node association
75 foreach ($graph->toRdfPhp() as $resource => $properties) {
76 if (array_key_exists($resource, $nodes)) {
77 $node = $nodes[$resource];
79 $node = $ld_graph->createNode($resource);
80 $nodes[$resource] = $node;
83 foreach ($properties as $property => $values) {
84 foreach ($values as $value) {
85 if ($value['type'] == 'bnode' or $value['type'] == 'uri') {
86 if (array_key_exists($value['value'], $nodes)) {
87 $_value = $nodes[$value['value']];
89 $_value = $ld_graph->createNode($value['value']);
90 $nodes[$value['value']] = $_value;
92 } elseif ($value['type'] == 'literal') {
93 if (isset($value['lang'])) {
94 $_value = new \ML\JsonLD\LanguageTaggedString($value['value'], $value['lang']);
95 } elseif (isset($value['datatype'])) {
96 $_value = new \ML\JsonLD\TypedValue($value['value'], $value['datatype']);
98 $_value = $value['value'];
101 throw new EasyRdf_Exception(
102 "Unable to serialise object to JSON-LD: ".$value['type']
106 if ($property == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") {
107 $node->addType($_value);
109 $node->addPropertyValue($property, $_value);
116 $use_native_types = !(isset($options['expand_native_types']) and $options['expand_native_types'] == true);
117 $should_compact = (isset($options['compact']) and $options['compact'] == true);
118 $should_frame = isset($options['frame']);
121 $data = $ld_graph->toJsonLd($use_native_types);
124 $data = \ML\JsonLD\JsonLD::frame($data, $options['frame'], $options);
128 if ($should_compact) {
130 $compact_context = isset($options['context']) ? $options['context'] : null;
131 $compact_options = array(
132 'useNativeTypes' => $use_native_types,
133 'base' => $graph->getUri()
136 $data = \ML\JsonLD\JsonLD::compact($data, $compact_context, $compact_options);
139 return \ML\JsonLD\JsonLD::toString($data);