diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index e101582b0..456c21de9 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -149,7 +149,7 @@ class Twig_Extension_Core extends Twig_Extension // encoding new Twig_Filter('url_encode', 'twig_urlencode_filter'), - new Twig_Filter('json_encode', 'twig_jsonencode_filter'), + new Twig_Filter('json_encode', 'json_encode'), new Twig_Filter('convert_encoding', 'twig_convert_encoding'), // string filters @@ -582,29 +582,6 @@ function twig_urlencode_filter($url) return rawurlencode($url); } -/** - * JSON encodes a variable. - * - * @param mixed $value The value to encode. - * @param int $options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT - * - * @return mixed The JSON encoded value - */ -function twig_jsonencode_filter($value, $options = 0) -{ - if ($value instanceof Twig_Markup) { - $value = (string) $value; - } elseif (is_array($value)) { - array_walk_recursive($value, function (&$value) { - if ($value instanceof Twig_Markup) { - $value = (string) $value; - } - }); - } - - return json_encode($value, $options); -} - /** * Merges an array with another one. * diff --git a/lib/Twig/Markup.php b/lib/Twig/Markup.php index 1620a41b4..8c8741e09 100644 --- a/lib/Twig/Markup.php +++ b/lib/Twig/Markup.php @@ -14,7 +14,7 @@ * * @author Fabien Potencier */ -class Twig_Markup implements Countable +class Twig_Markup implements Countable, JsonSerializable { private $content; private $charset; @@ -34,4 +34,9 @@ class Twig_Markup implements Countable { return mb_strlen($this->content, $this->charset); } + + public function jsonSerialize() + { + return $this->content; + } }