Skip to content

Commit 58939c0

Browse files
committed
Merge branch 'v1.x' into v2.x
2 parents d345a03 + 9997c36 commit 58939c0

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

FeastTests/JsonTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,47 @@ public function testUnmarshal(): void
167167
$this->assertEquals('ItWorks', $result->aClass->test);
168168
}
169169

170+
public function testMarshalArray(): void
171+
{
172+
$firstItem = new TestJsonItem();
173+
$firstItem->firstName = 'FEAST';
174+
$firstItem->lastName = 'Framework';
175+
$secondItem = new TestJsonItem();
176+
$secondItem->firstName = 'Jeremy';
177+
$secondItem->lastName = 'Presutti';
178+
179+
$data = [$firstItem, $secondItem];
180+
181+
$result = Json::marshal($data);
182+
$this->assertEquals('[{"first_name":"FEAST","last_name":"Framework","calls":null},{"first_name":"Jeremy","last_name":"Presutti","calls":null}]', $result);
183+
}
184+
185+
public function testMarshalArrayStdClass(): void
186+
{
187+
$firstItem = new stdClass();
188+
$firstItem->firstName = 'FEAST';
189+
$firstItem->lastName = 'Framework';
190+
$secondItem = new stdClass();
191+
$secondItem->firstName = 'Jeremy';
192+
$secondItem->lastName = 'Presutti';
193+
194+
$data = [$firstItem, $secondItem];
195+
196+
$result = Json::marshal($data);
197+
$this->assertEquals('[{"firstName":"FEAST","lastName":"Framework"},{"firstName":"Jeremy","lastName":"Presutti"}]', $result);
198+
}
199+
200+
public function testMarshalStdClass(): void
201+
{
202+
$item = new stdClass();
203+
$item->firstName = 'FEAST';
204+
$item->lastName = 'Framework';
205+
206+
207+
$result = Json::marshal($item);
208+
$this->assertEquals('{"firstName":"FEAST","lastName":"Framework"}', $result);
209+
}
210+
170211
public function testUnmarshalMarshal(): void
171212
{
172213
$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"}';

Json.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Json
5050
public static function marshal(object|array $object, ?int $propertyTypesFlag = null): string
5151
{
5252
if ($object instanceof stdClass || is_array($object)) {
53-
return json_encode($object);
53+
return json_encode(self::marshalArray((array)$object));
5454
}
5555
$return = new stdClass();
5656
$paramInfo = self::getClassParamInfo($object::class, $propertyTypesFlag);

0 commit comments

Comments
 (0)