Skip to content

Commit 53d1934

Browse files
committed
Json bug fixes for stdClass support
1 parent 34cbfda commit 53d1934

6 files changed

Lines changed: 25 additions & 11 deletions

File tree

FeastTests/JsonTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ public function testUnmarshalMarshal(): void
173173
$this->assertEquals($data, Json::marshal(Json::unmarshal($data, TestJsonItem::class)));
174174
}
175175

176+
public function testUnmarshalMarshalStdClass(): void
177+
{
178+
$data = '{"first_name":"FEAST","last_name":"Framework","test_item":{"first_name":"Jeremy","last_name":"Presutti","calls":4},"second_item":{"also_first_name":"Orlando","also_last_name":"Florida"},"items":[{"first_name":"PHP","last_name":"7.4","calls":null},{"first_name":"PHP","last_name":"8.0","calls":null}],"cards":["4",5,["6"]],"otherItems":{"first":{"first_name":"Json","last_name":"Serializer","calls":null},"second":{"first_name":"Item","last_name":"Parsing","calls":null}},"thirdItems":{"test":"theTest","test2":"theTest2"},"otherSet":[{"first_name":"Json","last_name":"Serializer","calls":null},{"first_name":"Item","last_name":"Parsing","calls":null}],"thirdSet":["theTest","theTest2"],"calls":null,"count":4,"aClass":{"test":"ItWorks"},"timestamp":"20210415","otherTimestamp":"2021-04-05T06:41:00-04:00","thirdTimestamp":"2021-04-15T20:56:24-04:00","fourthTimestamp":"20210405"}';
179+
$this->assertEquals($data, Json::marshal(Json::unmarshal($data, stdClass::class)));
180+
}
181+
176182
public function testUnmarshalMarshalUnmarshalMarshal(): void
177183
{
178184
$data = '{"first_name":"FEAST","last_name":"Framework","test_item":{"first_name":"Jeremy","last_name":"Presutti","calls":4},"second_item":{"also_first_name":"Orlando","also_last_name":"Florida"},"items":[{"first_name":"PHP","last_name":"7.4","calls":null},{"first_name":"PHP","last_name":"8.0","calls":null}],"cards":["4",5,["6"]],"otherItems":{"first":{"first_name":"Json","last_name":"Serializer","calls":null},"second":{"first_name":"Item","last_name":"Parsing","calls":null}},"thirdItems":{"test":"theTest","test2":"theTest2"},"otherSet":[{"first_name":"Json","last_name":"Serializer","calls":null},{"first_name":"Item","last_name":"Parsing","calls":null}],"thirdSet":["theTest","theTest2"],"calls":null,"count":4,"aClass":{"test":"ItWorks"},"timestamp":"20210415","otherTimestamp":"2021-04-05T06:41:00-04:00","thirdTimestamp":"2021-04-15T20:56:24-04:00","fourthTimestamp":"20210405"}';

HttpController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ public function forward(
9393
}
9494

9595
/**
96-
* @param object $responseObject
96+
* @param object|array $responseObject
9797
* @param int|null $jsonResponsePropertyTypes (see https://www.php.net/manual/en/class.reflectionproperty.php#reflectionproperty.constants.modifiers)
9898
* @return void
9999
*/
100-
public function sendJsonResponse(object $responseObject, ?int $jsonResponsePropertyTypes = null): void
100+
public function sendJsonResponse(object|array $responseObject, ?int $jsonResponsePropertyTypes = null): void
101101
{
102102
$this->response->setJsonWithResponseObject($responseObject, $jsonResponsePropertyTypes);
103103
}

HttpRequest/Response.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
namespace Feast\HttpRequest;
2222

2323
use Exception;
24+
use Feast\Enums\ResponseCode;
2425
use SimpleXMLElement;
2526
use stdClass;
2627

Interfaces/ResponseInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ public function getRedirectPath(): ?string;
8888
/**
8989
* Mark the Response as a JSON response and send the passed in object.
9090
*
91-
* @param object $response
91+
* @param object|array $response
9292
* @param int|null $jsonResponsePropertyTypes (see https://www.php.net/manual/en/class.reflectionproperty.php#reflectionproperty.constants.modifiers)
9393
*/
94-
public function setJsonWithResponseObject(object $response, ?int $jsonResponsePropertyTypes = null): void;
94+
public function setJsonWithResponseObject(object|array $response, ?int $jsonResponsePropertyTypes = null): void;
9595

9696
/**
9797
* Set an HTTP header. Overrides previous version set.

Json.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@ class Json
4141
*
4242
* The field names are kept as is, unless a Feast\Attributes\JsonItem attribute decorates the property.
4343
*
44-
* @param object $object
44+
* @param object|array $object
4545
* @param int|null $propertyTypesFlag (see https://www.php.net/manual/en/class.reflectionproperty.php#reflectionproperty.constants.modifiers)
4646
* @return string
4747
* @throws ReflectionException
4848
* @see \Feast\Attributes\JsonItem
4949
*/
50-
public static function marshal(object $object, ?int $propertyTypesFlag = null): string
50+
public static function marshal(object|array $object, ?int $propertyTypesFlag = null): string
5151
{
52+
if ($object instanceof stdClass || is_array($object)) {
53+
return json_encode($object);
54+
}
5255
$return = new stdClass();
5356
$paramInfo = self::getClassParamInfo($object::class, $propertyTypesFlag);
5457
/**
@@ -98,15 +101,19 @@ public static function marshal(object $object, ?int $propertyTypesFlag = null):
98101
* @param class-string<returned>|returned $objectOrClass
99102
* @psalm-param object|class-string $objectOrClass
100103
* @param bool $skipConstructor
101-
* @throws Exception\InvalidDateException
104+
* @return returned|object
102105
* @throws JsonException
103106
* @throws ReflectionException
104107
* @throws ServerFailureException
108+
* @throws Exception\InvalidDateException
105109
* @see \Feast\Attributes\JsonItem
106-
* @return returned|object
107110
*/
108111
public static function unmarshal(string $data, string|object $objectOrClass, bool $skipConstructor = false): object
109112
{
113+
if ($objectOrClass === stdClass::class) {
114+
/** @var stdClass */
115+
return json_decode($data);
116+
}
110117
if (is_string($objectOrClass)) {
111118
$object = self::getObjectFromClassString($objectOrClass, $skipConstructor);
112119
} else {

Response.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Response implements ServiceContainerItemInterface, ResponseInterface
4040

4141
private int $responseCode = ResponseCode::HTTP_CODE_200;
4242
private bool $isJson = false;
43-
private object|null $jsonResponse = null;
43+
private object|array|null $jsonResponse = null;
4444
private ?int $jsonResponsePropertyTypes = null;
4545
private ?string $redirectPath = null;
4646
/** @var array<string,string> */
@@ -158,10 +158,10 @@ public function redirect(string $path, int $code = 302): void
158158
/**
159159
* Mark the Response as a JSON response and send the passed in object.
160160
*
161-
* @param object $response
161+
* @param object|array $response
162162
* @param int|null $jsonResponsePropertyTypes (see https://www.php.net/manual/en/class.reflectionproperty.php#reflectionproperty.constants.modifiers)
163163
*/
164-
public function setJsonWithResponseObject(object $response, ?int $jsonResponsePropertyTypes = null): void
164+
public function setJsonWithResponseObject(object|array $response, ?int $jsonResponsePropertyTypes = null): void
165165
{
166166
$this->jsonResponse = $response;
167167
$this->jsonResponsePropertyTypes = $jsonResponsePropertyTypes;

0 commit comments

Comments
 (0)