/usr/share/php/arc/serializers/ARC2_RDFJSONSerializer.php is in libarc-php 2.2.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | <?php
/**
* ARC2 RDF/JSON Serializer
*
* @author Benjamin Nowack <bnowack@semsol.com>
* @license http://arc.semsol.org/license
* @homepage <http://arc.semsol.org/>
* @package ARC2
*/
ARC2::inc('RDFSerializer');
class ARC2_RDFJSONSerializer extends ARC2_RDFSerializer {
function __construct($a, &$caller) {
parent::__construct($a, $caller);
}
function __init() {
parent::__init();
$this->content_header = 'application/json';
}
/* */
function getTerm($v, $term = 's') {
if (!is_array($v)) {
if (preg_match('/^\_\:/', $v)) {
return ($term == 'o') ? $this->getTerm(array('value' => $v, 'type' => 'bnode'), 'o') : '"' . $v . '"';
}
return ($term == 'o') ? $this->getTerm(array('value' => $v, 'type' => 'uri'), 'o') : '"' . $v . '"';
}
if (!isset($v['type']) || ($v['type'] != 'literal')) {
if ($term != 'o') {
return $this->getTerm($v['value'], $term);
}
if (preg_match('/^\_\:/', $v['value'])) {
return '{ "value" : "' . $this->jsonEscape($v['value']) . '", "type" : "bnode" }';
}
return '{ "value" : "' . $this->jsonEscape($v['value']) . '", "type" : "uri" }';
}
/* literal */
$r = '{ "value" : "' . $this->jsonEscape($v['value']) . '", "type" : "literal"';
$suffix = isset($v['datatype']) ? ', "datatype" : "' . $v['datatype'] . '"' : '';
$suffix = isset($v['lang']) ? ', "lang" : "' . $v['lang'] . '"' : $suffix;
$r .= $suffix . ' }';
return $r;
}
function jsonEscape($v) {
if (function_exists('json_encode')) {
return preg_replace('/^"(.*)"$/', '\\1', str_replace("\/","/",json_encode($v)));
}
$from = array("\\", "\r", "\t", "\n", '"', "\b", "\f");
$to = array('\\\\', '\r', '\t', '\n', '\"', '\b', '\f');
return str_replace($from, $to, $v);
}
function getSerializedIndex($index, $raw = 0) {
$r = '';
$nl = "\n";
foreach ($index as $s => $ps) {
$r .= $r ? ',' . $nl . $nl : '';
$r .= ' ' . $this->getTerm($s). ' : {';
$first_p = 1;
foreach ($ps as $p => $os) {
$r .= $first_p ? $nl : ',' . $nl;
$r .= ' ' . $this->getTerm($p). ' : [';
$first_o = 1;
if (!is_array($os)) {/* single literal o */
$os = array(array('value' => $os, 'type' => 'literal'));
}
foreach ($os as $o) {
$r .= $first_o ? $nl : ',' . $nl;
$r .= ' ' . $this->getTerm($o, 'o');
$first_o = 0;
}
$first_p = 0;
$r .= $nl . ' ]';
}
$r .= $nl . ' }';
}
$r .= $r ? ' ' : '';
return '{' . $nl . $r . $nl . '}';
}
/* */
}
|