diff --git a/jsonld.php b/jsonld.php index c559e68..557d2b8 100644 --- a/jsonld.php +++ b/jsonld.php @@ -1,7 +1,7 @@ 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". *