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()
{
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();
}
/**

View File

@ -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') {

View File

@ -153,7 +153,7 @@ class Address implements \JsonSerializable
*/
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
{
return json_encode($this->toArray());
return $this->toArray();
}
/**

View File

@ -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)] ?? [];
}
/**

View File

@ -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');
}

View File

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