Fix json serialization

This commit is contained in:
Dennis Eichhorn 2017-01-22 21:04:31 +01:00
parent ae1a364378
commit 695f1623b1
7 changed files with 20 additions and 17 deletions

View File

@ -216,7 +216,7 @@ class Group implements ArrayableInterface, \JsonSerializable
*/ */
public function __toString() public function __toString()
{ {
return $this->jsonSerialize(); return json_encode($this->toArray());
} }
/** /**
@ -229,7 +229,7 @@ class Group implements ArrayableInterface, \JsonSerializable
*/ */
public function jsonSerialize() public function jsonSerialize()
{ {
return json_encode($this->toArray()); return $this->toArray();
} }
/** /**

View File

@ -660,12 +660,12 @@ class DataMapperAbstract implements DataMapperInterface
return null; return null;
} elseif ($type === 'DateTime') { } elseif ($type === 'DateTime') {
return $value->format('Y-m-d H:i:s'); return $value->format('Y-m-d H:i:s');
} elseif ($type === 'json') { } elseif ($type === 'Json' || $type === 'jsonSerializable') {
return json_encode($value); return json_encode($value);
} elseif ($type === 'Serializable') { } elseif ($type === 'Serializable') {
return $value->serialize(); return $value->serialize();
} elseif ($type === 'jsonSerializable') { } elseif ($value instanceof \JsonSerializable) {
return $value->jsonSerializable(); return json_encode($value->jsonSerialize());
} elseif (is_object($value)) { } elseif (is_object($value)) {
return $value->getId(); return $value->getId();
} elseif ($type === 'int') { } elseif ($type === 'int') {

View File

@ -153,7 +153,7 @@ class Address implements \JsonSerializable
*/ */
public function jsonSerialize(int $option = 0) : string public function jsonSerialize(int $option = 0) : string
{ {
return json_encode($this->toArray()); return $this->toArray();
} }
/** /**

View File

@ -247,7 +247,7 @@ class Location implements \JsonSerializable, \Serializable
*/ */
public function jsonSerialize() : string public function jsonSerialize() : string
{ {
return json_encode($this->toArray()); return $this->toArray();
} }
/** /**

View File

@ -63,8 +63,8 @@ class Header extends HeaderAbstract
if (!$overwrite && isset($this->header[$key])) { if (!$overwrite && isset($this->header[$key])) {
return false; return false;
} elseif ($overwrite && isset($this->header[$key])) { } elseif ($overwrite || !isset($this->header[$key])) {
if ($this->isSecurityHeader($key)) { if ($this->isSecurityHeader($key) && isset($this->header[$key])) {
throw new \Exception('Cannot change security headers.'); throw new \Exception('Cannot change security headers.');
} }
@ -166,7 +166,7 @@ class Header extends HeaderAbstract
*/ */
public function get(string $key) : array public function get(string $key) : array
{ {
return $this->header[$key] ?? []; return $this->header[strtolower($key)] ?? [];
} }
/** /**

View File

@ -114,12 +114,15 @@ class Response extends ResponseAbstract implements RenderableInterface
*/ */
public function render() : string public function render() : string
{ {
switch ($this->header->get('Content-Type')) { $types = $this->header->get('Content-Type');
case MimeType::M_JSON:
foreach($types as $type) {
if(stripos($type, MimeType::M_JSON) !== false) {
return $this->jsonSerialize(); return $this->jsonSerialize();
default: }
return $this->getRaw();
} }
return $this->getRaw();
} }
/** /**
@ -167,8 +170,8 @@ class Response extends ResponseAbstract implements RenderableInterface
$result += $response; $result += $response;
} elseif (is_scalar($response)) { } elseif (is_scalar($response)) {
$result[] = $response; $result[] = $response;
} elseif ($response instanceof \Serializable) { } elseif ($response instanceof \JsonSerializable) {
$result[] = $response->serialize(); $result[] = $response->jsonSerialize();
} else { } else {
throw new \Exception('Wrong response type'); throw new \Exception('Wrong response type');
} }

View File

@ -74,7 +74,7 @@ class Collection implements \Countable, \ArrayAccess, \Iterator, \JsonSerializab
*/ */
public function jsonSerialize() public function jsonSerialize()
{ {
return json_encode($this->collection); return $this->collection;
} }
/** /**