Add helper jsonld encode/decode functions.

This commit is contained in:
Dave Longley 2014-03-18 12:10:44 -04:00
parent b04f314afa
commit f77210c1c1

View file

@ -1,7 +1,7 @@
<?php <?php
/** /**
* PHP implementation of the JSON-LD API. * PHP implementation of the JSON-LD API.
* Version: 0.2.7 * Version: 0.2.8
* *
* @author Dave Longley * @author Dave Longley
* *
@ -161,6 +161,34 @@ function jsonld_to_rdf($input, $options=array()) {
return $p->toRDF($input, $options); return $p->toRDF($input, $options);
} }
/**
* JSON-encodes (with unescaped slashes) the given stdClass or array.
*
* @param mixed $input the native PHP stdClass or array which will be
* converted to JSON by json_encode().
*
* @return the encoded JSON data.
*/
function jsonld_encode($input) {
// newer PHP has a flag to avoid escaped '/'
if(defined('JSON_UNESCAPED_SLASHES')) {
return json_encode($input, JSON_UNESCAPED_SLASHES);
}
// use a simple string replacement of '\/' to '/'.
return str_replace('\\/', '/', json_encode($input));
}
/**
* Decodes a serialized JSON-LD object.
*
* @param string $input the JSON-LD input.
*
* @return mixed the resolved JSON-LD object, null on error.
*/
function jsonld_decode($input) {
return json_decode($input);
}
/** /**
* Parses a link header. The results will be key'd by the value of "rel". * Parses a link header. The results will be key'd by the value of "rel".
* *