25 class JsonResponse
extends Response
44 parent::__construct(
'', $status,
$headers);
47 $data = new \ArrayObject();
107 $pattern =
'/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*(?:\[(?:"(?:\\\.|[^"\\\])*"|\'(?:\\\.|[^\'\\\])*\'|\d+)\])*?$/u';
109 'break',
'do',
'instanceof',
'typeof',
'case',
'else',
'new',
'var',
'catch',
'finally',
'return',
'void',
'continue',
'for',
'switch',
'while',
110 'debugger',
'function',
'this',
'with',
'default',
'if',
'throw',
'delete',
'in',
'try',
'class',
'enum',
'extends',
'super',
'const',
'export',
111 'import',
'implements',
'let',
'private',
'public',
'yield',
'interface',
'package',
'protected',
'static',
'null',
'true',
'false',
114 foreach ($parts as $part) {
115 if (!preg_match($pattern, $part) || \in_array($part, $reserved,
true)) {
116 throw new \InvalidArgumentException(
'The callback name is not valid.');
154 $data = json_encode(
$data, $this->encodingOptions);
155 }
catch (\Exception $e) {
156 if (
'Exception' === \get_class($e) && 0 === strpos($e->getMessage(),
'Failed calling ')) {
157 throw $e->getPrevious() ?: $e;
162 if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $this->encodingOptions)) {
166 if (JSON_ERROR_NONE !== json_last_error()) {
167 throw new \InvalidArgumentException(json_last_error_msg());
194 return $this->
setData(json_decode($this->data));
204 if (
null !== $this->callback) {
206 $this->headers->set(
'Content-Type',
'text/javascript');
208 return $this->
setContent(sprintf(
'/**/%s(%s);', $this->callback, $this->data));
213 if (!$this->headers->has(
'Content-Type') ||
'text/javascript' === $this->headers->get(
'Content-Type')) {
214 $this->headers->set(
'Content-Type',
'application/json');