From 695f1623b124fa5e9061ca57441d09e6ee856982 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 22 Jan 2017 21:04:31 +0100 Subject: [PATCH] Fix json serialization --- Account/Group.php | 4 ++-- DataStorage/Database/DataMapperAbstract.php | 6 +++--- Datatypes/Address.php | 2 +- Datatypes/Location.php | 2 +- Message/Http/Header.php | 6 +++--- Message/Http/Response.php | 15 +++++++++------ Stdlib/Collection/Collection.php | 2 +- 7 files changed, 20 insertions(+), 17 deletions(-) diff --git a/Account/Group.php b/Account/Group.php index 05da5aa4c..62c5d26b3 100644 --- a/Account/Group.php +++ b/Account/Group.php @@ -216,7 +216,7 @@ class Group implements ArrayableInterface, \JsonSerializable */ public function __toString() { - return $this->jsonSerialize(); + return json_encode($this->toArray()); } /** @@ -229,7 +229,7 @@ class Group implements ArrayableInterface, \JsonSerializable */ public function jsonSerialize() { - return json_encode($this->toArray()); + return $this->toArray(); } /** diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index c2e948cbe..d6622805d 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -660,12 +660,12 @@ class DataMapperAbstract implements DataMapperInterface return null; } elseif ($type === 'DateTime') { return $value->format('Y-m-d H:i:s'); - } elseif ($type === 'json') { + } elseif ($type === 'Json' || $type === 'jsonSerializable') { return json_encode($value); } elseif ($type === 'Serializable') { return $value->serialize(); - } elseif ($type === 'jsonSerializable') { - return $value->jsonSerializable(); + } elseif ($value instanceof \JsonSerializable) { + return json_encode($value->jsonSerialize()); } elseif (is_object($value)) { return $value->getId(); } elseif ($type === 'int') { diff --git a/Datatypes/Address.php b/Datatypes/Address.php index 5965b636f..5592e4a99 100644 --- a/Datatypes/Address.php +++ b/Datatypes/Address.php @@ -153,7 +153,7 @@ class Address implements \JsonSerializable */ public function jsonSerialize(int $option = 0) : string { - return json_encode($this->toArray()); + return $this->toArray(); } /** diff --git a/Datatypes/Location.php b/Datatypes/Location.php index 3b93d3c11..05067b1fb 100644 --- a/Datatypes/Location.php +++ b/Datatypes/Location.php @@ -247,7 +247,7 @@ class Location implements \JsonSerializable, \Serializable */ public function jsonSerialize() : string { - return json_encode($this->toArray()); + return $this->toArray(); } /** diff --git a/Message/Http/Header.php b/Message/Http/Header.php index 2b7aa510b..8af40f49f 100644 --- a/Message/Http/Header.php +++ b/Message/Http/Header.php @@ -63,8 +63,8 @@ class Header extends HeaderAbstract if (!$overwrite && isset($this->header[$key])) { return false; - } elseif ($overwrite && isset($this->header[$key])) { - if ($this->isSecurityHeader($key)) { + } elseif ($overwrite || !isset($this->header[$key])) { + if ($this->isSecurityHeader($key) && isset($this->header[$key])) { throw new \Exception('Cannot change security headers.'); } @@ -166,7 +166,7 @@ class Header extends HeaderAbstract */ public function get(string $key) : array { - return $this->header[$key] ?? []; + return $this->header[strtolower($key)] ?? []; } /** diff --git a/Message/Http/Response.php b/Message/Http/Response.php index a6a632a9d..59d3a49fa 100644 --- a/Message/Http/Response.php +++ b/Message/Http/Response.php @@ -114,12 +114,15 @@ class Response extends ResponseAbstract implements RenderableInterface */ public function render() : string { - switch ($this->header->get('Content-Type')) { - case MimeType::M_JSON: + $types = $this->header->get('Content-Type'); + + foreach($types as $type) { + if(stripos($type, MimeType::M_JSON) !== false) { return $this->jsonSerialize(); - default: - return $this->getRaw(); + } } + + return $this->getRaw(); } /** @@ -167,8 +170,8 @@ class Response extends ResponseAbstract implements RenderableInterface $result += $response; } elseif (is_scalar($response)) { $result[] = $response; - } elseif ($response instanceof \Serializable) { - $result[] = $response->serialize(); + } elseif ($response instanceof \JsonSerializable) { + $result[] = $response->jsonSerialize(); } else { throw new \Exception('Wrong response type'); } diff --git a/Stdlib/Collection/Collection.php b/Stdlib/Collection/Collection.php index 7915b811b..81217b88d 100644 --- a/Stdlib/Collection/Collection.php +++ b/Stdlib/Collection/Collection.php @@ -74,7 +74,7 @@ class Collection implements \Countable, \ArrayAccess, \Iterator, \JsonSerializab */ public function jsonSerialize() { - return json_encode($this->collection); + return $this->collection; } /**